Is it possible to define a variable in an automation for a condition template's state inside triggered script

my aim is to make a script for making announcements on my various types of speakers, so some speakers use notify.alexa and some use google tts.
the script handles routine for making the announcement like pause music, adjust volume, resume, etc, determine whether the speaker uses alexa or google tts, announce in occupied rooms or wait until there is someone in the room before announcing but doesn’t announce if the task is already completed.
in my automation i have the phrases used for the announcement and i only need to provide the speaker name, volume (because different speakers have different loudness) and the sensor that determines when the task is completed.

i only need 1 script for all the various automations.

with a lot of help from @petro and @123 it is almost where i want it, but there is another thing that i can’t figure out - defining the desired sensor’s state in the automation so that the script can calculate the condition template.
at the moment i can define the variable for the binary sensor, but in my script the state is hardcoded to be ' on ' because i can’t figure out how to use a variable for it. i want to do something like this:

- condition: template
  value_template: '{{ is_state(check_sensor, ''{{ check_condition }}'' ) }}'

it doesn’t work well if i hardcode it as '{{ is_state(check_sensor, ''on'' ) }}' because sometimes i want a different value, like unavailable, off or just some other type of value.

the check_sensor part works and but if i use check_condition won’t work.

if someone could be so kind, have a look and see where i might have gone wrong?

here’s my automation: (i have cut short the number of speaker devices to reduce the lines for this long post)

alias: Reminder Gate Left Open
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.zone_gate_open
    from: 'off'
    to: 'on'
    for:
      hours: 0
      minutes: 10
      seconds: 0
condition: []
action:
  - service: script.announce_with_voice
    data:
      speaker_device: media_player.echo_show
      announce_volume: 0.35
      announcement_msg: '{{ phrases }}'
      check_sensor: '{{ wait_sensor }}'
      check_condition: '{{ wait_condition }}'
    enabled: true
  - service: script.announce_with_voice
    data:
      speaker_device: media_player.kitchen_2
      announce_volume: 0.35
      announcement_msg: '{{ phrases }}'
      check_sensor: '{{ wait_sensor }}'
      check_condition: '{{ wait_condition }}'
    enabled: true
variables:
  phrases: |
    {{ (
      "Did you forget to close the gate?",
      "Someone left the gate open",
      "Somebody forgot to close the gate...",
      "The gate is still open",
    ) | random }}
  wait_sensor: binary_sensor.zone_gate_open
  wait_condition: "on"
mode: single

and here is the script that all the automations would use: (much thanks to @petro for the inspiration!)

alias: Announce with Voice
variables:
  old_volume: '{{ state_attr(speaker_device,''volume_level'') | float }}'
  mediaplayer_State: '{{ states(speaker_device) }}'
  config_room:
    media_player.echo_show: input_boolean.room_occupancy_study
    media_player.kitchen_2: input_boolean.room_occupancy_kitchen
  config_service:
    media_player.echo_show: notify.alexa_media
    media_player.kitchen_2: tts.google_cloud_say
  boolean: |
    {{ config_room.get(speaker_device) }}
  speaker_service: |
    {{ config_service.get(speaker_device) }}
sequence:
  - wait_template: |
      {{ is_state(boolean, 'on') }}
    timeout: '00:30:00'
    continue_on_timeout: false
  - condition: template
    value_template: '{{ is_state(check_sensor, ''on'' ) }}'
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ speaker_service == ''notify.alexa_media'' }}'
        sequence:
          - 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 }}'
      - conditions:
          - condition: template
            value_template: '{{ speaker_service == ''tts.google_cloud_say'' }}'
        sequence:
          - 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:
              entity_id: '{{ speaker_device }}'
              message: '{{ announcement_msg }}'
          - delay:
              hours: 0
              minutes: 0
              seconds: 1
              milliseconds: 0
          - delay:
              hours: 0
              minutes: 0
              seconds: '{{ state_attr(speaker_device,''media_duration'') | float }}'
              milliseconds: 0
          - 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: 3
              milliseconds: 0
          - service: media_player.media_play
            target:
              entity_id: '{{ speaker_device }}'
    default:
      - stop: ''
mode: parallel
max: 5

i would really love to get more pointers from all you gurus.

i feel like there could be many ways to optimize all this code and i feel like i have one down the rabbit hole with this one. like there is so much repetition of codes. like within all the different automations the part where the announcement script is triggered - i have 9 speakers so all the automations have those same action sequences X 9.

Have you already tried

value_template: '{{ is_state(check_sensor, check_condition ) }}'

?

1 Like

whatttttt…
it literally took you 1 look and 1 try to fix this!
i can’t believe i didn’t try that… i swear i did…

i guess i still don’t really understand when not to use those curly bracket thingies, when to use single quote marks, etc… i have real a long way to go…

It’s because you cannot nest templates.

i surprised myself that i actually know what that means!

thank you!