History Statistic Sensor: Max value is same as previous day (if today is lower than yesterday)

Hi there!, I use a history_stats template to track my studying time when the study mode boolean is turned on.

Since this worked pretty well, I wanted to have theses statistics as a graph to see my daily study time on my Dashboard. I pick “max” so I get the max value of the day - which should be the value at the end of the day. This does work, but since it resets at 00:00:00, the highest value of yesterday stays for today, if the value of today is lower than yesterdays.

I tried moving the Time of the template up and down, but it does not work anymore. Moving it to 1 AM has still the problem and moving it down to 23:59:00 to reset, doesn’t count my study time of today at all. The only workaround for me is to set the graph to show hours instead. It’s not the same but at least it looks kinda correct. Daily hours would be nicer tho.

What am I doing wrong? Is there any way to fix this?

This is from my configuration.yaml

  - platform: history_stats
    name: Lernzeit Heute
    entity_id: input_boolean.lernen
    state: "on"
    type: time
    start: "{{ now().replace(hour=23, minute=59, second=0) }}"
    end: "{{ now() }}"

This is the graph I use:

chart_type: bar
period: hour
days_to_show: 30
type: statistics-graph
entities:
  - sensor.lernzeit_heute
stat_types:
  - max
style: |
  ha-card {
    background-color: transparent;
    box-shadow: none
  }

If I change the Card back to “day” and the template time to 0:00:00 you can see, that each day with a high value is followed by the same value (because on the next day I have studied less and the “max value” of the day is the one at 00:00:00)

Use the mini-graph card or Apexcharts. They both allow you to group by day and pick the max or last reading for the day.

1 Like

Thanks a lot, This works pretty well - I didn’t know the last reading works with mini-graph.

One more follow-up question: While my sensor data gets deleted after 7 Days, I can still access older values with the statistics-graph card. (See screnshots above).
The mini-graph card can only show me these 7 days. What is the reason behind this?
Thanks a lot! :slight_smile:

The history sensor uses the state history from the recorder database. This is deleted once it is older than your recorder purge_keep_days setting (10 days by default).

LTS (long term statistics) are not purged. They don’t take up much room in the recorder database.

There is known bug for this issue - see the bug and my workaround: history_stats sensor does not reset until slightly after midnight for the next day · Issue #75903 · home-assistant/core · GitHub

That appears to unrelated to this two year old solved topic.

@tom_l actually this is exactly what is happening when today’s value is lower than yesterday’s (or current hour value is lower than previous hour value) - values stored in statistics table are wrong so long term statistic is wrong. Short term data works fine as far as I know.

The reason why I posted it here is that this post come up when I was searching for this long term statistic bug.

I only skimmed it which is why I said:

i.e. “maybe”.

The important thing is that it is two years old and solved already. Please don’t necropost. Home assistant development moves very quickly and old posts become irrelevant or just plain wrong.

Sorry to refresh old page, but @tom_l it looks that issue still exists,
Are they any plan to fix it in future?

@CendaL have you find any other solution for this problem?

https://community.home-assistant.io/t/how-to-help-us-help-you-or-how-to-ask-a-good-question/114371#onesix-should-i-tag-people-22