Today it happened to me too… energy dashboard screwed due to sensor fault in “grid usage” input sensor.
I accidentally noticed this:
I can’t have negative values. Only taking power from the grid currently.
This input senor is generated using this yaml:
energy_total_total:
friendly_name: Stromverbrauch Alle Gesamt
unit_of_measurement: "kWh"
value_template: >-
{{
(
states('sensor.energy_1_total') | float(0) +
states('sensor.energy_2_total') | float(0) +
states('sensor.energy_3_total') | float(0) +
...
) | round(2)
}}
where the sensor.energy_X_total
are utility meters, each with simple (no reset etc.)
energy_X_total:
source: sensor.power_plug_X_total_consumption
In customize.yaml
I use
sensor.energy_total_total:
friendly_name: Stromverbrauch Alle gesamt
icon: mdi:counter
device_class: energy
state_class: total
last_reset: "2021-06-04T00:00:00+00:00"
Reason is, that a part of all input sensors was not available. Due to my calculation template above, the sum is calculated without those. Unfortunately that happened quite often as history shows…
…but luckily it never happened right at the snapshot time when LTS is being generated. This was the case today for the 1st time (part of the input sensors like
sensor.energy_2_total
were unavailable) right from 15:59:14 until 16:00:07, so that the LTS generation at 16:00:00 catched the dropped value and… et voila, the energy dashboard is screwed.
Todays’ drop/incident is exactly 616,13 kWh.
Now I see two (urgent) actions:
-
Fix the current states + statistics + statistics_short_term tables data to fix the energy dashboard. I did something similar in the past already and am quite confident being able to fix this in the backend with some SQL magic.
OR (likely saving me 2 hours of time, at least - compared to the SQL fixing):
how would I fix this in the/developer-tools/statistics
section properly?
-
Prevent this to happen again. The entities value NEVER can decrease, only an increase is possible. Ideas:
- 2.a) Would using
state_class: total_increasing
instead ofstate_class: total
achieve this? TBH I read Sensor Entity | Home Assistant Developer Docs twice and still don’t know if this is the right setting to achieve what I’m looking for (sensor value can only stay or increase, but never decrease). - 2.b) Adjust the
energy_total_total
sensor definition: check if all sensors arenot in ['0', 'NULL', 'unavailable', 'unknown']
and only calculate the sensor value then. Like:
{% if sensor_1 not in ['0', 'NULL', 'unavailable', 'unknown'] and
sensor_2 not in ['0', 'NULL', 'unavailable', 'unknown'] and
sensor_3 not in ['0', 'NULL', 'unavailable', 'unknown'] and
...
... %}
{% set total_energy = (sensor_1 | float(0) + sensor_2 | float(0) + sensor_3 | float(0) + ...) | round(2) %}
{{ total_energy }}
{% endif %}
Can you please advise on
- task 1 and
- what’s the best way for ticking task 2
Thank you in advance