home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
71.61k stars 29.92k forks source link

history_stats doesn't always reset #121535

Open JonLaliberte opened 2 months ago

JonLaliberte commented 2 months ago

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:

Screenshot 2024-07-08 at 11 23 22 AM

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_
home-assistant[bot] commented 2 months ago

history_stats documentation history_stats source

bcutter commented 1 month ago

I discovered the same recently. Example: grafik

  - platform: history_stats
    name: masked
    entity_id: switch.masked
    state: "on"
    type: time
    start: "{{ now().replace(hour=0, minute=0, second=0) }}"
    end: "{{ now() }}"
    # Update 2022-10-25: "scan_interval" not working anyway
    scan_interval: 900

I roughly know when this behaviour started... grafik

...but not why.

There was no change on 2024-08-04 according to my event log. Only:

Idea number one: As @JonLaliberte mentioned

I have seen this issue happen sporadically for maybe 6 months or so now.

that might match the HA 2024.2 release assuming @JonLaliberte installed it right in Februar - just a wild guess.

Idea number two: We both use the same start: "{{ now().replace(hour=0, minute=0, second=0) }}". Maybe something in HA Core's internals has changed when operating on these?

Anyway, a dev needs to have a look at this as I (we) can only provide assumptions and indicators or wild guesses. For me currently all my history_stats sensors are useless as not reliable