I have another data spike issue I would like to fix.
Background is I am using LocalTuya to read Energy (Wh) from an energy meter. I also have a “Utility Meter” Helper to break that down into Day, Week, Month and Year rather than a less relevant meter total.
Sometimes when I add a device or reconfigure something in Tuyalocal, or restart HA, it seems a lower value is sometimes (but not always) read. The main problem with this is that the Utility Meter helper seems to take notice of this lower value, and assume it is relevant, and the next read that is correct causes the Utility meter to jump by the difference between the lower spike and the correct value which is sometimes substantial.
So I thought I would create another “filter” sensor to try and fix this. The basic logic is if the new value is below the previous value, just update with the previous value. I don’t really have a good understanding of the yaml coding here, but I have cut and pasted snipets from other things that work. My code is below :
- sensor:
- name: "hiking5_local_energy_filtered2"
icon: "mdi:counter"
unit_of_measurement: "Wh"
device_class: energy
state_class: total_increasing
state: >
{% set old = states('sensor.hiking5_local_energy_filtered2') | float %}
{% set new = states('sensor.hiking5_local_energy') | float %}
{% if old < new %} {{ new }}
{% else %} {{ old }}
{% endif %}
availability: "{{ states('sensor.hiking5_local_energy')|float(0) > 1 }}"
This creates the Entity, but nothing ever appears in there?
Does anyone have ideals on how to fix this, or an alternate approach that filters out the corrupting entry that has the meter go backwards for 1 reading?
Thanks in advance.