OEE calculations with Node-Red

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

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":}]

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

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.

1 Like

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

what part its not working ?

I guess u are talking about that part?

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

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.

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;```

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

[{"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"]]}]```

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

image

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.

Thank you so much for help. it really worked. :+1:

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