# Creating array and then average

**URL:** https://discourse.nodered.org/t/creating-array-and-then-average/45817
**Category:** General
**Created:** [18 May 2021 07:51 UTC](https://discourse.nodered.org/t/creating-array-and-then-average/45817 "2021-05-18T07:51:25Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![uno386](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@uno386](https://discourse.nodered.org/u/uno386)
#### Post date: [18 May 2021 07:51 UTC](https://discourse.nodered.org/t/creating-array-and-then-average/45817/1 "2021-05-18T07:51:25Z")

</div>

Hi everyone,  
I am getting msg payload 4 times per day. So i want to store msg value in array of 4 elements.  
Then, I want to calculate average of these 4 Elementes. But I am stuck in between counter(to count which number of payload) and msg payload value.  
How to do better way?

Thank you  
kind regards,  
Gautam

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [18 May 2021 08:00 UTC](https://discourse.nodered.org/t/creating-array-and-then-average/45817/2 "2021-05-18T08:00:25Z")

</div>

Hi and welcome.  
Not sure of better way, but you could hold the last 4 values in context and at certain time of day read context and average.  
e.g.

```auto
[{"id":"262a06c7.d1a87a","type":"inject","z":"c74669a0.6a34f8","name":"at set time","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":150,"y":880,"wires":[["65f10022.76a3c8"]]},{"id":"65f10022.76a3c8","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$average($flowContext(\"hold\"))","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":880,"wires":[["7e06057b.d1b80c"]]},{"id":"7e06057b.d1b80c","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":570,"y":840,"wires":[]},{"id":"7f3522a3.fa06ac","type":"inject","z":"c74669a0.6a34f8","name":"incoming readins","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"$ceil($random()*5)","payloadType":"jsonata","x":180,"y":800,"wires":[["c99a39e2.503808"]]},{"id":"c99a39e2.503808","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"hold","pt":"flow","to":"$append([payload],$flowContext(\"hold\"))[[0..3]]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":290,"y":800,"wires":[[]]}]

```

---

<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: [17 July 2021 08:01 UTC](https://discourse.nodered.org/t/creating-array-and-then-average/45817/3 "2021-07-17T08:01:14Z")

</div>

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