Variable in wait template

So I want to use automations to call a script that temporarily increases the volume, then sends a text-to-speech message to a specified google home. I’ve almost got it, but I’m caught up on the wait template. Basically the script isn’t waiting for the message to finish playing before reducing the volume.

Here’s the script:

'1540512309772':
  alias: Send Alert - Local Media Player
  sequence:
  - data_template:
      entity_id: >
        {{ media_player_id }}
      volume_level: '0.5'
    service: media_player.volume_set
  - data_template:
      entity_id: >
        {{ media_player_id }}
      message: '{{ message }}'
    service: tts.google_say
  - delay: '1'
  - wait_template: >
      {{ not is_state({{ media_player_id }}, "playing") }}
  - data_template:
      entity_id: >
        {{ media_player_id }}
      volume_level: '0.25'
    service: media_player.volume_set

I can call this with:

- data:
    media_player_id: media_player.kitchen
    message: test message
service: script.1540512309772

Everything works except for the wait template portion. I know I’m missing something with passing the media_player_id variable but I haven’t been able to figure it out. As a work around, I’ve just commented out the broken portion and increased the delay, but that isn’t really ideal. Hopefully someone here can point me in the right direction.

Also, bonus points if someone has a clue on how to restore the original volume level rather than always setting it back to 0.25.

Thanks!

Remove the curly braces from around media_player_id.

  - wait_template: >
      {{ not is_state(media_player_id, "playing") }}

And that did the trick! I figured it was something simple, but I was banging my head trying to figure it out. Appreciate the help!

I haven’t done this but an attribute is volume_level so I guess you could save it to an input_number and restore it from there.

@ThePicklenat0r Don’t even need to do that. Just add this to the call service and script:

- data_template:
    media_player_id: media_player.kitchen
    message: test message
    volume_level: "{{ state_attr('media_player.kitchen','volume_level') }}"
service: script.1540512309772
'1540512309772':
  alias: Send Alert - Local Media Player
  sequence:
  - data_template:
      entity_id: >
        {{ media_player_id }}
      volume_level: '0.5'
    service: media_player.volume_set
  - data_template:
      entity_id: >
        {{ media_player_id }}
      message: '{{ message }}'
    service: tts.google_say
  - delay: '1'
  - wait_template: >
      {{ not is_state(media_player_id), "playing") }}
  - data_template:
      entity_id: >
        {{ media_player_id }}
      volume_level: "{{ volume_level }}"
    service: media_player.volume_set
2 Likes

And bonus points to you. That worked!

Thanks everyone for your help