# Simple maths flow to create difference between two temperature sensors

**URL:** <https://discourse.nodered.org/t/simple-maths-flow-to-create-difference-between-two-temperature-sensors/54898>\
**Category:** General\
**Created:** [8 December 2021 21:56 UTC](https://discourse.nodered.org/t/simple-maths-flow-to-create-difference-between-two-temperature-sensors/54898 "2021-12-08T21:56:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![neilsonwheels](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/neilsonwheels/32/52311_2.png) [@neilsonwheels](https://discourse.nodered.org/u/neilsonwheels)\
**Post date:** [8 December 2021 21:56 UTC](https://discourse.nodered.org/t/simple-maths-flow-to-create-difference-between-two-temperature-sensors/54898/1 "2021-12-08T21:56:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [8 December 2021 22:33 UTC](https://discourse.nodered.org/t/simple-maths-flow-to-create-difference-between-two-temperature-sensors/54898/2 "2021-12-08T22:33:49Z")

</div>

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.

```auto
[{"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](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](https://nodered.org/docs/user-guide/messages)

---

<div class="post-metadata">

**Author:** ![neilsonwheels](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/neilsonwheels/32/52311_2.png) [@neilsonwheels](https://discourse.nodered.org/u/neilsonwheels)\
**Post date:** [9 December 2021 00:23 UTC](https://discourse.nodered.org/t/simple-maths-flow-to-create-difference-between-two-temperature-sensors/54898/3 "2021-12-09T00:23:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [23 December 2021 00:23 UTC](https://discourse.nodered.org/t/simple-maths-flow-to-create-difference-between-two-temperature-sensors/54898/4 "2021-12-23T00:23:47Z")

</div>

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