home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
70.95k stars 29.6k forks source link

Statistics change_second should have an option to function filter out negative change (similar to sum_differences_nonnegative) #117989

Closed bakerkj closed 3 days ago

bakerkj commented 3 months ago

The problem

The statistics change_second function works nearly perfectly for my needs as it computes the difference between the newest and the oldest measurement, which is then divided by the seconds between them. However when this computation is applied to a sensor that can only increase, but may reset to zero it gives erroneous results.

It would be fantastic if this sensor had an option similar to the sum_differences_nonnegative option which works on sensors that only increase, but occasionally reset to zero.

What version of Home Assistant Core has the issue?

core-2024.5.4

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

https://www.home-assistant.io/integrations/statistics/

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response

home-assistant[bot] commented 3 months ago

Hey there @thomdietrich, mind taking a look at this issue as it has been labeled with an integration (statistics) you are listed as a code owner for? Thanks!

Code owner commands Code owners of `statistics` can trigger bot actions by commenting: - `@home-assistant close` Closes the issue. - `@home-assistant rename Awesome new title` Renames the issue. - `@home-assistant reopen` Reopen the issue. - `@home-assistant unassign statistics` Removes the current integration label and assignees on the issue, add the integration domain after the command. - `@home-assistant add-label needs-more-information` Add a label (needs-more-information, problem in dependency, problem in custom component) to the issue. - `@home-assistant remove-label needs-more-information` Remove a label (needs-more-information, problem in dependency, problem in custom component) on the issue.

(message by CodeOwnersMention)


statistics documentation statistics source (message by IssueLinks)

ThomDietrich commented 3 months ago

Hello @bakerkj, thanks for the feedback! The statistics component calculates change_second like this: https://github.com/home-assistant/core/blob/ef138eb976ca1833137825fb1c730077550cb913/homeassistant/components/statistics/sensor.py#L697 The calculation doesn't care about the values and the nature of the source sensor.

Did I understand you correctly: You would like a new characteristic which take sum_differences_nonnegative and divides it by the seconds between the oldest and newest sample?

bakerkj commented 3 months ago

Hi @ThomDietrich,

I am trying to use change_second to compute the rate of increase of a counter that is always increasing, except when the device is reset (which is when the counter starts again from zero).

I think instead of the existing code

    def _stat_change_second(self) -> StateType:
        if len(self.states) > 1:
            age_range_seconds = (self.ages[-1] - self.ages[0]).total_seconds()
            if age_range_seconds > 0:
                return (self.states[-1] - self.states[0]) / age_range_seconds
        return None

I think I want something like this

    def _stat_change_second_something(self) -> StateType:
        if len(self.states) > 1:
            age_range_seconds = (self.ages[-1] - self.ages[0]).total_seconds()
            change = self.states[-1] - self.states[0]
            if age_range_seconds > 0 and change >= 0:
                return change / age_range_seconds
        return None

So for example

time in seconds | counter | statistic
0               | 0       | None
10              | 10      | 1
20              | 30      | 2
30              | 40      | 1
40              | 0       | None <- counter reset
50              | 20      | 2
60              | 30      | 1

I think in this way, the statistic will not be computed in the case where the counter resets (the change is negative)?

Does that make sense?

Thanks, Ken

ThomDietrich commented 3 months ago

I think it does make sense. Did you check this integration? I wonder if it already does what you want (not that I am saying this is strictly out of scope for the statistics integration)

https://www.home-assistant.io/integrations/derivative/

bakerkj commented 3 months ago

I think it does make sense. Did you check this integration? I wonder if it already does what you want (not that I am saying this is strictly out of scope for the statistics integration)

https://www.home-assistant.io/integrations/derivative/

If I understand correctly, the derivative sensor has the same challenge with counters that reset to zero:

For sensors that reset to zero after a power interruption and need a “non-negative derivative”, such as bandwidth counters in routers, or rain gauges, consider using the Utility Meter integration instead. Otherwise, each reset will register a significant change in the derivative sensor.

I don't think the Utility Meter integration does what I am looking to do.

issue-triage-workflows[bot] commented 1 week ago

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. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.