Statistics Max Bar Chart Aggregationg

I have a sensor that outputs a total runtime per day for my AC. I want to use the statistics graph to show the max runtime for each day but it seems like there is an issue when I bin the bar graph by day. It can be clearly seen when also creating the same chart bin’d by hour. Seems like the current day hold the previous days high until it reaches higher.

Has anyone experienced this?

How’s your sensor set up? What’s your config?

Both of the charts shown use the same sensor. I set them up using the UI. The only thing different is the radio button on one is selected using day and the other is using hour.

Seems like maybe the graph is pulling data from the previous day maybe the last datapoint or something and since that is the max it uses that until the current day goes higher. But not sure why it would do that, my timezones are setup correctly and the sensor seems to be measuring okay, the first point of each day is ‘0’ just after midnight.

The sensor config looks like this:

- platform: history_stats
  name: Downstairs HVAC Runtime Today Cool
  entity_id: sensor.downstairs_hvac_action
  state: "cooling"
  type: time
  start: "{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}"
  end: "{{ now() }}"

The sensor is outputting this from the history tab, which looks right to me:

This is what I was looking for.

Your issue might be this: history_stats doesn't always reset · Issue #121535 · home-assistant/core · GitHub.

I think the problem is that at midnight the value of the sensor is still the accumulation of the previous day, and it takes a few milliseconds after midnight for it to be re-evaluated to reset to 0. So that value gets included in the statistic “max”, since the value of the sensor was technically still that value during the daily period.

Maybe what to try is get the history_stats to reset to 0 just before midnight, then the value at midnight will be 0?

Maybe could try this?

start: "{{ today_at() if (now() < today_at("23:59")) else now() }}"
end: "{{ now() }}"

Not sure if 1 minute is enough for history_stats to update, or if it needs to be longer, or if something could be done with the update_entity service to guarantee it.

You might be right, although I would think that the statistics chart would be smarter to account for this edge case. I feel like it must be relatively common.

I will try your code suggestion.