-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
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
Fix a history stats bug when window and tracked state change simultaneously #133770
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add tests for this case to show the problem and ensure it is solved?
../Frenck
Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍 |
async_fire_time_changed(hass, t4) | ||
await hass.async_block_till_done() | ||
|
||
assert hass.states.get("sensor.sensor1").state == "0.0" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without the fix the test fails here:
./tests/components/history_stats/test_sensor.py::test_state_change_during_window_rollover Failed: [undefined]AssertionError: assert '0.17' == '0.0'
A new test has been added. Fails on the original codebase and passes with the new bugfix. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, @karwosts 👍
../Frenck
Proposed change
This change attempts to fix a bug in history stats which looks like the following:
If the sensor has a tracked window that changes in the same minute as the source sensor changes, it is possible that the sensor will fail to register the state change for future updates until the next time the sensor toggles.
So if you have a sensor that tracks the on-time of a sensor for the current day (midnight to midnight), and if that sensor changes from on->off in the first minute after the window shift (12:00->12:01am), before the history stats sensor updates, history stats will act as if that sensor never turned off and keep accumulating time until the sensor turns on and back off again.
There is some racyness here so it doesn't always happen, it depends what event happens first after the window reset, the history stats sensor update or the source sensor update.
The update is missed because on the state change event, history stats sees it is a new time window and queries the database for state history, but this query does not yet include the current state change that triggered the update.
Then in the next scheduled update of the history stats sensor, it calculates that it does not need to re-query the database, so it just keeps operating on the previously fetched history, which is missing the latest state change.
To fix this issue, when we query the database in response to a state changed event, push the state change event to the end of the list of recorded state events (this is already done in cases where we don't query the database).
I'm fairly sure this fixes the issue reported here: #75903 (comment), and maybe also #121535, but not sure.
Type of change
Additional information
Checklist
ruff format homeassistant tests
)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest
.requirements_all.txt
.Updated by running
python3 -m script.gen_requirements_all
.To help with the load of incoming pull requests: