Mathematically dividing two payloads

Hello! I want to divide two payloads. I tried using join node and then calculator node but numerator injects later than denominator. So I used a delay node to delay denominator. Even then it couldn't work as at times numerator didn't inject the first time.

I tried searching for a solution in other forums where function node is being used but couldn't understand. Can anyone help me on this?

you dont need a calculator node for something as simple as a division...

Just add topics to your values and set the join to make a key/value object...
image

function code...

msg.payload = msg.payload.item1 / msg.payload.item2;
return msg;

Demo flow...

[{"id":"78a5547c.17a78c","type":"inject","z":"89aa4a33.4c3098","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"item1","payload":"20","payloadType":"num","x":430,"y":720,"wires":[["f9a52c63.53b6b"]]},{"id":"c9ce3cd8.6bc07","type":"inject","z":"89aa4a33.4c3098","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"item2","payload":"5","payloadType":"num","x":430,"y":760,"wires":[["f9a52c63.53b6b"]]},{"id":"f9a52c63.53b6b","type":"join","z":"89aa4a33.4c3098","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":580,"y":740,"wires":[["5fad36fe.4cd3a8"]]},{"id":"5fad36fe.4cd3a8","type":"function","z":"89aa4a33.4c3098","name":"msg.payload.item1 / msg.payload.item2","func":"msg.payload = msg.payload.item1 / msg.payload.item2;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":840,"y":740,"wires":[["a32f801a.4354"]]},{"id":"a32f801a.4354","type":"debug","z":"89aa4a33.4c3098","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1110,"y":740,"wires":[]}]
1 Like

@Steve-Mcl , I tried. check

[{"id":"8037e618.8cfef8","type":"function","z":"35319a86.99a836","name":"","func":"msg.payload = msg.payload.okprod / msg.payload.target;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1700,"y":420,"wires":[["a32f801a.4354"]]},{"id":"f9a52c63.53b6b","type":"join","z":"35319a86.99a836","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"payload","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1530,"y":420,"wires":[["ceaf078a.34d1d8","8037e618.8cfef8"]]},{"id":"a32f801a.4354","type":"debug","z":"35319a86.99a836","name":"","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1890,"y":420,"wires":[]},{"id":"ceaf078a.34d1d8","type":"debug","z":"35319a86.99a836","name":"","active":false,"tosidebar":false,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":1690,"y":480,"wires":[]},{"id":"49ef46e.67d9ab8","type":"function","z":"35319a86.99a836","name":"","func":"msg.topic=\"okprod\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1320,"y":400,"wires":[["f9a52c63.53b6b","9f0b1bf0.c5dde8"]]},{"id":"5ecae5b.4c5ec1c","type":"function","z":"35319a86.99a836","name":"","func":"msg.topic=\"target\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1320,"y":440,"wires":[["f9a52c63.53b6b","28182701.4807f8"]]},{"id":"9f0b1bf0.c5dde8","type":"debug","z":"35319a86.99a836","name":"","active":false,"tosidebar":false,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":1490,"y":360,"wires":[]},{"id":"28182701.4807f8","type":"debug","z":"35319a86.99a836","name":"","active":false,"tosidebar":false,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":1490,"y":480,"wires":[]}]

Check your values going into join have a topic.

Check the payloads going into join contain a number.

Check the join node is set exactly as I showed.

@Steve-Mcl Yes, corrected. Its working now. thankyou.

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