How to limit value of sensor using an template

Hi everyone,
we just got some new power meters from our local electricity provider. These can be read out using some ir-reades. Unfortuantly, the jumping once or twice a day to totally unreasonable numbers. Therefore I wanted to limit these jumps in order to gain meaningful charts and numbers in HA.

I’m currently using this template:

 - trigger:
      - platform: state
        entity_id: sensor.energiezaehler_kanzlei_pos_wirk_tariflos
        not_to:
          - unknown
          - unavailable
    sensor:
      - name: Stromverbrauch Kanzlei
        unique_id: stromverbrauch_kanzlei_2
        device_class: energy
        unit_of_measurement: kWh
        state_class: total_increasing
        state: >
          {% set x = trigger.to_state.state | float(0) %}
          {{x if (x > this.state) | float(0) and (x - this.state|float(0)) < 0.5 else this.state}}

But unfortunately this set’s the new “filtered” entity to “unavailable” instead of to the right value.

If it works, it should ignore all values that have a greater difference than 0.5 to the old value.

Has anyone an idea, why this doesn’t work?

Thanks in advance for your help!
Lars

        state: >
          {% set new = trigger.to_state.state | float(0) %}
          {% set old = this.state | float(0) %}
          {{ new if new > old and new - old < 0.5 else old }}