# Sum of different function nodes

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?

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.

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.

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.

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.

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.

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

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

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

``````let data = context.get("data") || {} ;

context.set("data",data);

for (var prop in data) {
}
return msg;
``````

Then every separate message needs a separate topic.

2 Likes

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.

1 Like