Yaml code to filter wrong values

Sorry for late Answer.
As I also get sometimes zero or very high values from the IR Reader of the smart meter I add some code to supress the low values. That is running up to now.

-platform: template
    sensors:
      energy_from_grid:
        friendly_name: "Energy from Grid"
        unique_id: Energy_from_Grid
        device_class: energy
        unit_of_measurement: kWh
        value_template: >
          {% if states('sensor.zaehler_sm_1_8_0') | float < 2000 %}
           {{ states('sensor.energy_from_grid') }}
          {% else %}
           {{ states('sensor.zaehler_sm_1_8_0') | float | multiply(1.0) | round(0) }}
          {% endif %}

This will only transfer new values if bigger than 2000kwh.
But sometimes I also get very big values that I also want to delete.

How to check if the new value is just the same or a little bit bigger than the old one (Energy from Grid)
So if my actual Energy_From Grid value is 2000kwh it should ignore values below 2000 and also ignore values above 2050

I’m not a yaml specialist :frowning:

-platform: template
    sensors:
      energy_from_grid:
        friendly_name: "Energy from Grid"
        unique_id: Energy_from_Grid
        device_class: energy
        unit_of_measurement: kWh
        value_template: >
#Ignore values below the actual energy value
          {% if states('sensor.zaehler_sm_1_8_0') | float < "Energy from Grid" % }
           {{ states('sensor.energy_from_grid') }}

#Ignore values above the actual energy value +50
          {% elseif states('sensor.zaehler_sm_1_8_0') | float > ("Energy from Grid" + 50) % }
           {{ states('sensor.energy_from_grid') }}

          {% else %}
           {{ states('sensor.zaehler_sm_1_8_0') | float | multiply(1.0) | round(0) }}
          {% endif %}

Can someone correct the code for me :wink:

Thank you in advance

Michael

Here is an Example of the values. Usually actual value is around 2000kwh and should not be lower or suddenly much bigger as I just consume around 10kwh per day

  - platform: template
    sensors:
      energy_from_grid:
        friendly_name: "Energy from Grid"
        unique_id: Energy_from_Grid
        device_class: energy
        unit_of_measurement: kWh
        value_template: >
          {% set grid = this.state | float(0) %}
          {% set zaehler = states('sensor.zaehler_sm_1_8_0') | float(0) %}
          {% if zaehler < grid %}
            {{ grid }}
          {% elseif zaehler > grid + 50 %}
            {{ grid }}
          {% else %}
            {{ zaehler | round(0) }}
          {% endif %}

EDIT

Correction. Removed unnecessary space character.

I’ve tried bu something is wrong

Invalid config for ‘template’ from integration ‘sensor’ at packages michael/02_zaehler.yaml, line 49: invalid template (TemplateSyntaxError: unexpected ‘}’) for dictionary value ‘sensors->energy_from_grid->value_template’, got “{% set grid = this.state | float(0) %} {% set zaehler = states(‘sensor.zaehler_sm_1_8_0’) | float(0) %} {% if zaehler < grid % }\n {{ grid }}\n{% elseif zaehler > grid + 50 % }\n {{ grid }}\n{% else %}\n {{ zaehler | round(0) }}\n{% endif %}\n”

I’ve tried to remove the space chr before the } in the if line.
Also I’ve changed elseif to elif.

After that the code was accepted by the system.

I’ll give it a try

Thanks

After restart hass i only get 0 value

Replace this line:

          {% set grid = this.state | float(0) %}

with this:

          {% set grid = states('sensor.energy_from_grid') | float(0) %}

Be advised that if its current value is 0 and zaehler is greater than 0 + 50 = 50 then it will remain 0 because that’s the logic you designed.

I’ve changed and set the value close to the real value.
But after each restart the value is 0 again because not initialised.

So how can I define a variable for comparison that will keep the value to use it also after restart of hassio?