# Sum of 2 values - flow?

**URL:** <https://discourse.nodered.org/t/sum-of-2-values-flow/59114>\
**Category:** General\
**Created:** [28 February 2022 12:32 UTC](https://discourse.nodered.org/t/sum-of-2-values-flow/59114 "2022-02-28T12:32:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Herbert](https://avatars.discourse-cdn.com/v4/letter/h/ce73a5/32.png) [@Herbert](https://discourse.nodered.org/u/Herbert)\
**Post date:** [28 February 2022 12:32 UTC](https://discourse.nodered.org/t/sum-of-2-values-flow/59114/1 "2022-02-28T12:32:24Z")

</div>

Hello.

With other aswers here in the forum I solved my problem to make a sum of 2 MQTT values.  
See here.

```auto
[{"id":"d10147cb.c374a8","type":"debug","z":"5f439c73.c516f4","name":"","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":210,"y":1600,"wires":[]},{"id":"3ffb9510.e6676a","type":"debug","z":"5f439c73.c516f4","name":"","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":790,"y":1600,"wires":[]},{"id":"6a97942.8839a6c","type":"mqtt in","z":"5f439c73.c516f4","name":"","topic":"solpiplog/pip/acoutw","qos":"0","datatype":"utf8","broker":"d891a2dc.5ed99","x":150,"y":1500,"wires":[["d10147cb.c374a8","acd6cb66.c99968","62946698.9563f8"]]},{"id":"650d067a.4787f8","type":"mqtt in","z":"5f439c73.c516f4","name":"","topic":"solpiplog2/pip/acoutw","qos":"0","datatype":"utf8","broker":"d891a2dc.5ed99","x":760,"y":1500,"wires":[["3ffb9510.e6676a","ae502b60.0a2a48"]]},{"id":"276bc209.1a5b9e","type":"change","z":"5f439c73.c516f4","name":"","rules":[{"t":"set","p":"A","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1110,"y":1640,"wires":[["718d2102.205ea"]]},{"id":"718d2102.205ea","type":"change","z":"5f439c73.c516f4","name":"sum","rules":[{"t":"set","p":"payload","pt":"msg","to":"$flowContext('A')+$flowContext('B')","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1390,"y":1660,"wires":[["87c2a3a2.c114e"]]},{"id":"87c2a3a2.c114e","type":"debug","z":"5f439c73.c516f4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","statusVal":"payload","statusType":"auto","x":1570,"y":1660,"wires":[]},{"id":"b5029a86.e5c3a8","type":"change","z":"5f439c73.c516f4","name":"","rules":[{"t":"set","p":"B","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1110,"y":1680,"wires":[["718d2102.205ea"]]},{"id":"62946698.9563f8","type":"function","z":"5f439c73.c516f4","name":"string to number","func":"msg.payload = Number(msg.payload)\nmsg.topic = \"A\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":940,"y":1640,"wires":[["276bc209.1a5b9e"]]},{"id":"ae502b60.0a2a48","type":"function","z":"5f439c73.c516f4","name":"string to number","func":"msg.payload = Number(msg.payload)\nmsg.topic = \"B\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":940,"y":1680,"wires":[["b5029a86.e5c3a8"]]},{"id":"d891a2dc.5ed99","type":"mqtt-broker","name":"raspberri","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

```

The MQTT values are so included:  
Server (is clear - the Raspberry)  
Topic: solpiplog2/pip/acoutw  
QoS = 0  
Output a String

So I changed with the function from string to number.  
My question is now: what is the backgound in the function node msg.topic = A  
Then the cange node set: flow.A to msg.payload

So now my question:  
solpiplog/pip/acoutw is the topic from MQTT. In the function node there is set a "new?" msg.topic with A? But the flow is A?

I tried to make different sum from different MQTTs.But it doesn´t work. What should I change? Can somebody explain the flow, topic questions.

Thanks

---

<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:** [28 February 2022 12:47 UTC](https://discourse.nodered.org/t/sum-of-2-values-flow/59114/2 "2022-02-28T12:47:44Z")

</div>

This may make more sense to you...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/8/780810cd349ae2004981e1b9e04b858182e93717.png)

```auto
[{"id":"6a97942.8839a6c","type":"mqtt in","z":"c1ebfd3083f3904f","name":"","topic":"solpiplog/pip/acoutw","qos":"0","datatype":"utf8","broker":"d891a2dc.5ed99","nl":false,"rap":false,"inputs":0,"x":990,"y":820,"wires":[["1760e6fee795e56f"]]},{"id":"650d067a.4787f8","type":"mqtt in","z":"c1ebfd3083f3904f","name":"","topic":"solpiplog2/pip/acoutw","qos":"0","datatype":"utf8","broker":"d891a2dc.5ed99","nl":false,"rap":false,"inputs":0,"x":1000,"y":660,"wires":[["1760e6fee795e56f"]]},{"id":"87c2a3a2.c114e","type":"debug","z":"c1ebfd3083f3904f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1610,"y":720,"wires":[]},{"id":"1760e6fee795e56f","type":"join","z":"c1ebfd3083f3904f","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":1290,"y":720,"wires":[["ed8f5d46331e76ba"]]},{"id":"ed8f5d46331e76ba","type":"function","z":"c1ebfd3083f3904f","name":"Add function","func":"var a = Number(msg.payload[\"solpiplog2/pip/acoutw\"])\nvar b = Number(msg.payload[\"solpiplog/pip/acoutw\"])\nmsg.payload = a + b;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1430,"y":720,"wires":[["87c2a3a2.c114e"]]},{"id":"4343a7d7e0a674ae","type":"inject","z":"c1ebfd3083f3904f","name":"solpiplog2/pip/acoutw - timestamp","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"solpiplog2/pip/acoutw","payload":"","payloadType":"date","x":1040,"y":620,"wires":[["1760e6fee795e56f"]]},{"id":"9ccf0b53bb0e0abf","type":"inject","z":"c1ebfd3083f3904f","name":"solpiplog/pip/acoutw - timestamp","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"solpiplog/pip/acoutw","payload":"","payloadType":"date","x":1030,"y":780,"wires":[["1760e6fee795e56f"]]}]

```

When 2 streams are joined, you need something to tell them apart. In this case, the topic. In your example, you were setting the topics to `a` and `b` before joining. In my example, I simply use the topic that the MQTT sent.

Look at the function node and the join settings - see if it helps you understand

PS,

---

<div class="post-metadata">

**Author:** ![Herbert](https://avatars.discourse-cdn.com/v4/letter/h/ce73a5/32.png) [@Herbert](https://discourse.nodered.org/u/Herbert)\
**Post date:** [28 February 2022 14:59 UTC](https://discourse.nodered.org/t/sum-of-2-values-flow/59114/3 "2022-02-28T14:59:08Z")

</div>

> [@Steve-Mcl](#):
>
> When 2 streams are joined, you need something to tell them apart. In this case, the topic. In your example, you were setting the topics to `a` and `b` before joining. In my example, I simply use the topic that the MQTT sent.
> 
> Look at the function node and the join settings - see if it helps you understand
> 
> PS,

I understand.  
Thank you very much!

---

<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:** [14 March 2022 14:59 UTC](https://discourse.nodered.org/t/sum-of-2-values-flow/59114/4 "2022-03-14T14:59:54Z")

</div>

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