Solution to removing nonsense sensor value stopped working reliably

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:

    1. (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)
    1. (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.
    2. 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 %}

Using

{{ this.state }}

In your template would be a lot less complicated. It gives you the previous value of the template sensor.

So remove your automation and input_number, and update your template sensor to this:

       stromzaehler_gerundet:
         value_template: >
           {% if  states('sensor.stromzaehler') | float < this.state | float %}
             {{ this.state }}
           {% elif states('sensor.stromzaehler') | float - this.state | float > 0.2 %}
             {{ this.state  }} 
           {% else %}
             {{ ( states('sensor.stromzaehler') | float / 1000 ) | round(1) }}
           {% endif %}
         availability_template: "{{ has_value('sensor.stromzaehler') }}"
2 Likes

Thanks for your fast reply! This does read so much simpler and intuitive; unfortunately though if I put this into action, I get “unknown” as state. And when I put it into the template section in the development tools, I get a “‘this’ is undefined” error.

The template editor has no way of knowing what the this refers to. So you can’t use it to test this template.

Initialise the sensor by using developer tools → states to set its value to whatever the value of sensor.stromzaehler is now.

Got it, understand why it is not testable. Thanks for the explanation!

sensor.stromzaehler and the template sensor sensor.stromzaehler_gerundet are now having concrete values, yet still after a restart the template sensor.stromzaehler_gerundet has an Unknown state. And it is the restarting situation which seems to break my own original code, so this solution somehow doesn’t want to work.

I will keep searching/researching, maybe I can figure it out. After the BBQ party tonight :slight_smile:

If you use a triggered template sensor in the new format it will be restored after a restart:

configuration.yaml

template:
  - trigger: 
      - platform: state
        entity_id: sensor.stromzaehler
        to: ~
    sensor:
      - name: Stromzaehler Gerundet
        state: >
          {% if states('sensor.stromzaehler') | float < this.state | float %}
            {{ this.state }}
          {% elif states('sensor.stromzaehler') | float - this.state | float > 0.2 %}
            {{ this.state  }} 
          {% else %}
            {{ ( states('sensor.stromzaehler') | float / 1000 ) | round(1) }}
          {% endif %}
        availability: "{{ has_value('sensor.stromzaehler') }}"

That worked! Thanks a lot tom, your help is always greatly appreciated. I am confident that this will overcome the issues when restarting HA at times creating those spikes. Thank you!

1 Like