Corruption in long period statistics sensors at HA re-starts

I’m collecting some long period weather stats for a project but they are getting corrupted, I think when HA restarts. I’m collecting atmspheric pressure over 14 days & wind over 60 hrs, so re-starts are a real problem. Just concentrating on the wind, here’s what I’m seeing:


I’m puzzled by the spikes and the sudden jumps in value. The source sensors don’t show this. For example:

Is there a way of avoiding the problems caused by re-starts?

The two sensor showing the spikes & jumps are:-

  - platform: statistics
    name: wind_60hr
    unique_id: wind_60hr
    entity_id: sensor.wind_display
    state_characteristic: average_linear
    precision: 2
    max_age:
      hours: 60

And for the direction i’m going the long way round becuase I’m not sure average_circular will work:

 - platform: statistics
   name: wind_dir_cosine_60hr_avg
   entity_id: sensor.wind_dir_cosine
   unique_id: wind_dir_cosine_60hr_avg
   state_characteristic: average_linear
   max_age:
     hours: 60
   
 - platform: statistics
   name: wind_dir_sine_60hr_avg
   entity_id: sensor.wind_dir_sine
   unique_id: wind_dir_sine_60hr_avg
   state_characteristic: average_linear
   max_age:
     hours: 60

 - sensor:
   - name: wind_direction_60hr
     unique_id: wind_direction_60hr
     state_class: measurement
     unit_of_measurement: "°"
     state: >
       {% set wind_direction = float(atan2(float(states('sensor.wind_dir_sine_60hr_avg'), 0), float(states('sensor.wind_dir_cosine_60hr_avg'), 1), 0)*180/pi, 0)  | round (0) %}
       {% if wind_direction < 0 %}
          {{ wind_direction + 360 }}
       {% else %}
          {{ wind_direction }}
       {% endif %}
   - name: wind_dir_cosine
     unique_id: wind_dir_cosine  
     state: "{{ (cos(float(states('sensor.wind_display_dir'), 0)*pi/180, 0) * float(states('sensor.sensor.wind_display'),0)) | round (4) }}"
     state_class: measurement
   - name:  wind_dir_sine
     unique_id: wind_dir_sine
     state: "{{ (sin(float(states('sensor.wind_display_dir'), 0)*pi/180, 0) * float(states('sensor.sensor.wind_display'),0)) | round (4) }}"
     state_class: measurement

The 2 entities sensor.wind_display & sensor.wind_display_dir monitor a few different sources of local wind conditions to eliminate problems caused by non-availability. The 2nd history display above shows they behave themselves.

See: https://community.home-assistant.io/t/strange-behavior-in-average-statistics-sensor/738470/2?u=tom_l

Thanks Tom, I raised a new issue on GitHub referencing that older one.

1 Like