Template Sensor update when difference is not to high or low

Hi,

i have a template sensor and want to prevent peaks.
For that reason i had the idea to only update the sensor, when the difference before and now is not that high.

It is for my Smart Meter, so the kWh can be only growing. But for some reason my sensor send sometimes peaks with really high kWh or minus kWh which cannot be real.

So my idea was to make state for the sensor, that the sensor only updates when the difference is lower than 1kWh.

Any ideas how i can do that?
This is my Template Sensor

- sensor:
  - name: 'Netzbezug'
    device_class: energy
    unit_of_measurement: kWh
    state_class: total_increasing
    icon: mdi:flash
    state: >
        {{ states('sensor.smartmeter_consumption')|float }}

Now i just get the value from smartmeter_consumption but how to update only, if the “Netzbezug” is only maximum 1kWh lower

Make it a trigger-based sensor:

Trigger off state changes, work out the difference between trigger.to_state.state and trigger.from_state.state. If you want to reject the new value, set the sensor to this.state for no change.

Did you have an example? Never used triggers :confused:

Thinking about it further, you don’t actually need a trigger. I was thinking you would need to compare the previous and new values of your smart meter sensor, but you don’t — you just need to compare against your template sensor. Something like this:

- sensor:
  - name: 'Netzbezug'
    device_class: energy
    unit_of_measurement: kWh
    state_class: total_increasing
    icon: mdi:flash
    state: >
        {% if (states('sensor.smartmeter_consumption')|float(0) -
               this.state|float(0))|abs > 1 %}
          {{ this.state }}
        {% else %}
          {{ states('sensor.smartmeter_consumption')|float(0) }}
        {% endif %}

Template says “if the absolute difference between this sensor’s existing value and the incoming smart meter sensor is greater than 1kWh, keep the prior value; otherwise update to the smart meter value”.

Might do some odd things at startup. If the smart meter sensor is unknown, then the |float(0) substitutes in 0 as the value to use.

The trigger version could have looked like this:

- trigger:
  - platform: state
    entity_id: sensor.smartmeter_consumption
  sensor:
  - name: 'Netzbezug'
    device_class: energy
    unit_of_measurement: kWh
    state_class: total_increasing
    icon: mdi:flash
    state: >
        {% if (trigger.from_state.state|float(0) -
               trigger.to_state.state|float(0))|abs > 1 %}
          {{ this.state }}
        {% else %}
          {{ trigger.to_state.state }}
        {% endif %}

That would ignore a peak, and would also ignore the value after it. If you got two peak values in succession, it would update to that.

You can just put it in a template condition.
So

  1. you trigger your automation on the state change without setting the from or to value/state
  2. you set a template condition at conditions like for example:
{% set state_change  = trigger.to_state.state - trigger.from_state.state %}  
{{
  state_change  > 0 and state_change  < 2
  or
  state_change  < 0 and state_change  > -2
}}

3 You set the action(s) you like

This isn’t an automation, it’s a template sensor. They don’t have conditions.

Sorry, responded too quickly :blush: