I’m trying to have an entity that holds the maximum temperature from the past 7 or so days to use in my irrigation process. The thought is to ultimately increase watering duration/events during extreme temperatures.
I’m testing on Home Assistant 2022.5.5. Initially I have created an SQL sensor that runs once a day and the query returns the maximum value from the outside temerature entity for the previous X hours. I am running the MariaDB addon with recorder setup for that database. I disabled polling for the sensor and created an automation that updates the entity once per day. That said, I found that this sensor/ integration isn’t reliable and frequently, but not always, returns an error on execution (MySQLdb._exceptions.OperationalError) (2006, ‘MySQL server has gone away’). No other DB errors are logged or occurred before creating that sensor.
Rather than going down the rabbit hole of troubleshooting the error (the SQL integration in UI is fairly new) I’ve decided to ask the community if there is a better way.
Has anyone solved this specific problem before? Thanks for any suggestions.
You can use a statistics sensor like Stephan is showing above, probably the easiest way. That being said you should be aware that statistics sensors reset at restart. It will either show you the maximum value since 7 days ago OR since the last restart of HA, whichever datetime is later.
Wanted to point that out to you since I assume that’s different then how your SQL sensor works. It’s unlikely your SQL query is affected by HA restart.
If this bothers you I’m not sure there’s a good alternative to an SQL sensor. You might just have to turn down the log level for that component.
I guess I didn’t provide enough details, for the sensor I’m trying to create, I’d like to find the average of the maximum temperature for 4-5 days. In other words if it’s over 100 degree for more than 4-5 days in a row, then change the irrigation schedule. That’s why I wanted one value per day.
For the SQL sensor, it’s not just that it logs an error, it doesn’t update when the error occurs, which makes it pretty useless in my case. I’ve tried changing the time it executes as well as lowering the count of the dataset the query runs against but it still randomly errors.
Wild thought, how about an automation that updates an input number only if the current sensor value is greater than the current input number value, then use the input number value as the trigger for your irrigation automation if it’s above 100 for x amount of time in your case 4-5 days, then reset the input number?
I do something similar to what coolie101 suggested above.
I use the hass-variables custom integration but I think you could do the same thing using an input number except I also record the time as an attribute that the input_number can’t do.
then I save that data every day to another variable so I have a daily history.
Input number wouldnt work. If the value hadnt increased in 5 days then you’d have a problem. The value is no longer the max in the past 5 days since it’s older then that. And you have no idea what the next highest value is since you aren’t tracking that.
What finity showed works though since you still have the history. Although come to think of it you can also just do that with a single trigger template sensor since those restore state now.
Try the ha-average integration. You can specify a date range like the last 7 days as you want and will provide an average, min and max. I do a similar thing in my own project to get a five day moving average temperature for a similar purpose. See here for the implementation.
Problem?, isn’t the requirement the highest value over that period of time?, so if it’s 20 on Mon, 25 on Tues, and 15 the rest of the week, then you’ll still have the highest value?
Right, and then? What happens when you hit the following Monday? If you had a sensor which was “highest value in the past 5 days” and your readings were this:
6/1 - 20
6/2 - 25
6/3 - 15
6/4 - 16
6/5 - 15
6/6 - 14
6/7 - 15
6/8 - 14
Then the input number approach breaks on 6/8. By those readings the sensor should have value 20 on 6/1, 25 on 6/2 - 6/7 and then 16 on 6/8. Because on 6/8 the 25 reading is more then 5 days old so it is no longer “the highest value in the past 5 days”. The highest reading in the past 5 days on 6/8 is 16 which occurred on 6/4.
See the problem here? If you only track the highest value it only works if the value keeps going up. As soon as the value peaks for 5+ days that approach breaks.
Thanks for all the input everyone. I’m still pondering the best path forward. I’ve seen the solution from @finity and it seems to be closest to my needs out of the box.
There are probably other good solutions suggested but I found the statistics sensor did what I needed. I marked @stevemann as the solution as the second example he posted is effectively what I needed. Adjust the sampling_size and max_age appropriately. Keep in mind sampling_size needs to be larger than the daily number of updates from the original entity_id.