# How to do a sum of 7 values

**URL:** <https://discourse.nodered.org/t/how-to-do-a-sum-of-7-values/27673>\
**Category:** General\
**Created:** [2 June 2020 15:39 UTC](https://discourse.nodered.org/t/how-to-do-a-sum-of-7-values/27673 "2020-06-02T15:39:46Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hamza](https://avatars.discourse-cdn.com/v4/letter/h/a88e57/32.png) [@Hamza](https://discourse.nodered.org/u/Hamza)\
**Post date:** [2 June 2020 15:39 UTC](https://discourse.nodered.org/t/how-to-do-a-sum-of-7-values/27673/1 "2020-06-02T15:39:47Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/8/289005919c80b75fdd7c1eeb12d9d8d1fb040473.png)

any help will be appreciated

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [2 June 2020 15:40 UTC](https://discourse.nodered.org/t/how-to-do-a-sum-of-7-values/27673/2 "2020-06-02T15:40:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Hamza](https://avatars.discourse-cdn.com/v4/letter/h/a88e57/32.png) [@Hamza](https://discourse.nodered.org/u/Hamza)\
**Post date:** [2 June 2020 15:51 UTC](https://discourse.nodered.org/t/how-to-do-a-sum-of-7-values/27673/3 "2020-06-02T15:51:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [2 June 2020 16:05 UTC](https://discourse.nodered.org/t/how-to-do-a-sum-of-7-values/27673/4 "2020-06-02T16:05:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Hamza](https://avatars.discourse-cdn.com/v4/letter/h/a88e57/32.png) [@Hamza](https://discourse.nodered.org/u/Hamza)\
**Post date:** [2 June 2020 16:16 UTC](https://discourse.nodered.org/t/how-to-do-a-sum-of-7-values/27673/6 "2020-06-02T16:16:37Z")

</div>

![debub of join node](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/7/07d9dd666bfb862425895779c81a4d5220910eb4.png)

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [2 June 2020 16:22 UTC](https://discourse.nodered.org/t/how-to-do-a-sum-of-7-values/27673/7 "2020-06-02T16:22:39Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Hamza](https://avatars.discourse-cdn.com/v4/letter/h/a88e57/32.png) [@Hamza](https://discourse.nodered.org/u/Hamza)\
**Post date:** [2 June 2020 16:26 UTC](https://discourse.nodered.org/t/how-to-do-a-sum-of-7-values/27673/8 "2020-06-02T16:26:46Z")

</div>

Yes sir i did.

 ![join node](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/c/8c454178e66d63299eb2fa6837f17865bf707e21.png)

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [2 June 2020 16:28 UTC](https://discourse.nodered.org/t/how-to-do-a-sum-of-7-values/27673/9 "2020-06-02T16:28:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Hamza](https://avatars.discourse-cdn.com/v4/letter/h/a88e57/32.png) [@Hamza](https://discourse.nodered.org/u/Hamza)\
**Post date:** [2 June 2020 16:31 UTC](https://discourse.nodered.org/t/how-to-do-a-sum-of-7-values/27673/10 "2020-06-02T16:31:02Z")

</div>

i set it to one second and nothing changed.

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [2 June 2020 16:32 UTC](https://discourse.nodered.org/t/how-to-do-a-sum-of-7-values/27673/11 "2020-06-02T16:32:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Hamza](https://avatars.discourse-cdn.com/v4/letter/h/a88e57/32.png) [@Hamza](https://discourse.nodered.org/u/Hamza)\
**Post date:** [2 June 2020 16:34 UTC](https://discourse.nodered.org/t/how-to-do-a-sum-of-7-values/27673/12 "2020-06-02T16:34:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [2 June 2020 16:36 UTC](https://discourse.nodered.org/t/how-to-do-a-sum-of-7-values/27673/13 "2020-06-02T16:36:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Hamza](https://avatars.discourse-cdn.com/v4/letter/h/a88e57/32.png) [@Hamza](https://discourse.nodered.org/u/Hamza)\
**Post date:** [2 June 2020 16:37 UTC](https://discourse.nodered.org/t/how-to-do-a-sum-of-7-values/27673/14 "2020-06-02T16:37:10Z")

</div>

exactly sir.

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [2 June 2020 16:42 UTC](https://discourse.nodered.org/t/how-to-do-a-sum-of-7-values/27673/15 "2020-06-02T16:42:16Z")

</div>

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:

```auto
$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](https://nodered.org/docs/user-guide/context)

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [2 June 2020 16:54 UTC](https://discourse.nodered.org/t/how-to-do-a-sum-of-7-values/27673/16 "2020-06-02T16:54:09Z")

</div>

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

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

```

---

<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:** [2 June 2020 16:55 UTC](https://discourse.nodered.org/t/how-to-do-a-sum-of-7-values/27673/17 "2020-06-02T16:55:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [2 June 2020 17:00 UTC](https://discourse.nodered.org/t/how-to-do-a-sum-of-7-values/27673/18 "2020-06-02T17:00:42Z")

</div>

> [@Colin](#):
>
> Then the join node will remember the previous values of each input

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

---

<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:** [1 August 2020 17:10 UTC](https://discourse.nodered.org/t/how-to-do-a-sum-of-7-values/27673/19 "2020-08-01T17:10:57Z")

</div>

Closed on 1 August 2020 17:10 UTC

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