I am trying to get the three highest readings from one energy sensor into three new sensors. The readings must be from the last month. I’ve tried with the statistics sensor, but I can only create a sensor with the highest max value. I also believe that the data is from the last 30 days, not monthly.
Do you store a month off data in your recorder database?
The default is only 10 days. And the stats sensor uses this database for its calculations.
If you do store that much, then one way might be to use three SQL sensors to query the recorder database to get the last months data, sort it by value then pick the 1st, 2nd and 3rd values in each sensor.
I have no idea what the syntax of the query would be. Maybe an SQL wizard here on the forum can help.
Thanks for reply.
I think the recorder setting was default. Tried this in configuration.yaml:
recorder:
purge_keep_days: 30
Hope this works.
Will this slow down home assistant and how much storage space do I need? HA runs on a raspberry pi 3 with around 30 GB of free space.
I will look into SQL-sensors.
Your other alternative is to record them going forwards with an automation. When you say “from the last month” do you mean “in the last 30 days” or “during the previous calendar month to the current one”?
So set up three input_number helpers with initial value zero and this automation should handle things, I think. Not tested in full but individual bits seem to work.
It should populate the helpers in the first three hours, and then start doing so selectively when new higher values come in. Resets on the first of the month.
Probably looks very useless, but this is how the electricity bill is calculated in my country. If the average of 3 peaks every hour in a month is greater than 2/5/10/15/20 etc. kwh, the electricity bill will increase.
Oh hang on. Does your sensor continually increase to the end of each hour and then reset to zero? If it does, this might need rethinking as a new peak value will wipe out the second and third peaks on the way up.
This might work as a fix: it triggers when the accumulated sensor drops to zero and uses the value it had immediately beforehand in the comparison:
Yes, the sensor increases until the end of each hour. Unfortunately, the value is slightly higher when the next hour starts, if consumption is high. So I tried using timepattern. This seems to work great. Have also tested the reset function.
Many thanks for the help. Here is the working automation in case anyone want something similar:
Thanks for this! My energy-company will now charge us for the averange value of the 3 highest power-tops each month. So I guess I just copy your code, and then make a template sensor that adds the values of the 3 input-numbers and the divide it by 3?
Actually, my company counts the 3 days with the highest power-tops. This code take the 3 highest per day, right? Is there anyway to change the code so that it sort it one input-number per day so to say?