Calculate the Difference between two topics

Hello, I am needing to display the difference between two number of sensors. The two sensors are coming in on different topics but share the same payload. Lets say if I wanted to have a sensor outside and one inside, I want to have a function that would take the topic.outside and - from topic.inside.

I dont know I to write the function with it being two different topics. Any ideas?

[{"id":"a6318325.434c","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"77b51b25.caef14","type":"function","z":"a6318325.434c","name":"Outside Temp","func":"msg.topic=\"Outside Temp\";\nreturn msg;","outputs":1,"noerr":0,"x":380,"y":120,"wires":[["66662f4c.5054c"]]},{"id":"a41b88eb.9ad4d8","type":"function","z":"a6318325.434c","name":"Inside Temp","func":"msg.topic=\"Inside Temp\";\nreturn msg;","outputs":1,"noerr":0,"x":370,"y":180,"wires":[["e1d1f82f.975308"]]},{"id":"66662f4c.5054c","type":"change","z":"a6318325.434c","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":540,"wires":[["55a345a4.d3fefc"]]},{"id":"e1d1f82f.975308","type":"change","z":"a6318325.434c","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":600,"wires":[["55a345a4.d3fefc"]]},{"id":"55a345a4.d3fefc","type":"function","z":"a6318325.434c","name":"Subtraction","func":"msg.payload = msg.payload[\"Out\"] - msg.payload[\"In\"]\nreturn msg;\n","outputs":1,"noerr":0,"x":590,"y":560,"wires":[["71e6567.d1e71a8"]]},{"id":"71e6567.d1e71a8","type":"debug","z":"a6318325.434c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":730,"y":500,"wires":[]},{"id":"6ae66d33.3236a4","type":"inject","z":"a6318325.434c","name":"Inside Temp","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":80,"wires":[["a41b88eb.9ad4d8"]]},{"id":"1e8e8cf.4ca3373","type":"inject","z":"a6318325.434c","name":"Outside Temp","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":40,"wires":[["77b51b25.caef14"]]}]

Try sending them both to a join node. Then both values will be in the same object and you can use a simple switch node to compare them.
Be sure to add a debug node before and after the join node to see what it's doing.

The first thing you need to realize is that msg's are single threaded so when you press the "Outside Temp" inject node, when that msg gets the the "Subtraction" function node, that node will process and not wait for the msg from the other inject node.

You should take a few minutes and tead https://nodered.org/docs/user-guide/messages

There are a couple ways you could go. You could save the teo temperautres in flow variables, or you could use a join node before the "Subtraction" node so that both pieces of data arrive in the same message when they get to the "Subtraction" node.

Try the join node first and if you have questions let us know.

EDIT: I see @Steve-Mcl answered while I was typing :slightly_smiling_face:

In particular see this article in the cookbook for an example of how to do it.

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