Would love to get your input on something I observed lately. This has been working beautifully until the recent past, let’s say starting with the August update. Here is the situation:
Context: I have an energy meter which reports its values through MQTT into my HA intance. That sensor is called “sensor.stromzaehler”. Reality is that this sensor every once in awhile reports nonsense, so I cannot use this as input to my utility meters.
Hence my workaround through this was so far.
-
I have an automation running which upon a change in this sensor.stromzaehler, immediately stores the previous value (via trigger.from_state.state) into an input number called “input_number.stromzaehler_aktuell_alt”. That allows me to compare the latest reported state with the previous state.
-
Within HA I have defined a template sensor which does the comparisons (and is then later used for my utility meters). In essence I check:
- (First case of the IF statement: If the new sensor state is below the old sensor state then the template sensor is assigned the previous state of the energy meter (because that meter can never report lower numbers than before)
- (Second case of the IF statement): If the new sensor state is significantly above the old sensor state (= an unrealistic spike is reported), then again the previous state is taken.
- Otherwise, things are seen as “good” and the template sensor gets assigned the new state of the energy meter sensor.
May not be the most elegant solution (when I wrote this, I was an absolute newbie to HA and to coding overall), yet it worked perfectly over many months. Now since a couple of weeks in spite of the above logic, I do get spikes into the template sensor (which then drives my utility meters crazy and I often have to recalibrate them). Attached is a picture of the more recent spikes. I would have thought that the 2nd statement in my IF logic should have prevented that from happening.
I think this may be related to when HA restarts. Maybe the order of HA reading the configuration might have something to do with this. Any thoughts, and ideas how I could achieve the same in a better way?
Thank you as always!!!
stromzaehler_gerundet:
value_template: >
{% if states('sensor.stromzaehler') | float < states('input_number.stromzaehler_aktuell_alt') | float %}
{{states('input_number.stromzaehler_aktuell_alt') | float | multiply(1/1000) | float | round(1) }}
{% elif (states('sensor.stromzaehler') | float - states('input_number.stromzaehler_aktuell_alt') | float) > 0.2 %}
{{states('input_number.stromzaehler_aktuell_alt') | float | multiply(1/1000) | float | round(1) }}
{% else %}
{{states('sensor.stromzaehler') | float | multiply(1/1000) | float | round(1) }}
{% endif %}