# Sum of different function nodes

**URL:** <https://discourse.nodered.org/t/sum-of-different-function-nodes/32995>\
**Category:** General\
**Created:** [17 September 2020 12:41 UTC](https://discourse.nodered.org/t/sum-of-different-function-nodes/32995 "2020-09-17T12:41:01Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![usmanEmder](https://avatars.discourse-cdn.com/v4/letter/u/977dab/32.png) [@usmanEmder](https://discourse.nodered.org/u/usmanEmder)\
**Post date:** [17 September 2020 12:41 UTC](https://discourse.nodered.org/t/sum-of-different-function-nodes/32995/1 "2020-09-17T12:41:02Z")

</div>

I have 8 input nodes and each node is connected with function nodes, where I am doing some multiplication and then showed them in separate graphs. Now, I need to sum up all payloads from function nodes and make a sum up a graph. But all 8 payloads have the same title. How can I make a function node, to sum up, all of them?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [17 September 2020 19:28 UTC](https://discourse.nodered.org/t/sum-of-different-function-nodes/32995/2 "2020-09-17T19:28:47Z")

</div>

You first need to think about the order and/or spread in which messages from each input arrive and how you want to handle that.

For example, do you simply want to create an updated total when _any_ of the inputs change value? Or do you need to wait for all of them to change value?

The simplest option is to save each input as it comes in to its own flow variable (or to a given position using a object or array flow variable). As any value changes, you retrieve all of the others and create a new sum value. You may need to watch out for race conditions though where two inputs happen so close together that they interfere with each other. So you might want to only do the new sum as the msg reaches the output which should help prevent that.

If you need to wait for all 8 new inputs to come in before you change the output value, that is more complex.

If you only need the sum value for a chart though, you could do the processing in the front-end but then you will certainly have to make a change to the data so that you can differentiate between the inputs.

---

<div class="post-metadata">

**Author:** ![usmanEmder](https://avatars.discourse-cdn.com/v4/letter/u/977dab/32.png) [@usmanEmder](https://discourse.nodered.org/u/usmanEmder)\
**Post date:** [19 September 2020 08:34 UTC](https://discourse.nodered.org/t/sum-of-different-function-nodes/32995/3 "2020-09-19T08:34:01Z")

</div>

Thanks for your kind reply. Actually, in my case, a simple sum of values while any change in inputs values will also change the total sum value. For more detail, I also attached the picture.

 ![pic](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/5/35617101645abb4bc1a02d4da9b84c26e23f517b.jpeg)

---

<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:** [19 September 2020 08:50 UTC](https://discourse.nodered.org/t/sum-of-different-function-nodes/32995/4 "2020-09-19T08:50:37Z")

</div>

Give each one a different topic (if they have not got that already) then you can feed them all into a Join node configured to Manual, to generate Key/Value pairs, After 8 inputs and Every input thereafter. Then you will get a message containing all 8 values each time any one provides a new value, and you can add them up in a function node.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [19 September 2020 10:49 UTC](https://discourse.nodered.org/t/sum-of-different-function-nodes/32995/5 "2020-09-19T10:49:45Z")

</div>

In the change node after the input, save the value to a flow variable. Then, in your sum node, retrieve all of the variables and do the sum.

Personally, if I were using uibuilder, I would do the sum in the front-end code. I would also, as Colin suggests, change the input topic which would make it easy to pass the data through to the front-end & I could even code the front-end so that any new lights I added would automatically appear in the UI. This _is_ possible in Dashboard but would require the use of a ui\_template node.

---

<div class="post-metadata">

**Author:** ![usmanEmder](https://avatars.discourse-cdn.com/v4/letter/u/977dab/32.png) [@usmanEmder](https://discourse.nodered.org/u/usmanEmder)\
**Post date:** [19 September 2020 11:01 UTC](https://discourse.nodered.org/t/sum-of-different-function-nodes/32995/6 "2020-09-19T11:01:30Z")

</div>

Thanks for @Colin and @TotallyInformation for your kind and fruitful information and your precious time. Actually, I simply add the payload 8 times (as shown in the attachment) and attached with the graph and it is working fine. thanks again.

 ![solution](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/4/d46da0ca62f0e014ca55b47d222e1acae1e6b864.jpeg)

---

<div class="post-metadata">

**Author:** ![Hypnos](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hypnos/32/7566_2.png) [@Hypnos](https://discourse.nodered.org/u/Hypnos)\
**Post date:** [19 September 2020 11:10 UTC](https://discourse.nodered.org/t/sum-of-different-function-nodes/32995/7 "2020-09-19T11:10:38Z")

</div>

This is not the sum of the individual message payloads. It is the multiplication of the last message Payload and can be simplified to:

```auto
msg.payload = msg.payload * 8;
return msg;

```

For the sum needs to be something like that (is pseudo code):

```auto
let data = context.get("data") || {} ;
data[msg.topic] = msg.payload || 0;

context.set("data",data);

msg.payload = 0;

for (var prop in data) {
   msg.payload += data[prop];
}
return msg;

```

Then every separate message needs a separate topic.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [19 September 2020 11:18 UTC](https://discourse.nodered.org/t/sum-of-different-function-nodes/32995/8 "2020-09-19T11:18:06Z")

</div>

As @Hypnos says, you are not doing what you think. Please follow my guide and you will get what you want.

Because of the way that Node-RED works, you can never know when specific inputs will arrive. That is why you need to save them temporarily. Then, when _any_ of the inputs trigger your sum function, you get _all_ of the saved inputs and sum them.

---

<div class="post-metadata">

**Author:** ![usmanEmder](https://avatars.discourse-cdn.com/v4/letter/u/977dab/32.png) [@usmanEmder](https://discourse.nodered.org/u/usmanEmder)\
**Post date:** [19 September 2020 11:34 UTC](https://discourse.nodered.org/t/sum-of-different-function-nodes/32995/9 "2020-09-19T11:34:51Z")

</div>

Ok, I will follow your instructions @TotallyInformation

---

<div class="post-metadata">

**Author:** ![usmanEmder](https://avatars.discourse-cdn.com/v4/letter/u/977dab/32.png) [@usmanEmder](https://discourse.nodered.org/u/usmanEmder)\
**Post date:** [19 September 2020 12:42 UTC](https://discourse.nodered.org/t/sum-of-different-function-nodes/32995/10 "2020-09-19T12:42:17Z")

</div>

> [@Hypnos](#):
>
> pseudo code

What changes are necessary in your example (pseudo code), to sum up for 8 inputs. one as I got from your message, I have to assign the topic for each input. What else will be required? And Where I need to assign the topic. While as you can see in above picture 8 function nodes and connected with one sum function node.

---

<div class="post-metadata">

**Author:** ![Hypnos](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hypnos/32/7566_2.png) [@Hypnos](https://discourse.nodered.org/u/Hypnos)\
**Post date:** [19 September 2020 23:32 UTC](https://discourse.nodered.org/t/sum-of-different-function-nodes/32995/11 "2020-09-19T23:32:06Z")

</div>

you need to give every message a different topic. This can be done by setting the topic in the "global.1" inject nodes on the beginning. The topic can be simply a number 1-8 (different for every inject).

Of course, the function node must not remove the topic from the message.

My code (I haven't testet is, so maybe wrong) must be go into the Sum function node. What it does is to store every incomming payload under the topic in the context of the node (can be seen in the context sidebar) and creates the sum of all previously saved values.

This is what @TotallyInformation means with "save them temporarily" . More information can be found under [https://nodered.org/docs/user-guide/writing-functions#storing-data](https://nodered.org/docs/user-guide/writing-functions#storing-data).

---

<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:** [3 October 2020 23:32 UTC](https://discourse.nodered.org/t/sum-of-different-function-nodes/32995/12 "2020-10-03T23:32:30Z")

</div>

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