# Calculate the Difference between two topics

**URL:** <https://discourse.nodered.org/t/calculate-the-difference-between-two-topics/27554>\
**Category:** General\
**Created:** [31 May 2020 04:15 UTC](https://discourse.nodered.org/t/calculate-the-difference-between-two-topics/27554 "2020-05-31T04:15:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Becherer](https://avatars.discourse-cdn.com/v4/letter/b/ec9cab/32.png) [@Becherer](https://discourse.nodered.org/u/Becherer)\
**Post date:** [31 May 2020 04:15 UTC](https://discourse.nodered.org/t/calculate-the-difference-between-two-topics/27554/1 "2020-05-31T04:15:46Z")

</div>

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?

```auto
[{"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"]]}]

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [31 May 2020 10:14 UTC](https://discourse.nodered.org/t/calculate-the-difference-between-two-topics/27554/2 "2020-05-31T10:14:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [31 May 2020 10:18 UTC](https://discourse.nodered.org/t/calculate-the-difference-between-two-topics/27554/3 "2020-05-31T10:18:05Z")

</div>

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](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 🙂

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [31 May 2020 13:12 UTC](https://discourse.nodered.org/t/calculate-the-difference-between-two-topics/27554/4 "2020-05-31T13:12:21Z")

</div>

In particular see [this article in the cookbook](https://cookbook.nodered.org/basic/join-streams) for an example of how to do it.

---

<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:** [30 July 2020 13:12 UTC](https://discourse.nodered.org/t/calculate-the-difference-between-two-topics/27554/5 "2020-07-30T13:12:27Z")

</div>

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