Simple maths flow to create difference between two temperature sensors

I have two temperature sensors in my Home Assistant. I would like to create a new value which is the difference of them, in other words Temperature Reading A minus Temperature Reading B = Temperature Reading C.
Could someone give me a hand to learn how to do this? I am pretty new to Node Red and mainly stick to creating flows via the GUI.

Here is an example of joining to messages and subtracting them, note i add topics to each wire. and use JSONata to subtract the values. You could also use a function node and Javascript.
Copy flow json, press ctrl i in editor, then paste code and inport.

[{"id":"16ae6b9d.e0f76c","type":"inject","z":"c791cbc0.84f648","name":"temp a","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"24","payloadType":"num","x":130,"y":520,"wires":[["b528afe1.ab3648"]]},{"id":"b528afe1.ab3648","type":"change","z":"c791cbc0.84f648","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"tempA","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":520,"wires":[["16697299.3924dd"]]},{"id":"16697299.3924dd","type":"join","z":"c791cbc0.84f648","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":490,"y":540,"wires":[["66020790.b3f578"]]},{"id":"e5241039.90a828","type":"change","z":"c791cbc0.84f648","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"tempB","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":560,"wires":[["16697299.3924dd"]]},{"id":"66020790.b3f578","type":"change","z":"c791cbc0.84f648","name":"","rules":[{"t":"set","p":"payload.TempC","pt":"msg","to":"payload.tempA - payload.tempB","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":690,"y":540,"wires":[["d6bf0095.97ba38"]]},{"id":"f4112c83.194c6","type":"inject","z":"c791cbc0.84f648","name":"temp b","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"26","payloadType":"num","x":120,"y":560,"wires":[["e5241039.90a828"]]},{"id":"d6bf0095.97ba38","type":"debug","z":"c791cbc0.84f648","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":740,"y":460,"wires":[]}]

here is a tutorial on joining messages
https://cookbook.nodered.org/basic/join-streams

and this is worth a read and the videos worth watching
https://nodered.org/docs/user-guide/messages

2 Likes

That's perfect, thank very much. I was able to get it working and better still able to understand how it's done.

1 Like

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