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") || {} ;
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.

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

Ok, I will follow your instructions @TotallyInformation

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.

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.

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