How to do a sum of 7 values

Hello guys,
I want to display a sum of 7 values on a blynk labeled value widget but it didn't work as intended because i see the old values plus the new one start to accumulate and sometimes after a modification a zero, here is my flow:

[{"id":"e1b07ecd.4ceac","type":"change","z":"58d0be7c.c1212","name":"$sum(payload)","rules":[{"t":"set","p":"payload","pt":"msg","to":"$sum(payload)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1060,"y":440,"wires":[["3e2ddd85.abc712","bc29f104.108d1"]]}]

problem

any help will be appreciated

First determine the output of the join node (ie debug node)

Thank you for your quick respond but i didn't get it what should i do to send the sum of all 7 at one time and keep updating it regularly.

Please add a debug node on the join node output and send a screenshot of the data so we know what kind of data comes out.

You have only shared the change node and not the whole flow. Have you tried setting the count of message parts to 7 (with a suitable timeout) in manual mode?

Yes sir i did.

And the timeout is not too short? It looks like the node outputs a message before all 7 messages have arrived?

i set it to one second and nothing changed.

Where do the values come and who/what sends them? Should all the values come within 1 second?

the values are sent continuously by sliders and buttons linked to virtual pins on blynk.

Ah, so your goal is to get the sum of all the current slider positions whenever one of them is moved, correct?

exactly sir.

Then I believe join node won't be the way to solve this. You'll need to store each of the slider value in the flow context. Add a change node to each slider output and set a flow context variable separately for each. So first slider would be stored to flow.slider1 etc.

When you have all the previous values stored in the context, you can use a change node expression to sum flow.slider1 + flow.slider2 + ... or so the same in a function node.

The sum expression might also work like:

$sum([flow.slider1, flow.slider2, ...])

...although I'm not sure. I haven't used JSONata all that much myself.

More info: https://nodered.org/docs/user-guide/context

Correction, I tried this myself and the syntax for the JSONata expression should be for example:

$sum([$flowContext('slider1'), $flowContext('slider2'), $flowContext('slider3')])

You can do it with a join node set to key/value pairs, wait for 7 to be received, then send value on every new message. Then the join node will remember the previous values of each input. You need a separate topic on each input of course. No need for flow variables.

1 Like

Thanks, I hadn't realized this. Might come in handy!

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