History stats do not reset at midnight but require an additional event to reset

Hi there,

Update: maybe related to history_stats sensor does not reset until slightly after midnight for the next day · Issue #75903 · home-assistant/core · GitHub but not sure since too many variants of strange behavior are reported.

I have a binary sensor which tells me if a door is open or closed.

I created a statistics sensor to count how long the door is open per day:

- platform: history_stats
  name: some door
  entity_id: binary_sensor.some_door
  state: "on"
  type: time
  start: "{{ today_at() }}"
  end: "{{ now() }}"

The start/end values have been taken from the first example given in the docs: History Stats - Home Assistant

Today I recognized, that the time for today was not reset, but still showed me the duration from yesterday. After the door was opened and closed for 11s today, the duration for today fell back to almost zero (which is actually correct). However, i expected the drop to 0 at midnight and not after the first event today.

So for me it seems, the statistics (or my config?) required a state change today in order to update the statistics for today. Is this the expected behavior for the history stats? If so, it is very confusing/unexpected to me.

here is the graph from yesterday/today:

Dan

Looks like the history stats sensor only updates when the entity changes.

It should render upon state changes of the source sensor and every minute. Clearly yours wasn’t updating every minute, but I’m interested to know if that was a one-time error or if that happens every time?

1 Like

This was the first time I observed that since the introduction of that sensor which was about 10 days ago.

I will check my graph if this case already occured in the past. Will be Sunday since I am on the road today.

so I checked this sensor if there are any weird state changes in the past. It seems all fine, except for the single instance I highlighted in the initial post:

@mekaneck it happened again…

any ideas?

Besides the “not resetting at midnight” problem, is your sensor even climbing at a 1:1 slope when the source is “on”? It appears to be jumping to some value just after 16:00 on your graph.

The way you’ve configured your history_stats sensor (using “time” and a window that starts at midnight each day), the graph should only be constructed with 3 distinct segments:

  1. A constant rising slope of 0.17 hours (i.e. 1 minute) per 1 minute of time on the x-axis, when the source sensor is “on”
  2. A flat horizontal line, when the source sensor is “off”
  3. A vertical line from some value down to zero, at midnight when it resents.

Can you also show the history of the source sensor when you take images?

Here’s what I would expect to see. This is for my HVAC, showing when it is cooling and when the fan is “on”. I’m also showing the state of the uptime sensor (“uptime” is an integration that can be added to give you a timestamp of when HA started). This is useful to see if any of the issues correspond to HA restarts.

Thanks for your reply!

I tried my best:

Let me know, if there are details missing. Unfortunately, my uptime sensor has only 2 days of history … did not notice that before. It should have more, since I added it weeks ago…

The zoomed part shows the source sensor for today. It rises in steps, while the door is open. Exactly as you wrote. However, sometimes the source sensor jumps and if this happens it does not reset at midnight. Maybe an indication for something?

Something I observed:

If the sensor is on, it rises with more than 0,17h per 1 min. So it seems the polling frequency is higher than that?