Skip to content

unfug-at-github/statistics-home-assistant

Repository files navigation

This is an enhanced copy of the home assistant statistics component including some improvements.

It fixes several issues:

  • Spikes at startup due to a lack of synchronization of the internal value queue
  • Values are provided even if only a single value is in the buffer (the standard component returns unknown in these cases). E.g. the average of [7] becomes 7, not unknown.
  • When a time interval is specified the average is computed over the given interval. The standard component only computes the average between the first and the last value change within the interval, which ignores the time before the first change as well as the time after the last change. This way of comuting time-based average leads to results that have led to a lot of questions / discussions in the forum.
  • The value before the first value change within the interval is taken into account for other computations as well. E.g. if the value changes only once from 2.0 to 1.0 within the interval, the max would be computed as 1.0 by the standard component. Now it becomes 2.0 (the last value reported before the interval).

It also adds one additional feature:

  • A "refresh_interval" parameter allows to re-compute the value on a regular basis. Usually values only change when a new value is received or an old value expires. However, the value of time-based statistics change just because the interval is shifting with time, and so a re-calculation even without external triggers is making sense.

As a side effect, the "keep_last_sample" will have no effect if "max_age" was specified

It also includes a test setup (configuration.yaml) to demonstrate the changes. You can easily see how the ouput of the standard and the custom component differ. The yellow line is the input, the blue line the standard sensor and the orange one this custom sensor. The blue line is broken in several places because the standard sensor is unable to compute a value if there are less than two value changes in the buffer.

screenshot

Here is the definition of the sample sensors:

sensor: 
  - platform: statistics_custom
    name: "xxx_test_avg_step_10s_refresh"
    entity_id: sensor.xxx_test_float
    state_characteristic: average_step
    max_age:
      seconds: 10
    refresh_interval:
      seconds: 1
  
  - platform: statistics_custom
    name: "xxx_test_avg_linear_10s_refresh"
    entity_id: sensor.xxx_test_float
    state_characteristic: average_linear
    max_age:
      seconds: 10
    refresh_interval:
      seconds: 1

I managed to get some of the changes integrated into the core system, but not all of them. The following changes were added to the core system, so they should be included in one of the next releases:

  • spikes
  • single value issue
  • adding values to the buffer even when the value wasn't changed (has a similar effect like the refresh interval)

It doesn't seem like I will get the remaining things in, so I leave this here as an alternative for those who want the other features as well.

This enhanced component solves issues discussed here: 1 2 3

Here is an image of the two time-based averages (linear / step) following an input curve: average_image

Please refer to homeassistant core regarding topics around copyright etc.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages