Script using a variable for wait_template

i tried defining a variable {{ speaker_template }} for my wait_template. i don’t get any errors but the script doesn’t seem to follow the template conditions.

the template syntax is correct because if i place it under the wait_template directly instead of using the {{ speaker_template }} the script will work perfectly.

is there something i’m missing?

(i initially posted this question as a follow up to my previous issue https://community.home-assistant.io/t/i-need-some-help-with-my-script-noob-warning/419748 then realized i should have created a separate topic. please forgive me for double posting)

alias: Announce Close Gate At Night
sequence:
  - variables:
      speaker_device: media_player.echo_show
      speaker_service: notify.alexa_media
      announce_volume: 0.3
      announcement_msg: '{{ ("1","2","3") | random }}'
      old_volume: '{{ state_attr(speaker_device,''volume_level'') | float }}'
      mediaplayer_State: '{{ states(speaker_device) }}'
      speaker_template: >-
        {{ (speaker_device == 'media_player.echo_show' and
        is_state('input_boolean.room_occupancy_study', 'on')) or (speaker_device
        == 'media_player.sonos_beam' and
        is_state('input_boolean.room_occupancy_living_room', 'on')) or
        (speaker_device == 'media_player.kitchen_2' and
        is_state('input_boolean.room_occupancy_kitchen', 'on')) or
        (speaker_device == 'media_player.landing_2' and
        is_state('input_boolean.room_occupancy_1st_floor', 'on')) }}
  - wait_template: '{{ speaker_template }}'
    timeout: '00:30:00'
    continue_on_timeout: false
  - condition: state
    entity_id: binary_sensor.zone_gate_open
    state: 'on'
  - service: media_player.media_pause
    target:
      entity_id: '{{ speaker_device }}'
  - service: media_player.volume_set
    data:
      volume_level: '{{ announce_volume }}'
    target:
      entity_id: '{{ speaker_device }}'
  - service: '{{ speaker_service }}'
    data:
      data:
        type: tts
      message: '{{ announcement_msg }}'
      target: '{{ speaker_device }}'
  - service: media_player.volume_set
    data:
      volume_level: '{{ old_volume }}'
    target:
      entity_id: '{{ speaker_device }}'
  - condition: template
    value_template: '{{ mediaplayer_State == ''playing'' }}'
  - delay:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
  - service: media_player.media_play
    target:
      entity_id: '{{ speaker_device }}'
mode: parallel
max: 5

thank you

does anyone know why the wait_template when defined like this in my script?

my idea was to make my speaker_template into 2 templates - alexa_device and google_device. then have 2 sequences inside the script so if an alexa device was specified by the automation the sequence for alexa devices will run and if a google or sonos device was called then the script will run the sequence for google_say.

thanks.

In your example, the value of the variable speaker_template is computed before it is used in wait_template. The value should be computed by wait_template.

Eliminate the speaker_template variable and use its template in wait_template.

You can do what @123 is saying and also keep the template to a minimum by adjusting the variables output to return the input_boolean instead. And you can use yaml to handle the hard configuration for you.

alias: Announce Close Gate At Night
variables:
  config:
    media_player.echo_show: input_boolean.room_occupancy_study
    media_player.sonos_beam: input_boolean.room_occupancy_living_room
    media_player.kitchen_2: input_boolean.room_occupancy_kitchen
    media_player.landing_2: input_boolean.room_occupancy_1st_floor
  speaker_device: media_player.echo_show
  speaker_service: notify.alexa_media
  announce_volume: 0.3
  announcement_msg: >
    {{ ("1","2","3") | random }}
  old_volume: >
    {{ state_attr(speaker_device, 'volume_level') | float }}
  boolean: >
    {{ config.get(speaker_device) }}
sequence:
  - condition: template
    value_template: "{{ boolean is not None }}"
  - wait_template: '{{ is_state(boolean, 'on') }}'
    timeout: '00:30:00'
    continue_on_timeout: false
  - condition: state
    entity_id: binary_sensor.zone_gate_open
    state: 'on'
  - service: media_player.media_pause
    target:
      entity_id: '{{ speaker_device }}'
  - service: media_player.volume_set
    data:
      volume_level: '{{ announce_volume }}'
    target:
      entity_id: '{{ speaker_device }}'
  - service: '{{ speaker_service }}'
    data:
      data:
        type: tts
      message: '{{ announcement_msg }}'
      target: '{{ speaker_device }}'
  - service: media_player.volume_set
    data:
      volume_level: '{{ old_volume }}'
    target:
      entity_id: '{{ speaker_device }}'
  - condition: template
    value_template: >
      {{ is_state(speaker_device, 'playing') }}
  - delay:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
  - service: media_player.media_play
    target:
      entity_id: '{{ speaker_device }}'
mode: parallel
max: 5
1 Like

@123

ok i understand. thanks for the explanation.

@petro wow this looks very nice. I’m definitely stealing this code!

i think i want to try modifying it so that i have 2 config variables - one for config_alexa_devices and one for config_google_devices so that i can let the script handle the speaker_service part ie. either notify.alexa_media or tts.google_say.

devices in the config_alexa_device will have one condition template and devices in the config_google_devices will have another condition template

hi @petro, i’m getting error Message malformed: invalid template (TemplateAssertionError: No test named 'None'.) for dictionary value @ data['sequence'][0]['value_template']

what does that mean?

other than that I would like to learn from you :-
I noticed that you moved variables to be immediately after alias.
is this better than placing it under sequence? i guess it makes the variables “global” for the script?

whats the difference doing

announcement_msg: '{{ ("1","2","3") | random }}'

and

announcement_msg: >
    {{ ("1","2","3") | random }}

?

is it when using > then i don’t need to enclose the template with ''?

for the condition value template you used "" instead of the single ''. when do we use one or the other?

  - condition: template
    value_template: "{{ boolean is not None }}"

i also noticed (not here, but elsewhere) sometimes there is a - after the > like so:

  announcement_msg: >-
    {{ ("1","2","3") | random }}

when would i need to use the >-?

i’m making some changes so the script will handle which speaker uses alexa or google tts.

it means I made a typo. Replace the work None with none.

the > means “Multiline field” in yaml. All lines below it are part of the field with the arrow (until the next field). Read up on it here.

If you use “” insdie the template you have to use ‘’ outside and vice versa.

the - removes leading whitespace. It’s literally pointless in the home assistant world because we trim leading and trailing whitespace.

1 Like

thank you @petro

i have modified my script based on your tips and it is almost where i want it.
I still need some help with that, but i think i should start a new topic for that.

i’ll mark this topic as solved.

thanks @123 and @petro