# OEE calculations with Node-Red

**URL:** <https://discourse.nodered.org/t/oee-calculations-with-node-red/45855>\
**Category:** Industrial\
**Created:** [18 May 2021 13:26 UTC](https://discourse.nodered.org/t/oee-calculations-with-node-red/45855 "2021-05-18T13:26:48Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shubhangi](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@Shubhangi](https://discourse.nodered.org/u/Shubhangi)\
**Post date:** [18 May 2021 13:26 UTC](https://discourse.nodered.org/t/oee-calculations-with-node-red/45855/1 "2021-05-18T13:26:48Z")

</div>

Hi, I am trying to make OEE calculations with the help of node red and firstly I have to make production tracking of units per hour. So, I have to multiply hours with the units(example:- hours\*units) and I am taking data from json(inject node ), but its not working, so could you help me find the write code or check my flow and tell me what is wrong with it.  
Thank you  
shubhangi

```auto
var value = flow.get('pieceValue');
var h = flow.get('hours');
var productionPerHour = flow.get('hourlyProduction');

productionPerHour = value/(h*24)||0;
if (productionPerHour.length<=1000);
    productionPerHour+1;
    value+2;

flow.set('pieceValue', value);  
flow.set('hours', h);
flow.set('hourlyProduction', productionPerHour);
    
//msg.payload = { hourlyProduction: msg.payload.productionPerHour };

return msg;

```

**FLOW**

> [{"id":"604bd7fa.9f54b8","type":"inject","z":"b6b3d834.5b80c8","name":"Machine 001","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"60","crontab":"","once":true,"onceDelay":"60","topic":"","payload":"{"machineid":"001","productid":"01","piecespercycle":2,"stdcycletime":8,"stdhourlyprodn":120,"stdloadunloadtime":6,"setupchangetime":2,"orderno":"1","status":"Running"}","payloadType":"json","x":140,"y":700,"wires":[["97e48b42.6389c8","2f65c548.85313a"]]},{"id":"97e48b42.6389c8","type":"function","z":"b6b3d834.5b80c8","name":"pieceValue","func":"pieceValue = msg.payload.piecespercycle;\nmsg.payload = pieceValue;\nreturn msg;\n\n\n\n\n\n\n\n\n\n//p=JSON.parse(msg.payload);\n//a=msg.payload;\n//node.log(typeof a);\n//msg.payload = a.piecespercycle;\n//return msg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":350,"y":640,"wires":[["1b5b43da.cd6ddc"]]},{"id":"1b5b43da.cd6ddc","type":"function","z":"b6b3d834.5b80c8","name":"Hourly Production","func":"var value = flow.get('pieceValue');\nvar h = flow.get('hours');\nvar productionPerHour = flow.get('hourlyProduction');\n\n\nproductionPerHour = value/(h\*24)||0;\nif (productionPerHour.length\<=1000);\n productionPerHour+1;\n value+2;\n\nflow.set('pieceValue', value); \nflow.set('hours', h);\nflow.set('hourlyProduction', productionPerHour);\n \n//msg.payload = { hourlyProduction: msg.payload.productionPerHour };\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":590,"y":640,"wires":[["1afe9010.fec6c"]]},{"id":"2f65c548.85313a","type":"function","z":"b6b3d834.5b80c8","name":"Time","func":"var d = new Date();\nvar dString = d.toISOString();\nvar hours = d.getHours();\nvar minutes = d.getMinutes();\n\nmsg.payload = dString; \n\nflow.set("hours",hours);\n\nflow.set("minutes",minutes);\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":290,"y":760,"wires":[["f6d52985.e094e8","e427527b.4b7d6","2caf5fa5.c115e"]]},{"id":"f6d52985.e094e8","type":"function","z":"b6b3d834.5b80c8","name":"Hours","func":"msg.payload = flow.get("hours");\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":450,"y":760,"wires":[["70c61ae8.393024","1b5b43da.cd6ddc"]]},{"id":"e427527b.4b7d6","type":"function","z":"b6b3d834.5b80c8","name":"Minutes","func":"msg.payload = flow.get("minutes");\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":460,"y":820,"wires":[["3a196f04.a430a"]]},{"id":"1afe9010.fec6c","type":"change","z":"b6b3d834.5b80c8","name":"Hourly Production","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.hourlyProduction","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":810,"y":640,"wires":[["4624021a.b0812c"]]},{"id":"2caf5fa5.c115e","type":"change","z":"b6b3d834.5b80c8","name":"Time","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.Time","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":610,"y":700,"wires":[["4624021a.b0812c"]]},{"id":"70c61ae8.393024","type":"change","z":"b6b3d834.5b80c8","name":"Hours","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.hours","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":630,"y":760,"wires":[["4624021a.b0812c"]]},{"id":"3a196f04.a430a","type":"change","z":"b6b3d834.5b80c8","name":"Minutes","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.minutes","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":820,"wires":[["4624021a.b0812c"]]},{"id":"4624021a.b0812c","type":"join","z":"b6b3d834.5b80c8","name":"","mode":"custom","build":"merged","property":"payload","propertyType":"msg","key":"topic","joiner":"\n","joinerType":"str","accumulate":false,"timeout":"","count":"4","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":910,"y":740,"wires":[["bc426181.4222c"]]},{"id":"bc426181.4222c","type":"debug","z":"b6b3d834.5b80c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1090,"y":740,"wires":}]

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [18 May 2021 14:00 UTC](https://discourse.nodered.org/t/oee-calculations-with-node-red/45855/2 "2021-05-18T14:00:29Z")

</div>

Hi, in order to make code more readable and importable it is important to surround your code with three backticks  
`````  
`like this`  
`````

You can edit and correct your post by clicking the pencil icon.

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

Also have a look at the node red docs page Writing Functions, that will show you how you can use `node.warn()` to display data from your function in the debug pane. That will enable you to work out which bit of your function is not doing what you expected.

---

<div class="post-metadata">

**Author:** ![Shubhangi](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@Shubhangi](https://discourse.nodered.org/u/Shubhangi)\
**Post date:** [19 May 2021 11:38 UTC](https://discourse.nodered.org/t/oee-calculations-with-node-red/45855/3 "2021-05-19T11:38:45Z")

</div>

Thank you for the reply. I'll make sure to write my query correctly for the next time and thank you for the help. 🙂

---

<div class="post-metadata">

**Author:** ![Cristiano](https://avatars.discourse-cdn.com/v4/letter/c/b5e925/32.png) [@Cristiano](https://discourse.nodered.org/u/Cristiano)\
**Post date:** [22 May 2021 00:56 UTC](https://discourse.nodered.org/t/oee-calculations-with-node-red/45855/4 "2021-05-22T00:56:09Z")

</div>

> [@Shubhangi](#):
>
> [{"id":"604bd7fa.9f54b8","type":"inject","z":"b6b3d834.5b80c8","name":"Machine 001","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"60","crontab":"","once":true,"onceDelay":"60","topic":"","payload":"{"machineid":"001","productid":"01","piecespercycle":2,"stdcycletime":8,"stdhourlyprodn":120,"stdloadunloadtime":6,"setupchangetime":2,"orderno":"1","status":"Running"}","payloadType":"json","x":140,"y":700,"wires":[["97e48b42.6389c8","2f65c548.85313a"]]},{"id":"97e48b42.6389c8","type":"function","z":"b6b3d834.5b80c8","name":"pieceValue","func":"pieceValue = msg.payload.piecespercycle;\nmsg.payload = pieceValue;\nreturn msg;\n\n\n\n\n\n\n\n\n\n//p=JSON.parse(msg.payload);\n//a=msg.payload;\n//node.log(typeof a);\n//msg.payload = a.piecespercycle;\n//return msg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":350,"y":640,"wires":[["1b5b43da.cd6ddc"]]},{"id":"1b5b43da.cd6ddc","type":"function","z":"b6b3d834.5b80c8","name":"Hourly Production","func":"var value = flow.get('pieceValue');\nvar h = flow.get('hours');\nvar productionPerHour = flow.get('hourlyProduction');\n\n\nproductionPerHour = value/(h\*24)||0;\nif (productionPerHour.length\<=1000);\n productionPerHour+1;\n value+2;\n\nflow.set('pieceValue', value); \nflow.set('hours', h);\nflow.set('hourlyProduction', productionPerHour);\n \n//msg.payload = { hourlyProduction: msg.payload.productionPerHour };\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":590,"y":640,"wires":[["1afe9010.fec6c"]]},{"id":"2f65c548.85313a","type":"function","z":"b6b3d834.5b80c8","name":"Time","func":"var d = new Date();\nvar dString = d.toISOString();\nvar hours = d.getHours();\nvar minutes = d.getMinutes();\n\nmsg.payload = dString; \n\nflow.set("hours",hours);\n\nflow.set("minutes",minutes);\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":290,"y":760,"wires":[["f6d52985.e094e8","e427527b.4b7d6","2caf5fa5.c115e"]]},{"id":"f6d52985.e094e8","type":"function","z":"b6b3d834.5b80c8","name":"Hours","func":"msg.payload = flow.get("hours");\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":450,"y":760,"wires":[["70c61ae8.393024","1b5b43da.cd6ddc"]]},{"id":"e427527b.4b7d6","type":"function","z":"b6b3d834.5b80c8","name":"Minutes","func":"msg.payload = flow.get("minutes");\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":460,"y":820,"wires":[["3a196f04.a430a"]]},{"id":"1afe9010.fec6c","type":"change","z":"b6b3d834.5b80c8","name":"Hourly Production","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.hourlyProduction","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":810,"y":640,"wires":[["4624021a.b0812c"]]},{"id":"2caf5fa5.c115e","type":"change","z":"b6b3d834.5b80c8","name":"Time","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.Time","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":610,"y":700,"wires":[["4624021a.b0812c"]]},{"id":"70c61ae8.393024","type":"change","z":"b6b3d834.5b80c8","name":"Hours","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.hours","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":630,"y":760,"wires":[["4624021a.b0812c"]]},{"id":"3a196f04.a430a","type":"change","z":"b6b3d834.5b80c8","name":"Minutes","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.minutes","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":820,"wires":[["4624021a.b0812c"]]},{"id":"4624021a.b0812c","type":"join","z":"b6b3d834.5b80c8","name":"","mode":"custom","build":"merged","property":"payload","propertyType":"msg","key":"topic","joiner":"\n","joinerType":"str","accumulate":false,"timeout":"","count":"4","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":910,"y":740,"wires":[["bc426181.4222c"]]},{"id":"bc426181.4222c","type":"debug","z":"b6b3d834.5b80c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1090,"y":740,"wires":}]

what part its not working ?

I guess u are talking about that part?

```auto
productionPerHour = value/(h*24)||0;
if (productionPerHour.length<=1000){
    productionPerHour+=1;
    value+=2;
}

```

---

<div class="post-metadata">

**Author:** ![Shubhangi](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@Shubhangi](https://discourse.nodered.org/u/Shubhangi)\
**Post date:** [22 May 2021 09:09 UTC](https://discourse.nodered.org/t/oee-calculations-with-node-red/45855/5 "2021-05-22T09:09:26Z")

</div>

Yes, actually I'm just a Computer Application student and I got this opportunity to work with a company and they asked me to learn Node-red, and make an oee dashboard with help of node-red and Grafana. So, with above code I wanted to show production per minute and hour in the dashboard. And I wanted to show how many units a machine is producing according to the hour or minute of the day. So, yeah the code is actually the data I have got from the internet only. i actually have many doubts but is where is was really stuck so here i am.

its working with node.warn().  
but i changed the code a little bit.

````auto
var value = flow.get("pieceValue");
var h = flow.get("hours");
var productionPerHour = flow.get("hourlyProduction");

productionPerHour = value*h||0;
if (productionPerHour<=600);
    productionPerHour+1;
    value+2;

flow.set("pieceValue", value);  
flow.set("hours", h);
flow.set("hourlyProduction", productionPerHour);

node.warn(productionPerHour);

//msg.payload = { hourlyProduction: msg.payload.productionPerHour };

return msg;```
````

---

<div class="post-metadata">

**Author:** ![Shubhangi](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@Shubhangi](https://discourse.nodered.org/u/Shubhangi)\
**Post date:** [29 May 2021 11:40 UTC](https://discourse.nodered.org/t/oee-calculations-with-node-red/45855/6 "2021-05-29T11:40:27Z")

</div>

Hi, sorry to ask this question again but in my code the output is NaN again and also the increment operator is not working (i++), could you please tell me what will be best for my code. And how to solve this little mistakes. And also why node.warn() doesn't work with join node ?

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/d/0d1d0b4b30d25e8f3d839032f0b669cab6705698.png)

````auto
[{"id":"66cab93.d788048","type":"debug","z":"b6b3d834.5b80c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1070,"y":660,"wires":[]},{"id":"604bd7fa.9f54b8","type":"inject","z":"b6b3d834.5b80c8","name":"Machine 001","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"60","crontab":"","once":true,"onceDelay":"60","topic":"","payload":"{\"machineid\":\"001\",\"productid\":\"01\",\"piecespercycle\":2,\"stdcycletime\":8,\"stdhourlyprodn\":120,\"stdloadunloadtime\":6,\"setupchangetime\":2,\"orderno\":\"1\",\"status\":\"Running\"}","payloadType":"json","x":140,"y":700,"wires":[["97e48b42.6389c8","2f65c548.85313a","5a257766.baef38"]]},{"id":"97e48b42.6389c8","type":"function","z":"b6b3d834.5b80c8","name":"pieceValue","func":"pieceValue = msg.payload.piecespercycle;\nmsg.payload = pieceValue;\nreturn msg;\n\n\n\n\n\n\n\n\n\n//p=JSON.parse(msg.payload);\n//a=msg.payload;\n//node.log(typeof a);\n//msg.payload = a.piecespercycle;\n//return msg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":350,"y":640,"wires":[["4040bdec.e03c64"]]},{"id":"2f65c548.85313a","type":"function","z":"b6b3d834.5b80c8","name":"Time","func":"var d = new Date();\nvar dString = d.toISOString();\nvar hours = d.getHours();\nvar minutes = d.getMinutes();\n\nmsg.payload = dString; \n\nflow.set(\"hours\",hours);\n\nflow.set(\"minutes\",minutes);\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":330,"y":760,"wires":[["f6d52985.e094e8","e427527b.4b7d6","2caf5fa5.c115e"]]},{"id":"f6d52985.e094e8","type":"function","z":"b6b3d834.5b80c8","name":"Hours","func":"msg.payload = flow.get(\"hours\");\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":450,"y":800,"wires":[["70c61ae8.393024"]]},{"id":"e427527b.4b7d6","type":"function","z":"b6b3d834.5b80c8","name":"Minutes","func":"msg.payload = flow.get(\"minutes\");\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":440,"y":860,"wires":[["3a196f04.a430a","4040bdec.e03c64"]]},{"id":"2caf5fa5.c115e","type":"change","z":"b6b3d834.5b80c8","name":"Time","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.Time","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":670,"y":760,"wires":[["4624021a.b0812c"]]},{"id":"70c61ae8.393024","type":"change","z":"b6b3d834.5b80c8","name":"Hours","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.hours","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":670,"y":800,"wires":[["4624021a.b0812c"]]},{"id":"3a196f04.a430a","type":"change","z":"b6b3d834.5b80c8","name":"Minutes","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.minutes","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":660,"y":860,"wires":[["4624021a.b0812c"]]},{"id":"4624021a.b0812c","type":"join","z":"b6b3d834.5b80c8","name":"","mode":"custom","build":"merged","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"4","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":910,"y":760,"wires":[["bc426181.4222c"]]},{"id":"bc426181.4222c","type":"debug","z":"b6b3d834.5b80c8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1090,"y":760,"wires":[]},{"id":"4040bdec.e03c64","type":"function","z":"b6b3d834.5b80c8","name":"unitsPerMinute","func":"var i = flow.get('pieceValue');\nvar n = flow.get('cycletimeValue');\nvar m = flow.get('minutes');\nvar u = flow.get('unitsPerMinute')||0;\n\nu = i*n;\nif (u<=600)\n i++;\n\nflow.set('pieceValue', i);\nflow.set('cycleTimeValue', n);\nflow.set('minutes', m);\nflow.set('unitsPerMinute', u);\n\nnode.warn(u);\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":580,"y":660,"wires":[["be48503.69865b"]]},{"id":"5a257766.baef38","type":"function","z":"b6b3d834.5b80c8","name":"cycletimeValue","func":"\ncycletimeValue = msg.payload.stdcycletime;\nmsg.payload = cycletimeValue;\nreturn msg;\n\n\n\n\n\n\n\n\n\n//d=msg.payload;\n//node.log(typeof d);\n//msg.payload = d.stdcycletime;\n//return msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":360,"y":700,"wires":[["4040bdec.e03c64"]]},{"id":"be48503.69865b","type":"change","z":"b6b3d834.5b80c8","name":"Units Per Minute","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.unitsPerMinute","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":780,"y":660,"wires":[["4624021a.b0812c","66cab93.d788048"]]}]```
````

---

<div class="post-metadata">

**Author:** ![machadotiago](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/machadotiago/32/23776_2.png) [@machadotiago](https://discourse.nodered.org/u/machadotiago)\
**Post date:** [1 June 2021 22:22 UTC](https://discourse.nodered.org/t/oee-calculations-with-node-red/45855/7 "2021-06-01T22:22:54Z")

</div>

I strongly recommend you to put this data on a database (like sqlite) and do the math in the query.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [2 June 2021 07:37 UTC](https://discourse.nodered.org/t/oee-calculations-with-node-red/45855/8 "2021-06-02T07:37:39Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/6/96feec07867eec908cb8dc75cda5e323fa11b3aa.png)

That is horrible. Don't split it up and then try to join the flows back together again. Keep the functions one after another. There is no need to extract the individual payload properties to the payload and pass them on, just use them directly from the object in the payload when you need them.

---

<div class="post-metadata">

**Author:** ![Shubhangi](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@Shubhangi](https://discourse.nodered.org/u/Shubhangi)\
**Post date:** [2 June 2021 11:17 UTC](https://discourse.nodered.org/t/oee-calculations-with-node-red/45855/9 "2021-06-02T11:17:08Z")

</div>

Thank you so much for help. it really worked. 👍

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [16 June 2021 11:17 UTC](https://discourse.nodered.org/t/oee-calculations-with-node-red/45855/10 "2021-06-16T11:17:17Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
