# How to do a simple sum of 2 msgs

**URL:** <https://discourse.nodered.org/t/how-to-do-a-simple-sum-of-2-msgs/5382>\
**Category:** General\
**Created:** [1 December 2018 19:47 UTC](https://discourse.nodered.org/t/how-to-do-a-simple-sum-of-2-msgs/5382 "2018-12-01T19:47:03Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ederpereira92](https://avatars.discourse-cdn.com/v4/letter/e/d9b06d/32.png) [@ederpereira92](https://discourse.nodered.org/u/ederpereira92)\
**Post date:** [1 December 2018 19:47 UTC](https://discourse.nodered.org/t/how-to-do-a-simple-sum-of-2-msgs/5382/1 "2018-12-01T19:47:03Z")

</div>

Hello,  
I`m working in my final paper and I find myself in a situation I can`t solve a simple sum of two mqtt messages.

 ![14](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/b/bf8d58e59dc52376759556500cee1ea16104fb58.png)

Could someone help-me?

---

<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:** [1 December 2018 20:17 UTC](https://discourse.nodered.org/t/how-to-do-a-simple-sum-of-2-msgs/5382/2 "2018-12-01T20:17:54Z")

</div>

Each msg going into the function will be acted upon seperately. Take a look at the join node.

---

<div class="post-metadata">

**Author:** ![DevThePhoenix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/devthephoenix/32/4153_2.png) [@DevThePhoenix](https://discourse.nodered.org/u/DevThePhoenix)\
**Post date:** [2 December 2018 06:29 UTC](https://discourse.nodered.org/t/how-to-do-a-simple-sum-of-2-msgs/5382/3 "2018-12-02T06:29:09Z")

</div>

1. You can use join node
2. In Fuction node define two msg.payloads as flows and add the two flows data and take output as single or multiple outputs.  
i hope you gone through documentation of fuction node

---

<div class="post-metadata">

**Author:** ![ederpereira92](https://avatars.discourse-cdn.com/v4/letter/e/d9b06d/32.png) [@ederpereira92](https://discourse.nodered.org/u/ederpereira92)\
**Post date:** [2 December 2018 18:26 UTC](https://discourse.nodered.org/t/how-to-do-a-simple-sum-of-2-msgs/5382/4 "2018-12-02T18:26:28Z")

</div>

> [@DevThePhoenix](#):
>
> msg.payload

Could you helpme with the code?  
I`ve tried different ones, but none worked.

---

<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:** [2 December 2018 19:25 UTC](https://discourse.nodered.org/t/how-to-do-a-simple-sum-of-2-msgs/5382/5 "2018-12-02T19:25:27Z")

</div>

Show us what you tried

---

<div class="post-metadata">

**Author:** ![machadotiago](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/machadotiago/32/23776_2.png) [@machadotiago](https://discourse.nodered.org/u/machadotiago)\
**Post date:** [2 December 2018 19:34 UTC](https://discourse.nodered.org/t/how-to-do-a-simple-sum-of-2-msgs/5382/6 "2018-12-02T19:34:52Z")

</div>

Hi Eder,  
first of all: you don't need a function node for that. Avoid using function nodes for such a simple task. I use node-red very often and it is really really rare the situations where I need to use a function. Usually all situations are solved by using "switch", "change", "split" and "join".

In your case you can do something like this:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/1/16f9291b56d4265ddaba9eec15c70cccf6bc2ce1.png)

```
[{"id":"490e81a5.72c82","type":"inject","z":"2901a55a.0bd27a","name":"","topic":"","payload":"2","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":120,"wires":[["31c9f087.3438f"]]},{"id":"e8709086.2de81","type":"inject","z":"2901a55a.0bd27a","name":"","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":160,"wires":[["7b8e0037.f117a"]]},{"id":"ce878c18.15589","type":"join","z":"2901a55a.0bd27a","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":510,"y":120,"wires":[["bac9a9fd.bbd678"]]},{"id":"31c9f087.3438f","type":"change","z":"2901a55a.0bd27a","name":"","rules":[{"t":"set","p":"parts","pt":"msg","to":"1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":120,"wires":[["ce878c18.15589"]]},{"id":"7b8e0037.f117a","type":"change","z":"2901a55a.0bd27a","name":"","rules":[{"t":"set","p":"parts","pt":"msg","to":"2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":160,"wires":[["ce878c18.15589"]]},{"id":"bac9a9fd.bbd678","type":"change","z":"2901a55a.0bd27a","name":"$sum(payload)","rules":[{"t":"set","p":"payload","pt":"msg","to":"$sum(payload)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":680,"y":120,"wires":[["5fc858e8.750c58"]]},{"id":"5fc858e8.750c58","type":"debug","z":"2901a55a.0bd27a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","x":890,"y":120,"wires":[]}]

```

or even something like this:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/b/b3c9a473fd0437039ea9f0b4d47a6dc0aae11078.png)

```
[{"id":"490e81a5.72c82","type":"inject","z":"2901a55a.0bd27a","name":"","topic":"","payload":"2","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":120,"wires":[["31c9f087.3438f"]]},{"id":"e8709086.2de81","type":"inject","z":"2901a55a.0bd27a","name":"","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":160,"wires":[["60ed0472.c5a9dc"]]},{"id":"31c9f087.3438f","type":"change","z":"2901a55a.0bd27a","name":"","rules":[{"t":"set","p":"var1","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":120,"wires":[["bac9a9fd.bbd678"]]},{"id":"bac9a9fd.bbd678","type":"change","z":"2901a55a.0bd27a","name":"sum","rules":[{"t":"set","p":"payload","pt":"msg","to":"$flowContext('var1')+$flowContext('var2')","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":530,"y":140,"wires":[["5fc858e8.750c58"]]},{"id":"5fc858e8.750c58","type":"debug","z":"2901a55a.0bd27a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","x":750,"y":140,"wires":[]},{"id":"60ed0472.c5a9dc","type":"change","z":"2901a55a.0bd27a","name":"","rules":[{"t":"set","p":"var2","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":160,"wires":[["bac9a9fd.bbd678"]]}]
```

---

<div class="post-metadata">

**Author:** ![ederpereira92](https://avatars.discourse-cdn.com/v4/letter/e/d9b06d/32.png) [@ederpereira92](https://discourse.nodered.org/u/ederpereira92)\
**Post date:** [2 December 2018 19:48 UTC](https://discourse.nodered.org/t/how-to-do-a-simple-sum-of-2-msgs/5382/7 "2018-12-02T19:48:11Z")

</div>

> [@machadotiago](#):
>
> [{"id":"490e81a5.72c82","type":"inject","z":"2901a55a.0bd27a","name":"","topic":"","payload":"2","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":120,"wires":[["31c9f087.3438f"]]},{"id":"e8709086.2de81","type":"inject","z":"2901a55a.0bd27a","name":"","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":160,"wires":[["60ed0472.c5a9dc"]]},{"id":"31c9f087.3438f","type":"change","z":"2901a55a.0bd27a","name":"","rules":[{"t":"set","p":"var1","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":120,"wires":[["bac9a9fd.bbd678"]]},{"id":"bac9a9fd.bbd678","type":"change","z":"2901a55a.0bd27a","name":"sum","rules":[{"t":"set","p":"payload","pt":"msg","to":"$flowContext('var1')+$flowContext('var2')","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":530,"y":140,"wires":[["5fc858e8.750c58"]]},{"id":"5fc858e8.750c58","type":"debug","z":"2901a55a.0bd27a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","x":750,"y":140,"wires":},{"id":"60ed0472.c5a9dc","type":"change","z":"2901a55a.0bd27a","name":"","rules":[{"t":"set","p":"var2","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":160,"wires":[["bac9a9fd.bbd678"]]}]

I tried the two options. None worked.  
This is what a tried.  
`[{"id":"2a7ab69f.a264ca","type":"mqtt in","z":"53c582.5dd31a8","name":"","topic":"V1","qos":"1","broker":"89cee77f.d83e78","x":150,"y":220,"wires":[["df44497c.180d78"]]},{"id":"e3422c01.7e31d","type":"mqtt in","z":"53c582.5dd31a8","name":"","topic":"V2","qos":"1","broker":"89cee77f.d83e78","x":150,"y":300,"wires":[["5c352832.8cd458"]]},{"id":"e59efe2e.e5ad5","type":"join","z":"53c582.5dd31a8","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":490,"y":240,"wires":[["f5c0b9ab.c10208"]]},{"id":"f5c0b9ab.c10208","type":"change","z":"53c582.5dd31a8","name":"$sum(payload)","rules":[{"t":"set","p":"payload","pt":"msg","to":"$sum(payload)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":660,"y":240,"wires":[["dc3533b7.3c382"]]},{"id":"dc3533b7.3c382","type":"debug","z":"53c582.5dd31a8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":850,"y":240,"wires":[]},{"id":"c39651c9.b6f5f","type":"mqtt in","z":"53c582.5dd31a8","name":"","topic":"V2","qos":"1","broker":"89cee77f.d83e78","x":150,"y":480,"wires":[["5f832ae8.939834"]]},{"id":"4ddbf2da.38324c","type":"mqtt in","z":"53c582.5dd31a8","name":"","topic":"V1","qos":"1","broker":"89cee77f.d83e78","x":150,"y":400,"wires":[["333e8100.3ef77"]]},{"id":"333e8100.3ef77","type":"change","z":"53c582.5dd31a8","name":"","rules":[{"t":"set","p":"var1","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":420,"wires":[["50af2dc8.ca4c44"]]},{"id":"5f832ae8.939834","type":"change","z":"53c582.5dd31a8","name":"","rules":[{"t":"set","p":"var2","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":460,"wires":[["50af2dc8.ca4c44"]]},{"id":"50af2dc8.ca4c44","type":"change","z":"53c582.5dd31a8","name":"sum","rules":[{"t":"set","p":"payload","pt":"msg","to":"$flowContext('var1')+$flowContext('var2')","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":440,"wires":[["9f4f2e16.8278d"]]},{"id":"df44497c.180d78","type":"change","z":"53c582.5dd31a8","name":"","rules":[{"t":"set","p":"parts","pt":"msg","to":"1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":240,"wires":[["e59efe2e.e5ad5"]]},{"id":"5c352832.8cd458","type":"change","z":"53c582.5dd31a8","name":"","rules":[{"t":"set","p":"parts","pt":"msg","to":"2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":280,"wires":[["e59efe2e.e5ad5"]]},{"id":"9f4f2e16.8278d","type":"debug","z":"53c582.5dd31a8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":690,"y":440,"wires":[]},{"id":"89cee77f.d83e78","type":"mqtt-broker","z":"","name":"","broker":"m11.cloudmqtt.com","port":"10808","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]`

---

<div class="post-metadata">

**Author:** ![machadotiago](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/machadotiago/32/23776_2.png) [@machadotiago](https://discourse.nodered.org/u/machadotiago)\
**Post date:** [2 December 2018 20:55 UTC](https://discourse.nodered.org/t/how-to-do-a-simple-sum-of-2-msgs/5382/8 "2018-12-02T20:55:08Z")

</div>

Hi Eder,  
I pasted your flow on my system and both options are working fine.  
Are you sure you are receiving the MQTT messages? Could you please add Debug nodes right after the MQTT nodes, configure it for "complete message", wait until the messages arrive and paste the debug result (screenshot) here?

---

<div class="post-metadata">

**Author:** ![ederpereira92](https://avatars.discourse-cdn.com/v4/letter/e/d9b06d/32.png) [@ederpereira92](https://discourse.nodered.org/u/ederpereira92)\
**Post date:** [2 December 2018 21:02 UTC](https://discourse.nodered.org/t/how-to-do-a-simple-sum-of-2-msgs/5382/9 "2018-12-02T21:02:13Z")

</div>

The MQTT node is working.

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

---

<div class="post-metadata">

**Author:** ![ederpereira92](https://avatars.discourse-cdn.com/v4/letter/e/d9b06d/32.png) [@ederpereira92](https://discourse.nodered.org/u/ederpereira92)\
**Post date:** [2 December 2018 21:15 UTC](https://discourse.nodered.org/t/how-to-do-a-simple-sum-of-2-msgs/5382/10 "2018-12-02T21:15:43Z")

</div>

It`s showing this message.  
Invalid JSONata expression: The left side of the "+" operator must evaluate to a number

> [{"id":"c39651c9.b6f5f","type":"mqtt in","z":"53c582.5dd31a8","name":"","topic":"V2","qos":"1","broker":"89cee77f.d83e78","x":150,"y":240,"wires":[["5f832ae8.939834"]]},{"id":"4ddbf2da.38324c","type":"mqtt in","z":"53c582.5dd31a8","name":"","topic":"V1","qos":"1","broker":"89cee77f.d83e78","x":150,"y":160,"wires":[["333e8100.3ef77"]]},{"id":"333e8100.3ef77","type":"change","z":"53c582.5dd31a8","name":"","rules":[{"t":"set","p":"V1","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":180,"wires":[["50af2dc8.ca4c44"]]},{"id":"5f832ae8.939834","type":"change","z":"53c582.5dd31a8","name":"","rules":[{"t":"set","p":"V2","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":220,"wires":[["50af2dc8.ca4c44"]]},{"id":"50af2dc8.ca4c44","type":"change","z":"53c582.5dd31a8","name":"sum","rules":[{"t":"set","p":"payload","pt":"msg","to":"$flowContext('V1') + $flowContext('V2')","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":200,"wires":[["dc3533b7.3c382"]]},{"id":"dc3533b7.3c382","type":"debug","z":"53c582.5dd31a8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":690,"y":200,"wires":},{"id":"89cee77f.d83e78","type":"mqtt-broker","z":"","name":"","broker":"[m11.cloudmqtt.com](http://m11.cloudmqtt.com)","port":"10808","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

---

<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:** [2 December 2018 21:27 UTC](https://discourse.nodered.org/t/how-to-do-a-simple-sum-of-2-msgs/5382/11 "2018-12-02T21:27:41Z")

</div>

What version of Node-RED you have installed in your system ?

---

<div class="post-metadata">

**Author:** ![ederpereira92](https://avatars.discourse-cdn.com/v4/letter/e/d9b06d/32.png) [@ederpereira92](https://discourse.nodered.org/u/ederpereira92)\
**Post date:** [2 December 2018 21:32 UTC](https://discourse.nodered.org/t/how-to-do-a-simple-sum-of-2-msgs/5382/12 "2018-12-02T21:32:25Z")

</div>

I`m using the v0.19.2

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [2 December 2018 21:33 UTC](https://discourse.nodered.org/t/how-to-do-a-simple-sum-of-2-msgs/5382/13 "2018-12-02T21:33:11Z")

</div>

> [@ederpereira92](#):
>
> Invalid JSONata expression: The left side of the "+" operator must evaluate to a number

I haven't followed the full discussion, but I can see from the Debug output your V1/V2 payloads are Strings not Numbers. This is why JSONata complains when you try to add them together. You can fix the Change node expression to convert the strings to numbers before adding them:

```auto
$number($flowContext('V1')) + $number($flowContext('V2'))

```

---

<div class="post-metadata">

**Author:** ![cpyandy](https://avatars.discourse-cdn.com/v4/letter/c/c6cbf5/32.png) [@cpyandy](https://discourse.nodered.org/u/cpyandy)\
**Post date:** [10 December 2018 10:12 UTC](https://discourse.nodered.org/t/how-to-do-a-simple-sum-of-2-msgs/5382/14 "2018-12-10T10:12:08Z")

</div>

I am doing something similar perhaps simpler.

I have 2 mqtt messages each contents "1" or "0", I am trying to create a function which only passes through the "1" when both input are equal to "1", pretty much an AND GATE function.

---

<div class="post-metadata">

**Author:** ![machadotiago](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/machadotiago/32/23776_2.png) [@machadotiago](https://discourse.nodered.org/u/machadotiago)\
**Post date:** [10 December 2018 11:32 UTC](https://discourse.nodered.org/t/how-to-do-a-simple-sum-of-2-msgs/5382/15 "2018-12-10T11:32:06Z")

</div>

again a function is not needed. You can simply use a "change" node for that:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/3/3badc3d52db0715c8b13bd6369cde492f00b410a.png)

Here is an example:

`[{"id":"490e81a5.72c82","type":"inject","z":"2901a55a.0bd27a","name":"","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":120,"wires":[["31c9f087.3438f"]]},{"id":"e8709086.2de81","type":"inject","z":"2901a55a.0bd27a","name":"","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":160,"wires":[["60ed0472.c5a9dc"]]},{"id":"31c9f087.3438f","type":"change","z":"2901a55a.0bd27a","name":"","rules":[{"t":"set","p":"var1","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":120,"wires":[["bac9a9fd.bbd678"]]},{"id":"bac9a9fd.bbd678","type":"change","z":"2901a55a.0bd27a","name":"sum","rules":[{"t":"set","p":"payload","pt":"msg","to":"$flowContext('var1') and $flowContext('var2')","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":530,"y":140,"wires":[["5fc858e8.750c58"]]},{"id":"5fc858e8.750c58","type":"debug","z":"2901a55a.0bd27a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","x":750,"y":140,"wires":[]},{"id":"60ed0472.c5a9dc","type":"change","z":"2901a55a.0bd27a","name":"","rules":[{"t":"set","p":"var2","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":160,"wires":[["bac9a9fd.bbd678"]]}]`

---

<div class="post-metadata">

**Author:** ![cpyandy](https://avatars.discourse-cdn.com/v4/letter/c/c6cbf5/32.png) [@cpyandy](https://discourse.nodered.org/u/cpyandy)\
**Post date:** [11 December 2018 05:50 UTC](https://discourse.nodered.org/t/how-to-do-a-simple-sum-of-2-msgs/5382/16 "2018-12-11T05:50:21Z")

</div>

Thank yo so much! This is a "Hello world" to JSONata for me.

---

<div class="post-metadata">

**Author:** ![fishman](https://avatars.discourse-cdn.com/v4/letter/f/839c29/32.png) [@fishman](https://discourse.nodered.org/u/fishman)\
**Post date:** [1 December 2019 12:34 UTC](https://discourse.nodered.org/t/how-to-do-a-simple-sum-of-2-msgs/5382/17 "2019-12-01T12:34:21Z")

</div>

Thank you. It works well for me

---

<div class="post-metadata">

**Author:** ![rockstarsmaf89](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@rockstarsmaf89](https://discourse.nodered.org/u/rockstarsmaf89)\
**Post date:** [28 July 2020 19:31 UTC](https://discourse.nodered.org/t/how-to-do-a-simple-sum-of-2-msgs/5382/18 "2020-07-28T19:31:00Z")

</div>

Hey! can you explain it clearly like why exactly did you use change and join.

---

<div class="post-metadata">

**Author:** ![machadotiago](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/machadotiago/32/23776_2.png) [@machadotiago](https://discourse.nodered.org/u/machadotiago)\
**Post date:** [28 July 2020 19:43 UTC](https://discourse.nodered.org/t/how-to-do-a-simple-sum-of-2-msgs/5382/19 "2020-07-28T19:43:44Z")

</div>

The very first important thing here is to understand that node-red is based on **events**.  
If you want to sum 2 events you have 2 options:

1. Join those 2 events with a Join node: try to play a bit with the join node and you will understand how it works
2. Store the values on a Flow variable and sum them every time a new event is triggered

Regarding why I used the "change" node before the join node, it is simply because the Join node requires the msg.parts attribute as stated in the manual of the node:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/e/ee0773508f05877533408784d160cd4927263608.png)

---

<div class="post-metadata">

**Author:** ![machadotiago](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/machadotiago/32/23776_2.png) [@machadotiago](https://discourse.nodered.org/u/machadotiago)\
**Post date:** [28 July 2020 19:56 UTC](https://discourse.nodered.org/t/how-to-do-a-simple-sum-of-2-msgs/5382/20 "2020-07-28T19:56:31Z")

</div>

Actually, I was testing this flow and there's a bug on it. If you click twice on the first button it will sum only the values of the first button. I'm afraid it is not possible to implement it with a Join node (sorry community 🥶 for the wrong answer).  
I strongly recommend the second alternative (save variables to the flow context and then sum later)

[Next page](https://discourse.nodered.org/t/how-to-do-a-simple-sum-of-2-msgs/5382.md?page=2)
