core: history_stats sensor does not reset until slightly after midnight for the next day
The problem
History_stats gives a wrong value if the sensor used as the base for the measurement is “on” over midnight and you want to measure a daily value.This problem has been reported before (#72357) but this issue was closed without action.
The following sensor is used to keep track of the daily usage of a water tap:
- platform: history_stats
name: Waterfill daily
entity_id: switch.poolwater
state: "on"
type: time
start: "{{ now().replace(hour=0, minute=0, second=0) }}"
end: "{{ now() }}"
This speciIfic night switch.poolwater was open less than 5 minutes:

The history stat sensor should present 3m14s for July 27th and 22s for July 28th. But it continued to count all day ( today) even if the switch turned of 22s after midnight. In the morning it looked like this:

What version of Home Assistant Core has the issue?
2022.7.5
What was the last working version of Home Assistant Core?
No response
What type of installation are you running?
Home Assistant OS
Integration causing the issue
history_stats
Link to integration documentation on our website
No response
Diagnostics information
No response
Example YAML snippet
No response
Anything in the logs that might be useful for us?
No response
Additional information
Not sure if this problem occurs everytime the sensor is open over midnight. I’ve seen this problem before monitoring another sensor, but I thought it was corrected in a later release. See https://community.home-assistant.io/t/strange-history-statistics/433815 for additional details.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 11
- Comments: 76 (20 by maintainers)
Not fixed.
This is still an issue. Such a fundamental logic shouldn’t require multiple sensors and workarounds to work.
Following documentation should result in correct MAX value, but that is not the case and therefore this is an issue yet to be resolved
Bug still exist for me too.
I just re-tested in 2024.10.1, and it’s still not fixed. Same exact behavior as 2 years ago.
Workaround needs to be added to official documentation. Otherwise this issue will be closed and new ones will be created asking about problem. As software engineer I know - when something is not a bug but a feature and clients still complain it means that documentation is not explaining “feature” correctly.
I understand @karwosts thanks. I suppose it’s more about a bit of transparency around that - with maintainers chipping in on issues to let us know solutions need to come from the community. Otherwise we just wait, and threads like this suddenly come back to life when new people experience the issue and wonder why nothing has happened in years. I am unfortunately one of those who isn’t capable enough to actually dev any integrations but tries to contribute to the community in other ways where I can.
I understand the core maintainers can’t do everything, but I think there is a perception that core integrations = HA team responsibility, custom integrations = responsibility of the dev who built it. Many folk who discover that history stats exists (like me) then expect it to work like the docs suggest. There’s no visibility of these types of issues there.
I actually feel bad coming across a bit critical here. HA is unbelievably amazing (user of 4 years here) so I only get to this point when I feel there’s a genuine point of value to make. But of course I’m not sure any of the core HA team will actually see this! Meantime I will stick with my workaround knowing that it works, but with unnecessary additional stuff going on which I also know could be avoided.
No worries, it’s easy to get them mixed up.
History stats wouldn’t be appropriate for energy sensors, as it doesn’t sum an accumulation, it only counts the time a sensor is in a particular state.
The only real way I can think of to fix the statistics accumulation is to make sure your sensor resets to 0 before midnight. So if you have a utility meter, trigger the reset at 23:59 instead of 00:00 should help with that.
For making sure history stats resets to 0 before midnight, I would use something like:
This will sum the time your sensor is on during a day, but it will reset to 0 at 23:59:?? instead of 00:00:??, so the max won’t holdover into the next day.
@antsalm I was going to see if I can reproduce this. Just for completeness, could you include the switch.plant_desk history in the same graph?
I’m kind of curious, what happened here prior to the 18th? It seems like it reset to 0 some time before midnight? Any idea why it changed to 0 at like ~8pm? All the other days it seemed to reset at midnight.
having exactly same with mine history_stats
while the switch pc_s got turned off yesterday
This definitely still needs attention as I’m pretty sure it hasn’t been addressed. I have a workaround in place but it should just do what it says it does.