core: history_stats doesn't always reset
The problem
I have some history stat’s sensors set up, like this:
- platform: history_stats
name: Pool SWG on today
entity_id: switch.pool_swg_switch
unique_id: 'pool_swg_on_today_history_stats'
state: 'on'
type: time
start: "{{ now().replace(hour=0, minute=0, second=0) }}"
end: "{{ now() }}"
Most of the time, this works great. But sometimes it gets stuck:
Here you can see that the sensor was at 125 from about 12pm to about 10am. At that time I restarted Home Assistant and it reset the sensor’s value to 0, then an automation kicked in which started it accumulating time again.
The problem is, this automation depends on this sensor to know whether or not to run - so if I had missed this, the automation would have never turned the switch on.
I have seen this issue happen sporadically for maybe 6 months or so now. To be perfectly clear, I don’t know that I would have noticed it much before that as I didn’t use this sensor type as much before.
What version of Home Assistant Core has the issue?
core-2024.7.1
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
https://www.home-assistant.io/integrations/history_stats/
Diagnostics information
No response
Example YAML snippet
- platform: history_stats
name: Pool SWG on today
entity_id: switch.pool_swg_switch
unique_id: 'pool_swg_on_today_history_stats'
state: 'on'
type: time
start: "{{ now().replace(hour=0, minute=0, second=0) }}"
end: "{{ now() }}"
### Anything in the logs that might be useful for us?
_No response_
### Additional information
_No response_
About this issue
- Original URL
- State: open
- Created 2 years ago
- Comments: 17 (5 by maintainers)
In #133770 I propose a fix for a bug that feels very similar, but I can’t tell if it’s exactly the same as your issue here. Lets see if it helps.
I’ve setup a similar entity on my dev instance I’ll monitor for a little while, with some extra debug logging, see if I can reproduce this at all. (unfortunately doesn’t have much logging by default so there’s not much to check when it doesn’t work).
Must be some subtle timing related issue but looking at the code I couldn’t obviously see anything.