Wait_template in a script

I am trying to use wait_template in a script. I want to wait until the TTS notification has finished playing by using the state of the TTS media player as the condition ( so, wait until ‘idle’ is true). The length of the TTS notification depends on a number of factors.

- service: script.tts_morning_info
- wait_template: "{{ states.media_player.vlc_tts.state == 'idle' }}"
  time_out: 00:01:00

I am not sure what I am doing wrong based on the documentation


And don’t see any examples of wait_template here on the forums.

Has anyone successfully used wait_template?

1 Like

I would like to know that as well, not working for my configuration

It should be timeout: not time_out:

If that doesn’t fix it, could you please include more of your script? (I’m relatively new to homeassistant and yaml, but I’ll help if I can!)

I’m not using the timeout option. Just the wait_template

My script currently looks like this:

welcome_home:
  alias: Play welcome home sound and start Radio
  sequence:
    - service: media_player.turn_on
      data:
        entity_id: media_player.castbedroom
    - service: media_player.volume_set
      data:
        entity_id: media_player.castbedroom
        volume_level: '0.4'
    - service: tts.google_say
      entity_id: media_player.castbedroom
      data:
        message: 'Welcome home. Starting Einslive Radiostation.'
    - delay: 00:00:09
#    - wait_template: "{{ states.media_player.castbedroom.state == 'idle' }}"   # currently broken?
    - service: media_player.volume_set
      data:
        entity_id: media_player.castbedroom
        volume_level: '0.15'
    - service: media_player.play_media
      data:
        entity_id: media_player.castbedroom
        media_content_id: 'http://wdr-1live-live.cast.addradio.de/wdr/1live/live/mp3/128/stream.mp3'
        media_content_type: 'audio/mp4'

This is working right now, but if I try to use the wait_template option instead of the delay option it just skips the tts

I am having the same issue. I have to disable any wait_template steps in my scripts. The variables don’t get passed to them.

I am getting this also:

- wait_template: "{{ trigger.to_state.state == 'on' }}"

2017-08-29 18:01:52 ERROR (MainThread) [homeassistant.helpers.condition] Error during template condition: UndefinedError: ‘trigger’ is undefined

in the logs. has an issue been raised on github?

I’ve been trying to get this to work with the timeout as well without any success, anyone any ideas?

I was able to get this to work. in a script/automation

- wait_template: "{{is_state('input_boolean.test', 'on')}}"

it appears that {{states.input_boolean.test.state == ‘on’}} does not

however the timeout option does not continue on with the script like documented it causes the script quit.

there seems to be an old open issue on it.

1 Like

Awesome! Thanks. I was able to get this to work with your script. I couldn’t get the timeout to work however.

was this ever solved? Im struggling with this wait_template that evaluates just fine, but the automation won’t g past it:

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 }}
  - service: switch.turn_off
    entity_id: switch.master_bed_outlet

wait_template can also be:

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

see: What's wrong with this wait_template? please for more info

You can’t use now() in a wait_template.

Now() is evaluated once when it is asked. You need to use sensor.time which is evaluated every time it changes (every minute).

1 Like

Hi!

yes, thank you. I did realize that too. @pnbruckner pointed me to that also.
I did test to see if the same template as a binary_sensor template, with the addition of entity_id sensor.time would work, but that failed also.

found the solution finally here: What's wrong with this wait_template?

hope it’s of any use to the dear community.

woud still be very interested in a wait template that would do it on its own, and not need the secondary binary_sensor.

1 Like

After a long time I’m stuck with the same problem here. The wait_template doesn’t seem to work. How did you solve this in the end?

alias: test media player
sequence:
  - service: tts.azure_say
    data:
      entity_id: media_player.mini
      message: 'Hallo liebe Leute, hier spricht dein zuhause!'
  - wait_template: "{{ states.media_player.mini.state == 'idle'}}"
    continue_on_timeout: true
    timeout: '10'
  - service: tts.azure_say
    data:
      entity_id: media_player.mini
      message: >-
        Und hier kommt noch ein zweiter Satz hinterher
mode: single

The script goes straight to the second message.

use a wait_for_trigger

Why can it not finish sayin the full tts message? ends after “Hejsan. Tre minuter och femton”

  balsam_timer:
    alias: Balsam timer 3min 15sec
    sequence:
    - service: timer.start
      data:
        entity_id: timer.balsam
        duration: "00:02:15"
    - service: tts.reversotts_say
      data:
        entity_id: media_player.badrum
        message: "Hejsan. Tre minuter och femton sekunder startar nu."
    - delay: '00:00:15'
    - wait_template: "{{ is_state('media_player.badrum', 'idle') }}"
    - delay: '00:00:05'
```