-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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
Spikes in statistics averages when HA restarts #119738
Comments
Hey there @ThomDietrich, mind taking a look at this issue as it has been labeled with an integration ( Code owner commandsCode owners of
(message by CodeOwnersMention) statistics documentation |
I also have this sometimes after a restart But not for every restart. First time I noticed was after my update to 2024.7. That a reload can trigger this, basically excludes a missing source sensor as a cause. During reload the source remained present. Just an example, where the source sensor remained nearly zero, but the statistics daily average sensor spikes between +31k and -31k when I reloaded the statistics yaml config. Note I reloaded 3-4 times in a row but just one spiked: Sensor config:
At the same time, a 15 minute average sensor for the same source sensor spiked as well. But lower values and first negative and then positive: Update: Someone in the forums suggested that the spike size seems to correlate with max_age. That is what I see as well |
I have also seen those spikes and didn't see any reason for them in my source data. I'm pretty sure that this happens because an undefined value is incorrectly being interpreted as MAX_INT or something and then getting into the calculation when the system is restarted. I moved away from the statistics / average_step because of this. Currently I am using filters instead, which at least don't give me any spikes. Both options have the annoying habit of becoming "undefined" when the value is stable for a long time. Currently, I am countering that with a template that is switching between the computed average and the actual source value depending on how long the value remains stable. This is pretty annoying because it means copying the same code again and again. It would be great if this functionality would be part of the integration itself. Sensor:
Template:
|
I just noticed that it is only my statistic helpers that have a value measured in kW. The statistics that use W, deg C or PPM are not affected by the restart. |
I have found a solution that will trigger a computation of the statistics values, even if the input value is stable. It was also mentioned in another thread here somewhere.
Adding such an attribute will change the state of a sensor whenever it is computed. Any statistic based on that sensor would then also be recomputed. I like this solution better than having to create a lot of otherwise useless sensors and it produces a lot less code. This is definitely a workaround for the issue that statistics are not recomputed when the actual value is stable for longer times. I'm still using statistics in a few places and I don't see the spikes anymore (min and max, not average). I'm not sure whether this is just a coincidence, since I made a couple of other changes as well. |
Hey everyone, Btw I was planning to release a "custom component" to accelerate the development of a "statistics beta" component. |
I have started to fix the issues and will soon create a merge request. I have solved or am on the was of solving the following issues:
|
Thank you for your efforts – it would be so good to get back to using this integration. Whilst you are looking at it, is there any way to define a period for the statistic; by both its start (done), but also by its end or length in time (only sample size currently allowed)?
Cheers,
David Inwood
From: unfug-at-github ***@***.***>
Sent: Saturday, August 24, 2024 10:47 AM
To: home-assistant/core ***@***.***>
Cc: Dtrotmw ***@***.***>; Author ***@***.***>
Subject: Re: [home-assistant/core] Spikes in statistics averages when HA restarts (Issue #119738)
I have started to fix the issues and will soon create a merge request.
I have solved or am on the was of solving the following issues:
* the time based average functions are now including the values before and after the last change (average step / linear), this is in particular relevant when values stay stable for a longer time
* many functions that currently require at least two values to work, can now work with a single value as well (e.g. simple average)
* the peaks will be a thing of the past (they happen because the value sequence is not correctly ordered according to time stamps)
* I will also try to add a refresh trigger, so that the average can be recomputed even if the inputs didn't change
—
Reply to this email directly, view it on GitHub <#119738 (comment)> , or unsubscribe <https://github.com/notifications/unsubscribe-auth/A3IC4W2LTSJ4T26SB3OOQ23ZTBJBDAVCNFSM6AAAAABJLVYWCSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMBYGI3TSNZXGE> .
You are receiving this because you authored the thread. <https://github.com/notifications/beacon/A3IC4W5EY2PAZ47DZVAFWRLZTBJBDA5CNFSM6AAAAABJLVYWCSWGG33NNVSW45C7OR4XAZNMJFZXG5LFINXW23LFNZ2KUY3PNVWWK3TUL5UWJTUJSWG5W.gif> Message ID: ***@***.*** ***@***.***> >
|
I have provided a pull request that will fix the issues: #124644 |
Hi David, Let's say you want the average from -30 sec to -20 sec, you can compute both the average for 30 sec (av30) and for 20 sec (av20) and then combine them like this: ((av30 * 30) - (av20 * 20)) / (30-20). |
Thank you so much for replying. I feel such a fool! You’d never know I had a good degree, albeit 5 decades ago…
And thank you again for addressing the problem I’ve been having with spikes etc for so long. It is much appreciated.
From: unfug-at-github ***@***.***>
Sent: Monday, August 26, 2024 4:07 PM
To: home-assistant/core ***@***.***>
Cc: Dtrotmw ***@***.***>; Author ***@***.***>
Subject: Re: [home-assistant/core] Spikes in statistics averages when HA restarts (Issue #119738)
Thank you for your efforts – it would be so good to get back to using this integration. Whilst you are looking at it, is there any way to define a period for the statistic; by both its start (done), but also by its end or length in time (only sample size currently allowed)? Cheers, David Inwood
Hi David,
you can basically achieve that already by combining two statistics:
Let's say you want the average from -30 sec to -20 sec, you can compute both the average for 30 sec (av30) and for 20 sec (av20) and then combine them like this:
((av30 * 30) - (av20 * 20)) / (30-20).
—
Reply to this email directly, view it on GitHub <#119738 (comment)> , or unsubscribe <https://github.com/notifications/unsubscribe-auth/A3IC4W27Z57EMYADNS7LVOLZTNABTAVCNFSM6AAAAABJLVYWCSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMJQGQ2DEOBVGM> .
You are receiving this because you authored the thread. <https://github.com/notifications/beacon/A3IC4WZUYCDYAGQCWJIS57TZTNABTA5CNFSM6AAAAABJLVYWCSWGG33NNVSW45C7OR4XAZNMJFZXG5LFINXW23LFNZ2KUY3PNVWWK3TUL5UWJTUJW2HWK.gif> Message ID: ***@***.*** ***@***.***> >
|
The spikes were created by race conditions. The component has an internal queue that is supposed to be sorted by time. During the startup phase values are loaded from the database and put into the queue. If during this phase a regular update is coming in, this will mess up the order of the queue, which then leads to incorrect averages being computed. Let's hope they will accept the proposed pull request and the issue will be gone. |
I am really looking forward for this PR to come through, thank you very much for fixing this issue which has been bothering me for a while! Your work is appreciated! |
The problem
I have created a number of statistics entities with long time periods (days or weeks) which are getting corrupted when HA restarts. Looking at their graphs I see large spikes around the time of a restart. E.g.:
The source sensor does not show any discontinuity at the restart:
What version of Home Assistant Core has the issue?
core-2024.6.2
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
Statistics
Link to integration documentation on our website
No response
Diagnostics information
No response
Example YAML snippet
Anything in the logs that might be useful for us?
No response
Additional information
Exactly this issue has been previously raised #89000 but went stale
I have reported details https://community.home-assistant.io/t/corruption-in-long-period-statistics-sensors-at-ha-re-starts/739113
The text was updated successfully, but these errors were encountered: