Please could someone guide me as I have spent hours looking for solutions to this problem.
I have a home battery that I charge overnight to a set level, depending on the time of year.
This level needs to increase as we approach winter and i was looking to check check the low points each day as a guide to whether it should be increased.
I’m specifically interested in 06:00-10:00 each day and would ideally have a sensor populated with a single daily statistical value for this time window.
The statistics options doesn’t seem to offer any time filters and I can’t see a template option to calculate against a time window either.
Could someone offer a solution to this?
What is the exact statistic you’re looking to measure?
Perhaps this will help: History Stats - Home Assistant.
I’m looking for the low point in the battery charge state during this period:
sensor.battery_state_of_charge
In that case, there are other options, but it gets tricky (and I could be missing another trick here to do something simpler).
Option 1: Make an input_number
. Set the value of this helper with an automation. The automation should trigger on your battery sensor’s state changes and have a condition to ensure that it only runs between your desired times. This is relatively easy. When you set the helper you take the min
between the current value and new value. This is just some templating. You’ll need to set a state class of measurement for the helper to record long-term stats.
You will need a second automation that runs once before the start of your measurement period to set an initial value for the helper for a given day (I’d run this at midnight). The initial value should be your max value for the sensor (i.e. 100%). If you don’t, you’ll be comparing with the previous day’s minimum and you’ll effectively be calculation an overall minimum, which is not desired.
Option 2: Make a trigger-based template sensor. The trigger will be the battery sensor. You can’t have a condition
block in these sensors like in an automation, but because it’s template-based, you can do the check in the template itself: Get now()
and check the bounds, and if within bounds, take the min
between the current and new value. Do do this, you need to realise that you have a this
state object that will have the previous minimum (this.state
). You’ll need extra checks to output 100% if it’s before the start of your period and to output the last (current minimum, using this.state
) value when after. In this case too, set the state class.
In both cases, the last value of the sensor per day will be the minimum for the day. You can plot this with a stats card using stats type min
. For other use cases, you’ll probably need to write a SQL query to retrieve the values.
You could also make a SQL sensor that reads data for your sensor only between those times (the WHERE
clause) and then take the MIN
. This is probably the easiest way to go.
Thanks all, I’ll look into the options above.
I do think the SQL sensor looks the simplest to get the result I need but I’ll look at it all to improve my learning.