# Get three highest avg values

**URL:** <https://discourse.nodered.org/t/get-three-highest-avg-values/61426>\
**Category:** General\
**Tags:** function-node\
**Created:** [18 April 2022 09:08 UTC](https://discourse.nodered.org/t/get-three-highest-avg-values/61426 "2022-04-18T09:08:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lundig](https://avatars.discourse-cdn.com/v4/letter/l/50afbb/32.png) [@lundig](https://discourse.nodered.org/u/lundig)\
**Post date:** [18 April 2022 09:08 UTC](https://discourse.nodered.org/t/get-three-highest-avg-values/61426/1 "2022-04-18T09:08:16Z")

</div>

Hello,

I have a sensor that shows the actual energy consumption. I want to calculate the avg of the three highest values of a month.

Anyone? 🙂

Regards

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [18 April 2022 11:06 UTC](https://discourse.nodered.org/t/get-three-highest-avg-values/61426/2 "2022-04-18T11:06:17Z")

</div>

Since I have an sql database I would do that by storing the readings and retrieve the value by an sql query.

But you could use persistent context storage, an array of the three highest readings seen so far.  
Or store all values - depends how many you get in a month I suppose.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [18 April 2022 12:08 UTC](https://discourse.nodered.org/t/get-three-highest-avg-values/61426/3 "2022-04-18T12:08:33Z")

</div>

As jbudd says, without a db, use persistent storage to keep an Array.

When you get a new value, add it to the end of the array (`.push`), sort the array descending and then use `.slice` to keep the first 3 entries.

Then recalculate the average using a `reduce` function.

---

<div class="post-metadata">

**Author:** ![PdeJ](https://avatars.discourse-cdn.com/v4/letter/p/c89c15/32.png) [@PdeJ](https://discourse.nodered.org/u/PdeJ)\
**Post date:** [18 April 2022 12:16 UTC](https://discourse.nodered.org/t/get-three-highest-avg-values/61426/4 "2022-04-18T12:16:42Z")

</div>

Here's another option which may be of interest. My hourly temperature sensor values gets sent in a Google Form to Google Sheets which manipulates, stores the data and represents it graphically in Google Chart for display in the Dashboard.

---

<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 April 2022 13:42 UTC](https://discourse.nodered.org/t/get-three-highest-avg-values/61426/5 "2022-04-18T13:42:12Z")

</div>

Or you can keep the 3 highest readings in flow context as they arrive, then monthly averge them and then set arry to [].  
e.g.

```auto
[{"id":"5c388fda.3a3578","type":"inject","z":"bf9e1e33.030598","name":"monthly","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":150,"y":140,"wires":[["947e0bd4.cd15e8"]]},{"id":"947e0bd4.cd15e8","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$average([$flowContext(\"monthly_avg\")])\t","tot":"jsonata"},{"t":"set","p":"monthly_avg","pt":"flow","to":"[]","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":357.33331298828125,"y":141.33334350585938,"wires":[["7058f197.2bcf"]]},{"id":"7058f197.2bcf","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":690,"y":120,"wires":[]},{"id":"3bb85251.01d13e","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"set","p":"monthly_avg","pt":"flow","to":"[$append([$flowContext(\"monthly_avg\")], $$.payload)^(>$)[[0..2]]]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":80,"wires":[["7058f197.2bcf"]]},{"id":"9695aa45.1b492","type":"inject","z":"bf9e1e33.030598","name":"temps","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"$floor($random()*5)+20","payloadType":"jsonata","x":110,"y":80,"wires":[["3bb85251.01d13e"]]}]

```

---

<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 June 2022 13:42 UTC](https://discourse.nodered.org/t/get-three-highest-avg-values/61426/6 "2022-06-17T13:42:20Z")

</div>

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