Statistics not working anymore?

I’ve got a statistics sensor for several things and after this last update none of them work anymore. I don’t see any errors, I just don’t get the statistics anymore, it just gives zero for the differences. The input data changes frequently so I don’t know why the statistics stopped.

sensor:

  - platform: mqtt
    name: "Power Company Meter"
    unique_id: "power_company_meter_consumption"
    state_topic: "homeassistant/sensor/rtlamr/scm/48722275"
    unit_of_measurement: 'kWh'
    value_template: "{{ value_json.Message.Consumption | float / 100.0 }}"

  - platform: template
    sensors:

      power_company_meter_buffered:
        friendly_name: "Power Company Meter Buffered"
        unit_of_measurement: "kWh"
        value_template: >-
          {% if states('sensor.power_company_meter') == "unknown" %}
            {{ states('sensor.power_company_meter_buffered') }}
          {% else %}
            {{ states('sensor.power_company_meter') }}
          {% endif %}
# Compute real time (approximate) usage by measuring the
# time difference between changes and amount of change

sensor:
  - platform: statistics
    entity_id: sensor.power_company_meter_buffered
    name: "Power Company Meter Stats"
    sampling_size: 3
    # age defines how long previous value waits without change assumed zero
    max_age: '00:10:00'

  - platform: template
    sensors:
      power_meter_current_power:
        friendly_name: "Power Meter Current Power"
        unit_of_measurement: "kW"
        icon_template: hass:flash
        value_template: >-
          {% if state_attr('sensor.power_company_meter_stats','count') | int < 2 %}
            0
          {% else %}
            {% set rate_calc = ( state_attr('sensor.power_company_meter_stats', 'change')|float/
                 ((state_attr('sensor.power_company_meter_stats', 'max_age') - state_attr('sensor.power_company_meter_stats', 'min_age')).total_seconds()/3600)
               ) | round(2) %}
            {% if rate_calc < -100 or rate_calc > 100%}
              0
            {% else %}
              {{ rate_calc }}
            {% endif %}
          {% endif %}

It’s probably not the problem, but all your floats and ints, need to have (0) after them, so that template won’t fail to render on startup if the sensors are not actually outputting numbers yet.

Because the Statistics integration was subject to a major revision and is listed in the 2021.12 Release Notes under Breaking Changes.

Revise the existing configuration of your Statistics sensors to comply with the new format.

1 Like

Ugh, so when the documentation says its optional, what it really means is it its required. Shame they couldn’t fix the documentation to reflect what is in the code.

I guess I’ll have to open a feature request or bug report, looks like there is no way to calculate change per hour with the available fields anymore. I can’t use change per second because its always zero and I can’t see the start/end date-time on the regular “change” one.