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 %}