# Substraction 2 msg payload by using topic

**URL:** https://discourse.nodered.org/t/substraction-2-msg-payload-by-using-topic/10471
**Category:** General
**Created:** [23 April 2019 15:40 UTC](https://discourse.nodered.org/t/substraction-2-msg-payload-by-using-topic/10471 "2019-04-23T15:40:30Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![aabbas](https://avatars.discourse-cdn.com/v4/letter/a/3be4f8/32.png) [@aabbas](https://discourse.nodered.org/u/aabbas)
#### Post date: [23 April 2019 15:40 UTC](https://discourse.nodered.org/t/substraction-2-msg-payload-by-using-topic/10471/1 "2019-04-23T15:40:30Z")

</div>

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

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

```

---

<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: [23 April 2019 15:55 UTC](https://discourse.nodered.org/t/substraction-2-msg-payload-by-using-topic/10471/2 "2019-04-23T15:55:33Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)
#### Post date: [23 April 2019 16:01 UTC](https://discourse.nodered.org/t/substraction-2-msg-payload-by-using-topic/10471/3 "2019-04-23T16:01:17Z")

</div>

> [@zenofmud](#):
>
> 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

Good solution from @zenofmud . Alternatively, you can use the [node-red-contrib-combine](https://flows.nodered.org/node/node-red-contrib-combine), more specifically the Delta node.

 ![s-01](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/6/65697003ff002e8ea0512c1b22c032c8947f0f6d.png)

---

<div class="post-metadata">

### Author: ![aabbas](https://avatars.discourse-cdn.com/v4/letter/a/3be4f8/32.png) [@aabbas](https://discourse.nodered.org/u/aabbas)
#### Post date: [7 June 2019 12:14 UTC](https://discourse.nodered.org/t/substraction-2-msg-payload-by-using-topic/10471/4 "2019-06-07T12:14:27Z")

</div>

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

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

```

---

<div class="post-metadata">

### Author: ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)
#### Post date: [7 June 2019 13:27 UTC](https://discourse.nodered.org/t/substraction-2-msg-payload-by-using-topic/10471/5 "2019-06-07T13:27:41Z")

</div>

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

---

<div class="post-metadata">

### Author: ![aabbas](https://avatars.discourse-cdn.com/v4/letter/a/3be4f8/32.png) [@aabbas](https://discourse.nodered.org/u/aabbas)
#### Post date: [7 June 2019 13:43 UTC](https://discourse.nodered.org/t/substraction-2-msg-payload-by-using-topic/10471/6 "2019-06-07T13:43:16Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)
#### Post date: [7 June 2019 13:52 UTC](https://discourse.nodered.org/t/substraction-2-msg-payload-by-using-topic/10471/7 "2019-06-07T13:52:05Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![aabbas](https://avatars.discourse-cdn.com/v4/letter/a/3be4f8/32.png) [@aabbas](https://discourse.nodered.org/u/aabbas)
#### Post date: [7 June 2019 14:13 UTC](https://discourse.nodered.org/t/substraction-2-msg-payload-by-using-topic/10471/8 "2019-06-07T14:13:24Z")

</div>

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

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

```

 ![Capture](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/a/ab2818537f1c98493b955404d09dee0e165b6710.png)

---

<div class="post-metadata">

### Author: ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)
#### Post date: [7 June 2019 14:22 UTC](https://discourse.nodered.org/t/substraction-2-msg-payload-by-using-topic/10471/9 "2019-06-07T14:22:05Z")

</div>

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

---

<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: [7 June 2019 14:24 UTC](https://discourse.nodered.org/t/substraction-2-msg-payload-by-using-topic/10471/10 "2019-06-07T14:24:12Z")

</div>

> [@ukmoose](#):
>
> You need to use a JOIN node to join the two routes together.

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