Custom template: new state for x seconds

sensor:
  - platform: template
      tvattmaskin_idle:
        friendly_name: 'Tvättmaskinen Idle'
        value_template: >-
          {% if (states.sensor.tvattmaskinen_switch_power.state | float) < 3.0  %}
            true
          {% else %}
            false
          {% endif %}  

This custom template above creates a sensor that returns true as soon as the power from my washing machine goes under 3 W witch indicates that the laundry is done.

I would like to extend the template so the condition is true after 60 seconds.

Some times the washing machine goes under the threshold for a minute or so witch gives false positives.

Any suggestions are welcome since I cannot find this info in the jinja2 docs.

I use something simliar for motion sensors in my house, where I want something to happen, but only if the sensor has been off for a full minute. I then use the delayed sensor in my automations.

Untested, but this should work

- platform: template
   sensors:
     tvattmaskin_idle_delayed:
       friendly_name: Tvättmaskinen Idle Delayed
       value_template: "{{ is_state('sensor.tvattmaskin_idle', 'true') }}"
       delay_off:
         seconds: 60
       entity_id: sensor.tvattmaskin_idle

Thanks @ptdalen. I had to edit your suggestion a bit:

- platform: template
   sensors:
      tvattmaskin_idle_delayed:
        friendly_name: 'Tvättmaskinen Idle Delayed'
        delay_off:
          seconds: 30
        value_template: >-
          {{ is_state('sensor.tvattmaskin_idle', 'true') }} 

Now I just have to calibrate the minimum amount of seconds needed in order to avoid false positives.

1 Like