New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
history_stats sensor does not reset until slightly after midnight for the next day #75903
Comments
history_stats documentation |
I encountered the same problem. In the chart below, note how the sensor begins to accumulate time immediately at 00:00 on Aug 2. I only noticed the anomaly mid-afternoon the following day (the AC had definitely not been running non-stop for 15 hours). I restarted Home Assistant to clear the problem. |
Today my two history_stats values are back to normal. So far, for me it's been a one-time thing; the value was zero for 8 days, then when it started accumulating again it started with a seemingly random value rather than starting with zero. I say random because this was not a previous value. In both cases the values were out of what would be a normal range. |
I have the exact same issue since updating to the newest versions HA Core. I have a Tablet uptime sensor based on a Tablet switch being on/off but where before the values were being reported accurately, now it shows that my daughter has been spending like 19hours in a day on her tablet (when I know it's been much much MUCH less than that. I can provide more details on this, just let me know what kind of info would be useful (have nothing wrong on the logs though). |
This is still an issue on 2022.9.6 |
It happened again a few days ago. I have a history_stats sensor which tracks how long my 1st floor (zone 1) heating is “on” over the course of the day. Yesterday it only came on just once. After that, the “Zone 1 on today” sensor suddenly went to 13.3 hours: In the second chart, notice that the binary “Heat 1st” sensor had no state before that. I haven’t needed heat for longer than the purge_keep_days in Recorder, so the previous state change had rolled off. The sensor changed to “on” at around 1302. It stayed on for about 15 minutes and changed to “off.” The 13.3 hours which were added to history_stats looks like the elapsed time since midnight, not since the sensor changed to “on.” |
I have exactly the same issue. My heating turns off exactly at midnight, yet history stats assume it never turned off As a temporary fix, I change my accumulation period to start from 0:00:01 (1 second after midninght) and this fixed mine. There must be a bag if the state changes exactly at the same time as the accumulation window starts.
Below is the situation after the "fix" |
I had the same problem this morning, I was about about to apply the 1 second fix, but after the HA update to 2022.12.7 my history stats was ok again (0min instead of 7h) Was this corrected ? |
I'm having issues with this. The history_stats sensor resets slightly after midnight, meaning that plotting any statistics graphs with'max' picks up the value from the previous day if it was higher than today's. In my case it's how long the heating has been on each day, but I get lots of double days with the same value. |
My sensor showing currently time not sensor state time.
|
Same problem here. For a daily Pretty sure that would happen if it's a monthly time integration of a sensor and the sensor is |
There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. |
This definitely still needs attention as I'm pretty sure it hasn't been addressed. I have a workaround in place but it should just do what it says it does. |
@fenty17 You suggest that you've got a workaround; can you elaborate? Seems that this should not be allowed to go stale. Partially because I'm not aware of a way in the UI to fix the discrepancies. I've had to modify directly in the db, which is super ugly. |
@WoosterInitiative - I was following another similar GitHub issue and got the workaround from there: #84840 (comment) That issue has just been closed as 'not planned'. I'd much rather things like this got attention than voice assistant and Matter progress, but that's just my take. |
Hi all - I have a related/similar issue with history stats. See thread the root cause is that the 12am start does not happen until 30 seconds after midnight so the max for the new day holds the max from the previous day. strangely, if you chart using group_by max for the day, the data is correct but if using LTS (statistics) then max/day returns the max from the previous day (until the current day max > previous day max). Chart below shows the issue quite well. I need to use LTS (as I require more than 10 days of data) but the LTS data in not correct. |
@fenty17 - thanks for the link and tip to reset the entity state. I have implemented this and will see how it works. Whilst setting up the python_script i noticed there is a HACS script that does the same thing and a bit more. You can just add it through the HACS GUI |
I think I'm seeing the same issue, basically when I upgrade Home Assistant and it restarts, my history_stats type:time based on a binary sensor values are wrong for the next 24 hours, with odd spikes. See the screenshot, you can see the time period after I did the restart with all of the odd spikes: My issue might actually be #80871 |
I have a possible work-a-round for the issues with the history_stats reset after midnight. It is working very well for my use case (to be able to track daily hrs/use of a template switch). The solution is to use a Utility Meter Helper and use the History_stats sensor as the target entity. The Utility Meter can be set to daily and this resets correctly at midnight. Let me know if this works for you and if you need any further help. |
@jata1 if you just need charts best work around is to use https://github.com/alexarch21/history-explorer-card |
I see. Your use case is different than mine. I did post about another work-a-round that involved using a python script and an automation to reset the entity to 0 just before midnight (11.59.59) and this worked until 2023.8/9 but stopped working consistently. Why can't you use the history_stats and a utility meter as I describe above and then just export the stats from the utility meter? |
Hello, i have the problem that i use the history_stat for daily tracking of the filter cycle of a hot tub. The filtercycle starts as soon as the electricity price is low (i use Tibber with hourly changing electricity prices). I reset the history_stat for the binary filter sensor at midnight. I observed that when the history_stat sensor is reset at midnight and the Automation switches on the filter cycle due to cheap electricity at the same time (midnight) the history_stat stays at zero for hours even when filter is running. Only after restarting Homeassistant the correct history _stat value is displayed again. Looks to me that maybe the filtering starts seconds before history _stats reset and then history_stats does not recognize any change an stays at zero. |
@Arturfrain - just checking to see if your issue was solved by starting the automation just after midnight? That sounds like a reasonable approach to test (try 1 or 2 minutes after 12am). I am solving a different issue using the utility meter approach but it might still help you... My issue was that my stats were not calculating the correct max/day as the history_stat sensor would not reset until just after midnight but I do think the issues are probably related. My understanding of your issue is that your sensor/switch is 'on' before the history_stats resets and then the switching on event is missed. I doubt my solution will work for you as the underlying issue has not been solved. To answer the specific question - yes - I do use the utility meter to track the same data as the history stats and this works for calculating the time on/per day correctly. |
@jata1 : thank you for your answer. Yes at the moment it looks like my added wait time of 1 minute before starting the Action in my Automation seems to solve the problem. I still will observe the behavior through the next days. But it seems to me that my problem was as I thought related to a timing problem. I guess that the history_stats after resetting need a state change before it starts to actively check the sensor’s state. But this is only a guess and conclusion of my observation and maybe not correct. I will post in some days if my solution works stable. Maybe that helps others with same problem. Nevertheless i would love to understand if my interpretation is valid. |
@jata1 : my approach with short wait time before starting the Action in the Automation still works. The history_stats sensor updates on-values correctly after reset at midnight. |
I use utility meter workaround as the max value in statistics is wrong when you use the recommended approach for tracking time:
The hourly reset is done only during the first minute which causes max to still use the previous hour value. I don't know how to fix it (I've checked the sources). The workaround is to set up a Minute Time Tracker (I use 15 seconds precision):
Then I use utility meter to track states:
And automation to update it:
It's a little bit more work to set it up but work correctly. |
There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. |
This is still an issue. Such a fundamental logic shouldn't require multiple sensors and workarounds to work. Following documentation should result in correct MAX value, but that is not the case and therefore this is an issue yet to be resolved |
The problem
History_stats gives a wrong value if the sensor used as the base for the measurement is "on" over midnight and you want to measure a daily value.This problem has been reported before (#72357) but this issue was closed without action.
The following sensor is used to keep track of the daily usage of a water tap:
This speciIfic night switch.poolwater was open less than 5 minutes:
The history stat sensor should present 3m14s for July 27th and 22s for July 28th. But it continued to count all day ( today) even if the switch turned of 22s after midnight. In the morning it looked like this:
What version of Home Assistant Core has the issue?
2022.7.5
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
No response
Diagnostics information
No response
Example YAML snippet
No response
Anything in the logs that might be useful for us?
No response
Additional information
Not sure if this problem occurs everytime the sensor is open over midnight. I've seen this problem before monitoring another sensor, but I thought it was corrected in a later release. See https://community.home-assistant.io/t/strange-history-statistics/433815 for additional details.
The text was updated successfully, but these errors were encountered: