# Hello all , I want your help in adding two consecutive msg.payload array\[\] and then finding running average for latest value + stored value

**URL:** <https://discourse.nodered.org/t/hello-all-i-want-your-help-in-adding-two-consecutive-msg-payload-array-and-then-finding-running-average-for-latest-value-stored-value/58513>\
**Category:** General\
**Created:** [17 February 2022 07:36 UTC](https://discourse.nodered.org/t/hello-all-i-want-your-help-in-adding-two-consecutive-msg-payload-array-and-then-finding-running-average-for-latest-value-stored-value/58513 "2022-02-17T07:36:23Z")\
**Posts on this page:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)\
**Post date:** [18 February 2022 09:23 UTC](https://discourse.nodered.org/t/hello-all-i-want-your-help-in-adding-two-consecutive-msg-payload-array-and-then-finding-running-average-for-latest-value-stored-value/58513/21 "2022-02-18T09:23:35Z")

</div>

can you send some data examples in array form to do some testing?

---

<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:** [18 February 2022 09:44 UTC](https://discourse.nodered.org/t/hello-all-i-want-your-help-in-adding-two-consecutive-msg-payload-array-and-then-finding-running-average-for-latest-value-stored-value/58513/22 "2022-02-18T09:44:04Z")

</div>

Something like this maybe?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/7/7726d2d100275fbf76b6c978db2d0cb50ab9b32b.png)

```auto
[{"id":"8f3d969699e2a1fe","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[10,20,30,40,50]","payloadType":"json","x":160,"y":2720,"wires":[["244402f5969de187"]]},{"id":"e693c12579532f86","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[60,70,80,90,100]","payloadType":"json","x":160,"y":2760,"wires":[["244402f5969de187"]]},{"id":"77fc9874c0e3b2d2","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[20,40,60,80,100]","payloadType":"json","x":160,"y":2800,"wires":[["244402f5969de187"]]},{"id":"244402f5969de187","type":"function","z":"bdd7be38.d3b55","name":"Running average","func":"// Get current running average, initialise to current values\nlet running = context.get(\"running\") || msg.payload\nfor (let i=0; i<running.length; i++) {\n if (typeof msg.payload[i] === \"undefined\") {\n running[i] = undefined\n } else if (typeof running[i] === \"undefined\") {\n running[i] = msg.payload[i]\n } else {\n running[i] = (running[i] + msg.payload[i]) / 2\n }\n}\ncontext.set(\"running\", running)\nmsg.payload = running\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":470,"y":2760,"wires":[["eaf44ccd924ab573"]]},{"id":"eaf44ccd924ab573","type":"debug","z":"bdd7be38.d3b55","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":650,"y":2760,"wires":[]},{"id":"5bd16e617b7fbb64","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[10,20,30,40,50]","payloadType":"json","x":120,"y":2860,"wires":[["5adcee3fa90cfdf6"]]},{"id":"5adcee3fa90cfdf6","type":"function","z":"bdd7be38.d3b55","name":"Set first two undefined","func":"msg.payload[0] = undefined\nmsg.payload[1] = undefined\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":2860,"wires":[["244402f5969de187"]]}]

```

---

<div class="post-metadata">

**Author:** ![developedbymayur](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/developedbymayur/32/56701_2.png) [@developedbymayur](https://discourse.nodered.org/u/developedbymayur)\
**Post date:** [18 February 2022 09:50 UTC](https://discourse.nodered.org/t/hello-all-i-want-your-help-in-adding-two-consecutive-msg-payload-array-and-then-finding-running-average-for-latest-value-stored-value/58513/23 "2022-02-18T09:50:10Z")

</div>

[undefined , undefine 74 ,137.5, -876 ,400]

---

<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:** [18 February 2022 09:54 UTC](https://discourse.nodered.org/t/hello-all-i-want-your-help-in-adding-two-consecutive-msg-payload-array-and-then-finding-running-average-for-latest-value-stored-value/58513/24 "2022-02-18T09:54:01Z")

</div>

Or use this for the function if you want to be able to adjust the smoothing factor

```auto
// set this to 0.5 to get running average of new an previous average
// set it to less than 0.5 (min 0) to get greater smoothing (slower response)
// set it to greater than 0.5 (max 1) to get quicker response
const factor = 0.5
// Get current running average, initialise to current values
let running = context.get("running") || msg.payload
for (let i=0; i<running.length; i++) {
    if (typeof msg.payload[i] === "undefined") {
        running[i] = undefined
    } else if (typeof running[i] === "undefined") {
        running[i] = msg.payload[i]
    } else {
        running[i] = (running[i]*(1-factor) + msg.payload[i]*factor)
    }
}
context.set("running", running)
msg.payload = running
return msg;

```

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [18 February 2022 16:48 UTC](https://discourse.nodered.org/t/hello-all-i-want-your-help-in-adding-two-consecutive-msg-payload-array-and-then-finding-running-average-for-latest-value-stored-value/58513/25 "2022-02-18T16:48:18Z")

</div>

> [@Colin](#):
>
> `running[i] = (running[i]*(1-factor) + msg.payload[i]*factor)`

That's a very handy snippet @Colin ....I'll bookmark that for later 🙂

---

<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:** [18 February 2022 16:52 UTC](https://discourse.nodered.org/t/hello-all-i-want-your-help-in-adding-two-consecutive-msg-payload-array-and-then-finding-running-average-for-latest-value-stored-value/58513/26 "2022-02-18T16:52:16Z")

</div>

It gives a close approximation to an RC filter, so a step input gives and exponential curve up to the new value. The Time Constant depends on the factor and on the time interval between the samples.

---

<div class="post-metadata">

**Author:** ![developedbymayur](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/developedbymayur/32/56701_2.png) [@developedbymayur](https://discourse.nodered.org/u/developedbymayur)\
**Post date:** [22 February 2022 04:37 UTC](https://discourse.nodered.org/t/hello-all-i-want-your-help-in-adding-two-consecutive-msg-payload-array-and-then-finding-running-average-for-latest-value-stored-value/58513/27 "2022-02-22T04:37:22Z")

</div>

Hello all thank you for your valuable time

---

<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:** [23 April 2022 04:37 UTC](https://discourse.nodered.org/t/hello-all-i-want-your-help-in-adding-two-consecutive-msg-payload-array-and-then-finding-running-average-for-latest-value-stored-value/58513/28 "2022-04-23T04:37:25Z")

</div>

Closed on 23 April 2022 04:37 UTC

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

[Previous page](https://discourse.nodered.org/t/hello-all-i-want-your-help-in-adding-two-consecutive-msg-payload-array-and-then-finding-running-average-for-latest-value-stored-value/58513.md?page=1)
