Wait for Template with a timer

Hi everyone, I am still very new to Home Assistant, but I and trying to make my automations better and need some help. Basically I have a motion sensor in the bedroom and I want the lights to turn off if there is no motion in the bedroom but also if we are not in the bathroom either (door will be open if unoccupied, but might switch it to the lights being off), I don’t want to walk back in there to a dark room. based on my other motion sensors I have found the best way was with a wait for template action, but I want to add a little bit of a for timer so if the motion has stopped for 2 minutes and the door has been open for 2 minutes as well? For reference the wait for trigger yaml is below. Thanks in advanced

  - wait_template: >
      {{ is_state('binary_sensor.master_bedroom_motion_sensor_motion_detection','off') and
      is_state('binary_sensor.master_bathroom_door_sensor','on') }}
    continue_on_timeout: true

Welcome to the community!

Copy the following into Developer Tools —> Template and play around with it:

- wait_template: >
    {% set one = 'binary_sensor.master_bedroom_motion_sensor_motion_detection' %}
    {% set two = 'binary_sensor.master_bathroom_door_sensor' %}
    {% set d_one = now() - states[one].last_changed >= timedelta(minutes=2) %}
    {% set d_two = now() - states[two].last_changed >= timedelta(minutes=2) %}

    {{ is_state(one,'off') and d_one and is_state(two,'on') and d_two }}
  continue_on_timeout: true

{{ one }}
{{ two }}
{{ d_one }}
{{ d_two }}
{{ states[one].last_changed }}
{{ states[two].last_changed }}
{{ now() - states[one].last_changed }}
{{ now() - states[two].last_changed }}

In your automation you can also use trigger variables like trigger.to_state.last_changed if the binary sensors above are used to trigger the automation.

1 Like