Wait Template and "wait.completed" for Automation

Hi all, I’m new to HA and was wondering if someone could help me with my “wait” syntax, I came up with the code through the Automation Editor, but when I put it in the Automation editor it saves but doesn’t work. Under “Wait” I put {{ is_state('binary_sensor.master_bedroom_motion_sensor_motion', 'on') }} with the “Timeout” being “00:00:03”. Under “Option 1” of the “Choose” template in the Automation Editor I put {{ wait.completed }} and under the second “Option” I put {{ not wait.completed }}. But it doesn’t work.

When I put I translate it all to YAML is translates to this:

timeout: '00:00:03'
wait_template: '{{ is_state(''binary_sensor.master_bedroom_motion_sensor_motion'', ''on'') }}'

choose:
  - conditions:
      - condition: template
        value_template: '{{ wait.completed }}'
    sequence:
      - device_id: 49c749c8c17f4829a692c9a4139ae8b1
        domain: light
        entity_id: light.master_bedroom_lamp_1
        type: flash
      - device_id: 8b85ba4057b04c5a9e457d035a287f84
        domain: light
        entity_id: light.master_bedroom_lamp_2
        type: flash
      - data: {}
        entity_id: automation.going_to_sleep_sleeping
        service: automation.turn_off
  - conditions:
      - condition: template
        value_template: '{{ not wait.completed }}'
    sequence:
      - device_id: 49c749c8c17f4829a692c9a4139ae8b1
        domain: light
        entity_id: light.master_bedroom_lamp_1
        type: flash
      - device_id: 8b85ba4057b04c5a9e457d035a287f84
        domain: light
        entity_id: light.master_bedroom_lamp_2
        type: flash
      - delay: '00:00:01'
      - device_id: 49c749c8c17f4829a692c9a4139ae8b1
        domain: light
        entity_id: light.master_bedroom_lamp_1
        type: flash
      - device_id: 8b85ba4057b04c5a9e457d035a287f84
        domain: light
        entity_id: light.master_bedroom_lamp_2
        type: flash
      - data: {}
        entity_id: automation.bed_time
        service: automation.turn_on
      - data: {}
        entity_id: automation.going_to_sleep_sleeping
        service: automation.turn_off
default: []

When I cleanup the apostrophes in the Template Editor (which I can’t save in the Automation Editor) to this:

timeout: '00:00:03'
wait_template: '{{ is_state('binary_sensor.master_bedroom_motion_sensor_motion', 'on') }}'

...

It finally tells me, the wait is undefined.
Could someone tell me why the wait is undefined, or how to define it?

You have not cleaned them up you have made this:

wait_template: '{{ is_state('

See where the quotes open and close?

They’re even coloured to show you this in your post (red = a string).

If you don’t like the single quotes escaped with single quotes that the UI editor uses (I don’t), use double quotes outside the template, like this:

wait_template: "{{ is_state('binary_sensor.master_bedroom_motion_sensor_motion', 'on') }}"

Hi, thanks for your help…Sorry it took me so long to reply, but I didn’t quite see the difference in the appostrophes you were talking about at first, but I did see the color difference, so I changed it from apostrophes to quotations and it still didn’t change colors until I eliminated the spacing here ) } and added it back and it was red afterwards, but it still tells me that the wait isn’t defined. Could you help me a bit further? I’m sorry if it seems like the same thing lol, but I have this now.

timeout: '00:00:03'
wait_template: "{{ is_state('binary_sensor.master_bedroom_motion_sensor_motion', 'on') }}"

choose:
  - conditions:
      - condition: template
        value_template: '{{ wait.completed }}'
    sequence:
      - device_id: 49c749c8c17f4829a692c9a4139ae8b1
        domain: light
        entity_id: light.master_bedroom_lamp_1
        type: flash
      - device_id: 8b85ba4057b04c5a9e457d035a287f84
        domain: light
        entity_id: light.master_bedroom_lamp_2
        type: flash
      - data: {}
        entity_id: automation.going_to_sleep_sleeping
        service: automation.turn_off
  - conditions:
      - condition: template
        value_template: '{{ not wait.completed }}'
    sequence:
      - device_id: 49c749c8c17f4829a692c9a4139ae8b1
        domain: light
        entity_id: light.master_bedroom_lamp_1
        type: flash
      - device_id: 8b85ba4057b04c5a9e457d035a287f84
        domain: light
        entity_id: light.master_bedroom_lamp_2
        type: flash
      - delay: '00:00:01'
      - device_id: 49c749c8c17f4829a692c9a4139ae8b1
        domain: light
        entity_id: light.master_bedroom_lamp_1
        type: flash
      - device_id: 8b85ba4057b04c5a9e457d035a287f84
        domain: light
        entity_id: light.master_bedroom_lamp_2
        type: flash
      - data: {}
        entity_id: automation.bed_time
        service: automation.turn_on
      - data: {}
        entity_id: automation.going_to_sleep_sleeping
        service: automation.turn_off
default: []

Edit: I see it’s still not red on here…where is it you were saying? I’m not seeing the difference.

The code block is not picking up that your code is yaml because it has recognised keywords from another language.Try this before your block of code:

```yaml
your code here…
```

Can you paste your whole automation, the bit before the wait template as well?

Ok, I even copied and pasted the code you put here, but it’s still not recognizing it as code. What about in the Template Editor or in the Automation Editor? What should I put to get it to recognize it there? Or if I put it in the “automations.yaml” file? What should I place before it there?

I don’t understand your question. Can you paste your whole automation, the bit before the wait template as well?

Edit: Ok, Here’s my automation:

- id: '1607192223149'
  alias: Going to Sleep/Sleeping
  description: Going to Sleep
  trigger:
  - device_id: b512556005b948f8b7e7e89df960329f
    domain: binary_sensor
    entity_id: binary_sensor.master_bedroom_motion_sensor_motion
    platform: device
    type: motion
  condition:
  - condition: zone
    entity_id: person.terrence
    zone: zone.home
  - condition: and
    conditions:
    - condition: device
      device_id: 1d9bb876f9564603b05ef417fd7c9824
      domain: binary_sensor
      entity_id: binary_sensor.media_room_motion_sensor_motion
      for:
        hours: 0
        minutes: 0
        seconds: 10
      type: is_no_motion
    - condition: device
      device_id: 63d492c118614773af81e28c0ef3a41a
      domain: binary_sensor
      entity_id: binary_sensor.office_motion_sensor_motion
      for:
        hours: 0
        minutes: 0
        seconds: 6
      type: is_no_motion
    - condition: not
      conditions:
      - condition: device
        device_id: b512556005b948f8b7e7e89df960329f
        domain: binary_sensor
        entity_id: binary_sensor.master_bedroom_motion_sensor_motion
        for:
          hours: 0
          minutes: 0
          seconds: 4
        type: is_motion
  - condition: not
    conditions:
    - condition: state
      entity_id: automation.bed_time
      state: 'on'
  action:
  - device_id: 49c749c8c17f4829a692c9a4139ae8b1
    domain: light
    entity_id: light.master_bedroom_lamp_1
    type: flash
  - device_id: 8b85ba4057b04c5a9e457d035a287f84
    domain: light
    entity_id: light.master_bedroom_lamp_2
    type: flash
  - delay: 00:00:01
  - choose:
    - conditions:
      - above: '120'
        condition: numeric_state
        entity_id: light.master_bedroom_ceiling
        value_template: '{{ state.attributes.brightness }}'
      sequence:
      - choose:
        - conditions:
          - condition: device
            device_id: 1d9bb876f9564603b05ef417fd7c9824
            domain: binary_sensor
            entity_id: binary_sensor.media_room_motion_sensor_motion
            type: is_motion
          - condition: device
            device_id: 63d492c118614773af81e28c0ef3a41a
            domain: binary_sensor
            entity_id: binary_sensor.office_motion_sensor_motion
            type: is_motion
          sequence:
          - device_id: 49c749c8c17f4829a692c9a4139ae8b1
            domain: light
            entity_id: light.master_bedroom_lamp_1
            type: flash
          - device_id: 8b85ba4057b04c5a9e457d035a287f84
            domain: light
            entity_id: light.master_bedroom_lamp_2
            type: flash
          - data: {}
            entity_id: automation.going_to_sleep_sleeping
            service: automation.turn_off
        - conditions: []
          sequence:
          - choose:
            - conditions: []
              sequence:
              - timeout: 00:00:03
                wait_template: '"{{ is_state(''binary_sensor.master_bedroom_motion_sensor_motion'',
                  ''on'') }}"'
              - choose:
                - conditions:
                  - condition: template
                    value_template: '{{ wait.completed }}'
                  sequence:
                  - device_id: 49c749c8c17f4829a692c9a4139ae8b1
                    domain: light
                    entity_id: light.master_bedroom_lamp_1
                    type: flash
                  - device_id: 8b85ba4057b04c5a9e457d035a287f84
                    domain: light
                    entity_id: light.master_bedroom_lamp_2
                    type: flash
                  - data: {}
                    entity_id: automation.going_to_sleep_sleeping
                    service: automation.turn_off
                - conditions:
                  - condition: template
                    value_template: '{{ not wait.completed }}'
                  sequence:
                  - device_id: 49c749c8c17f4829a692c9a4139ae8b1
                    domain: light
                    entity_id: light.master_bedroom_lamp_1
                    type: flash
                  - device_id: 8b85ba4057b04c5a9e457d035a287f84
                    domain: light
                    entity_id: light.master_bedroom_lamp_2
                    type: flash
                  - delay: 00:00:01
                  - device_id: 49c749c8c17f4829a692c9a4139ae8b1
                    domain: light
                    entity_id: light.master_bedroom_lamp_1
                    type: flash
                  - device_id: 8b85ba4057b04c5a9e457d035a287f84
                    domain: light
                    entity_id: light.master_bedroom_lamp_2
                    type: flash
                  - data: {}
                    entity_id: automation.bed_time
                    service: automation.turn_on
                  - data: {}
                    entity_id: automation.going_to_sleep_sleeping
                    service: automation.turn_off
                default: []
            default: []
        default: []
    - conditions: []
      sequence:
      - device_id: 49c749c8c17f4829a692c9a4139ae8b1
        domain: light
        entity_id: light.master_bedroom_lamp_1
        type: flash
      - device_id: 8b85ba4057b04c5a9e457d035a287f84
        domain: light
        entity_id: light.master_bedroom_lamp_2
        type: flash
      - data: {}
        entity_id: automation.bed_time
        service: automation.turn_on
      - data: {}
        entity_id: automation.going_to_sleep_sleeping
        service: automation.turn_off
    default: []
  mode: restart

Other than the UI editor mangling the quotes again I don’t see anything wrong with it. However I don’t use the UI automation editor and do not know what is supported and what isn’t. You could try using a state trigger instead of a device trigger. I vaguely remember something about device automations not supporting templates. As I said though, I could be wrong on that.

It is quite a complex automation. You may be better off just using yaml. The editor is not complete yet and may be struggling with it.

Ah, that makes sense…I do rarely see advanced coders using devices. I think they’re more in favor of states. I thought I might try it though lol. I’m not very good when it comes to syntax so I use the editors for help lol. Thanks for your help.