Substraction 2 msg payload by using topic

Hi Node community!
I need some help in trying to do a subtraction operation between two messages that are msg.payload: Topic like: {“T1”:25,“T2”:22}. I want all time make substraction (T1-T2) even T2 is receiving before T1.
Here the flow I am using but I don't know how can I define the msg payload by topic.

Thank you for your help

[{"id":"582edf40.44d24","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"cdb5c940.30f2f8","type":"function","z":"582edf40.44d24","name":"T1","func":"msg.topic=\"T1\";\nmsg.payload=msg.payload;\nreturn msg;","outputs":1,"noerr":0,"x":590,"y":220,"wires":[["6616ab7b.aa7584"]]},{"id":"d672ce87.fcb22","type":"inject","z":"582edf40.44d24","name":"","topic":"","payload":"25","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":"","x":390,"y":220,"wires":[["cdb5c940.30f2f8"]]},{"id":"dec4e2d.489c32","type":"function","z":"582edf40.44d24","name":"T2","func":"msg.topic=\"T2\";\nmsg.payload=msg.payload;\nreturn msg;","outputs":1,"noerr":0,"x":610,"y":300,"wires":[["6616ab7b.aa7584"]]},{"id":"6616ab7b.aa7584","type":"function","z":"582edf40.44d24","name":"Subtraction","func":"msg.payload = (T1 - T2);\n\nreturn msg;","outputs":1,"noerr":0,"x":870,"y":260,"wires":[["aadca427.0494d8"]]},{"id":"df9deb68.eed9c8","type":"inject","z":"582edf40.44d24","name":"","topic":"","payload":"22","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":"","x":390,"y":300,"wires":[["dec4e2d.489c32"]]},{"id":"aadca427.0494d8","type":"debug","z":"582edf40.44d24","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":1070,"y":260,"wires":[]}]

What you should do is have both msg's go to a join and then to the function so you can access the contents of both in the function - I'd set the mode to maanual and then create a key:value pair

1 Like

Good solution from @zenofmud . Alternatively, you can use the node-red-contrib-combine, more specifically the Delta node.

1 Like

I am using equation node to make the substraction but it doesn’t work. I get all time NaN
Thank you for your help

[{"id":"51e16efc.60bc","type":"inject","z":"10870198.997aae","name":"","topic":"T1","payload":"25","payloadType":"num","repeat":"1","crontab":"","once":false,"onceDelay":"","x":220,"y":240,"wires":[["733fb56c.441bec"]]},{"id":"6679dccc.8b7084","type":"debug","z":"10870198.997aae","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":770,"y":280,"wires":[]},{"id":"f6b2bee9.1dbef","type":"function","z":"10870198.997aae","name":"Subtraction","func":"msg.payload = msg.payload[\"T1\"] - msg.payload[\"T2\"]\nmsg.topic=\"Tf\";\nreturn msg;\n","outputs":1,"noerr":0,"x":610,"y":280,"wires":[["6679dccc.8b7084"]]},{"id":"733fb56c.441bec","type":"function","z":"10870198.997aae","name":"T1","func":"msg.topic=\"T1\";\nreturn msg;","outputs":1,"noerr":0,"x":410,"y":240,"wires":[["f6b2bee9.1dbef"]]},{"id":"f0a8bd35.2b1dc","type":"inject","z":"10870198.997aae","name":"","topic":"T1","payload":"21","payloadType":"num","repeat":"1","crontab":"","once":false,"onceDelay":"","x":220,"y":300,"wires":[["10bb1e8c.d14f31"]]},{"id":"10bb1e8c.d14f31","type":"function","z":"10870198.997aae","name":"T2","func":"msg.topic=\"T2\";\nreturn msg;","outputs":1,"noerr":0,"x":410,"y":300,"wires":[["f6b2bee9.1dbef"]]}]

NaN means not a number
so check your inputs to make sure they are what you think they are

I verified that are number. The function I am using :

msg.payload = msg.payload.T2 - msg.payload.T1
msg.topic="Tf";
return msg;

It is right?

I’m not in front of a computer so. any import your flow but are you trying to do the calculation on two numbers in different messages?

If you are, you need to look at the join node to join the messages together.

Yes i have 2 different messgaes and I want to make the substraction by fixing the ordre of values.

[{"id":"51e16efc.60bc","type":"inject","z":"10870198.997aae","name":"","topic":"T1","payload":"25","payloadType":"num","repeat":"1","crontab":"","once":false,"onceDelay":"","x":220,"y":240,"wires":[["733fb56c.441bec"]]},{"id":"6679dccc.8b7084","type":"debug","z":"10870198.997aae","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":990,"y":300,"wires":[]},{"id":"f6b2bee9.1dbef","type":"function","z":"10870198.997aae","name":"Subtraction","func":"msg.payload = msg.payload.T2 - msg.payload.T1\nmsg.topic=\"Tf\";\nreturn msg;\n","outputs":1,"noerr":0,"x":790,"y":300,"wires":[["6679dccc.8b7084"]]},{"id":"733fb56c.441bec","type":"function","z":"10870198.997aae","name":"T1","func":"msg.topic=\"T1\";\nmsg.payload = Number(msg.payload)\nreturn msg;","outputs":1,"noerr":0,"x":410,"y":240,"wires":[["f6b2bee9.1dbef"]]},{"id":"f0a8bd35.2b1dc","type":"inject","z":"10870198.997aae","name":"","topic":"T2","payload":"21","payloadType":"num","repeat":"1","crontab":"","once":false,"onceDelay":"","x":220,"y":300,"wires":[["10bb1e8c.d14f31"]]},{"id":"10bb1e8c.d14f31","type":"function","z":"10870198.997aae","name":"T2","func":"msg.topic=\"T2\";\nmsg.payload = Number(msg.payload)\nreturn msg;","outputs":1,"noerr":0,"x":410,"y":300,"wires":[["f6b2bee9.1dbef"]]}]

Most nodes have no memory of previous messages. So your subtraction node gats a message doesn’t know what to do with one message.

You need to use a JOIN node to join the two routes together. Also read the “working with messages” page in the docs to see how to use a debug node to get the path to the data you want, right every time

1 Like

Hmmmm I seem to remember telling @aabbas that in this very thread back on April 23.

1 Like