Get three highest avg values

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? :slight_smile:

Regards

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.

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.

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.

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.

[{"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"]]}]