What's wrong with this wait_template?

HI,

new to the wait_template I was wondering if this would suffice (and is even allowed to add a for: to the wait_template, since it is not mentioned in the docs ) or that Id need some other form of delay, or even timeout:

  - alias: 'Switch Masterbed outlet when movement'
    id: 'Switch Masterbed outlet when movement'
    # initial_state: 'on'
    trigger:
      platform: state
      entity_id: sensor.master_bedroom_motion_sensor
      to: 'on'
    condition:
      condition: template
      value_template: >
        {{ is_state ('sensor.activity_selection', 'Naar bed')}}
    action:
      - service: switch.turn_on
        entity_id: switch.master_bed_outlet
      - wait_template: >
          {{ is_state('sensor.master_bedroom_motion_sensor', 'off') }}
        for:
          minutes: 2
      - service: switch.turn_off
        entity_id: switch.master_bed_outlet
      - condition: template
        value_template: >
          {{ is_state('input_boolean.notify_system', 'on')}}
      - service: notify.notify
        data_template:
          message: >
            {{as_timestamp(now()) | timestamp_custom("%X") }}: You've walked safely lift. Signing off.

idea: when getting up in the night, switch on the bed outlet powering the floor lights. Motion sensor sees movement, and when movement has stopped for 2 minutes, turn off the outlet again.

the checker error out, and will only accept in the form of:

  - condition: template
    value_template: >
      {{as_timestamp(now()) | int - 
          as_timestamp(states.sensor.master_bedroom_motion_sensor.last_changed)) 
         | default(0) | int > 120 }}

This is how I have a wait template setup for my welcome home notifications, it just sits and waits for 15 minutes till the motion sensor is tripped.

  action:
  - wait_template: '{{ states.binary_sensor.motion_sensor_158d0002281e2f.state == ''on'' }}'
    timeout: 00:15:00
  - data:
      entity_id: input_boolean.guest_greeting
    service: input_boolean.turn_on

Maybe this will help you out a bit.

yes thank you.

The way I understood the documentation, the timeout is to be used, catering the situation if nothing happens (read it takes longer than anything specified in the wait_template).
That might have the same effect, but clearly is something else. Again, as far as I understand it…

in a regular trigger we’d use the for: statement, but that isn’t allowed I think.

I am not really sure, how you can do this in a single automation using the wait template (i am not even sure if this can be done), but if you cant figure it out, then you can split your automation into two parts:
One to turn on the light and one to turn it off if the motion is off for two minutes ( using the “for” statement in the trigger)

I guess an extra binary_sensor template could be made which uses a time setting, and used in the wait_template.

switch_on
wait_template
switch_off

—edit—

  - wait_template: >
      {{as_timestamp(now()) | int - 
        as_timestamp(states.sensor.master_bedroom_motion_sensor.last_changed) | default(0) | int > 120 }}

or

  - wait_template: >
      {{ ( now() - 
        states.sensor.master_bedroom_motion_sensor.last_changed | default(0)).total_seconds() > 120 }}

should do it…?

both templates calculate the time correctly and evaluate fine in the dev-template. The automation doesn’t go past the wait_template though.

if I manually switch off the switch, the automation does continue and the notify.notify runs…
how strange this is…

testing this now:

  - wait_template: >
      {{ is_state('sensor.master_bedroom_motion_sensor', 'off') }}
  - delay:
      minutes: 1

because of @petro 's help here Turn a light off after x mins if it was turn on via rule - #15 by petro

which works half way. It does switch off after 1 minute, but when movement is sensed within that 1 minute delay, it doesn’t retrigger the wait template. Which is what I would have hoped for with my original wait_template…
can this be done?

separate post for clarity’s sake.
Although a bit of a hassle, an too bad the wait_template can’t be written as in the OP, this is working just fine now.

binary_sensor:
- platform: template
  sensors:
    master_bedroom_motion_sensor_timed:
      friendly_name: 'Master bedroom motion sensor timed'
      value_template: >
        {{ is_state('sensor.master_bedroom_motion_sensor','on')}}
      delay_off:
        minutes: 1
      device_class: motion

Had to puzzle a bit to have the sensor show the correct state in the frontend and react to the correct delay_off, and, not unimportantly, have the automation use the correct state, but here it is… finally.

for completenes sake the automation (I’ll leave the tested but not successful tries in there for reference):

  - alias: 'Switch Masterbed outlet when movement'
    id: 'Switch Masterbed outlet when movement'
#    initial_state: 'on'
    trigger:
      platform: state
      entity_id: sensor.master_bedroom_motion_sensor
      to: 'on'
    condition:
      - condition: template
        value_template: >
          {{ is_state ('sensor.activity_selection', 'Naar bed')}}
      - condition: template
        value_template: >
          {{is_state('switch.master_bed_outlet', 'off')}}
    action:
      - service: switch.turn_on
        entity_id: switch.master_bed_outlet
#      - wait_template: >
#          {{as_timestamp(now()) | int - 
#            as_timestamp(states.sensor.master_bedroom_motion_sensor.last_changed) | default(0) | int > 120 }}
      - wait_template: >
          {{ is_state('binary_sensor.master_bedroom_motion_sensor_timed','off')}}
#          {{ is_state('sensor.master_bedroom_motion_sensor', 'off') }}
#      - delay:
#          minutes: 1
      - service: switch.turn_off
        entity_id: switch.master_bed_outlet
      - condition: template
        value_template: >
          {{ is_state('input_boolean.notify_system', 'on')}}
      - service: notify.notify
        data_template:
          message: >
            {{as_timestamp(now()) | timestamp_custom("%X") }}: You've walked safely lift. Signing off.
1 Like