Pass variables from automation to script

I’m trying to combine 8 automations into 1 automation and 1 script.

I can get the switch.plug5_socket_1 to turn on using the state of the entity_id but I’d like to be able to pass the trigger.to_state.state successfully and use that to set and “on” and “off” condition and then match the associated input boolean to the devices that need to be turned on… does that make sense?

Here is the automation

alias: Airplay
description: ""
trigger:
  - platform: state
    entity_id:
      - input_boolean.airplaygreatroom
      - input_boolean.airplaymasterbedroom
      - input_boolean.airplaybathroom
      - input_boolean.airplayutiltyroom
    from: "off"
    to: "on"
    for:
      hours: 0
      minutes: 0
      seconds: 1
condition: []
action:
  - service: script.airplaymusic
    data:
      room: "{{ trigger.entity_id }}"
      pwrstate: "{{ trigger.to_state.state }}"
mode: single

here is the script

alias: AirplayMusic
sequence:
  - choose:
      - conditions:
          - condition: state
            entity_id: input_boolean.airplayutiltyroom
            state: "on"
        sequence:
          - service: switch.turn_on
            data: {}
            target:
              entity_id:
                - switch.plug5_socket_1
      - conditions:
          - condition: template
            value_template: "\"{{ states('trigger.to_state.state' == 'on') }}\""
        sequence:
          - service: light.turn_on
            target:
              entity_id:
                - light.office_lights_2
            data: {}

The following is non-sense, in at least 5 ways:

value_template: "\"{{ states('trigger.to_state.state' == 'on') }}\""

I would say you might want something like:

value_template: "{{ pwrstate == 'on' }}"

But the entire script seems to boil down to “If input_boolean.airplayutiltyroom is on, turn on plug5_socket, otherwise turn on office lights 2.”

It is not clear from your description and example what purpose the room or pwrstate variables are actually serving.

Sorry, I am terrible at explaining it in writing.
So the desired scenario would be to get the automation to pass the room and the trigger.to_state.state to the script.
The script could then utilize the variables to determine the proper sequence “on” or “off” and each of those paths would have a sequence to be executed depending on the room.
In my example, I was able to use the entity_id state and if that is all I can get, I could create 4 sequences (1 for each room) to turn them on and then create 4 more one to turn them each off.

so for each room, I have an automation nearly identical to this:

alias: AirplayUtilityOn
sequence:
  - service: switch.turn_on
    data: {}
    target:
      entity_id:
        - switch.plug5_socket_1
  - delay:
      hours: 0
      minutes: 0
      seconds: 1
      milliseconds: 0
  - service: media_player.turn_on
    data: {}
    target:
      entity_id: media_player.zone_5
  - delay:
      hours: 0
      minutes: 0
      seconds: 1
      milliseconds: 0
  - service: media_player.select_source
    data:
      source: Airplay2
    target:
      entity_id: media_player.zone_5
  - delay:
      hours: 0
      minutes: 0
      seconds: 1
      milliseconds: 0
  - service: media_player.volume_set
    data:
      volume_level: 0.68
    target:
      entity_id: media_player.zone_5
  - delay:
      hours: 3
      minutes: 30
      seconds: 0
      milliseconds: 0
  - service: script.utilityroomspeakersoff
    data: {}
mode: single
icon: mdi:music

the differences are the media_player.zones
but turning on the switch.plug1_socket_1 could be an action that occurs one time if the pwrstate = ‘on’
your suggestion did work.
I made some progress and am able to have the two pwrstate sequences.
any additional recommendations to make the script more efficient?

It’s definitely possible, but you need to share with us how you expect to link the input booleans in the automation to the media players and switches/lights in the script.

Do your entity IDs follow a strict naming convention? Or will you need to set up a mapping?

For example, if the pattern is switch.plugX_socket_1 always goes with media_player.zone_X, you can use a couple variables to take the value of room from the earlier automation:

variables:
  room_map:
    input_boolean.airplayutiltyroom: "5"
    input_boolean.airplaygreatroom:
    input_boolean.airplaymasterbedroom:
    input_boolean.airplaybathroom:
  num: "{{ room_map.get(room) }}"
sequence:
  - service: switch.turn_on
    data: {}
    target:
      entity_id:
        - switch.plug{{num}}_socket_1
  - delay: 1
  - service: media_player.turn_on
    data: {}
    target:
      entity_id: media_player.zone_{{num}}
  - delay: 1
  - service: media_player.select_source
    data:
      source: Airplay2
    target:
      entity_id: media_player.zone_{{num}}
  - delay: 1
  - service: media_player.volume_set
    data:
      volume_level: 0.68
    target:
      entity_id: media_player.zone_{{num}}
  - delay:
      hours: 3
      minutes: 30
      seconds: 0
      milliseconds: 0
  - service: script.utilityroomspeakersoff
    data: {}
mode: single
icon: mdi:music

Though, I suggest not using the delay at the end. Set up a second automation to handle that. Delays do not survive reload or restart, so it’s best to avoid them for anything longer than a few minutes.

okay, so with your help I have a start on what I am wanting to do.
There is a “on” sequence" and and “off” sequence and each one was going to have a separate sequence for each room before I saw your room mapping example…

alias: AirplayMusic
sequence:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ pwrstate == 'on' }}"
        sequence:
          - choose:
              - conditions:
                  - condition: template
                    value_template: "{{ room == 'input_boolean.airplayutiltyroom' }}"
                sequence:
                  - service: media_player.turn_on
                    data: {}
                    target:
                      entity_id: media_player.zone_5
                    enabled: true
                  - delay:
                      hours: 0
                      minutes: 0
                      seconds: 1
                      milliseconds: 0
                    enabled: true
                  - service: media_player.select_source
                    data:
                      source: Airplay2
                    target:
                      entity_id: media_player.zone_5
                    enabled: true
                  - delay:
                      hours: 0
                      minutes: 0
                      seconds: 1
                      milliseconds: 0
                    enabled: true
                  - service: media_player.volume_set
                    data:
                      volume_level: 0.68
                    target:
                      entity_id: media_player.zone_5
                    enabled: true
                alias: input_boolean.airplayutiltyroom
        alias: pwrstate == 'on'
      - conditions:
          - condition: template
            value_template: "{{ pwrstate == 'off' }}"
        sequence:
          - service: switch.turn_off
            target:
              entity_id:
                - switch.plug5_socket_1
            data: {}
          - choose:
              - conditions:
                  - condition: template
                    value_template: "{{ room == 'input_boolean.airplayutiltyroom' }}"
                sequence:
                  - service: media_player.volume_set
                    data:
                      volume_level: 0.68
                    target:
                      entity_id: media_player.zone_5
                    enabled: true
                  - service: media_player.turn_off
                    target:
                      entity_id:
                        - media_player.zone_5
                    data: {}
                alias: input_boolean.airplayutiltyroom
        alias: pwrstate == 'off'
  - delay:
      hours: 3
      minutes: 30
      seconds: 0
      milliseconds: 0
    enabled: false
mode: single
icon: mdi:music
fields:
  room:
    selector:
      text: {}
    name: room
    required: true
  pwrstate:
    selector:
      text: {}
    name: pwrstate
    required: true

unfortunately, I did not use a strict naming convention so I will have more work.
your example is amazing, though. It clears some things up.
and the plug powers on an old airplay apple router I had lying around. if I left it on all the time it became unreliable so I put a plug on it and automate turning it on anytime I wanna play music in the house so that is why it gets turned on one time in the script above.
The mapping in your example is fantastic. I think that is the key for me.

so my mapping is going to be something like this:

variables:
  room_map:
    input_boolean.airplayutiltyroom: "zone_5"
    input_boolean.airplaygreatroom:
    input_boolean.airplaymasterbedroom: "masterbr"
    input_boolean.airplaybathroom: "zone_4"
  num: "{{ room_map.get(room) }}"

but… the greatroom is two zones… how is that handled?
- media_player.greatroom
- media_player.kitchen_2

In that case you may be better off setting up a more complex mapping

variables:
  room_map:
    input_boolean.airplayutiltyroom:
      media: media_player.zone_5
      switch: switch.plug5_socket_1
    input_boolean.airplaygreatroom:
      media:
        - media_player.greatroom
        - media_player.kitchen_2
      switch: 
    input_boolean.airplaymasterbedroom::
      media: media_player.zone_masterbr
      switch: switch.plugmasterbr_socket_1
    input_boolean.airplaybathroom:
      media: media_player.zone_4
      switch: switch.plug4_socket_1
  player: "{{ room_map.get(room).media }}"
  plug: "{{ room_map.get(room).switch }}"

Then you would use plug and player in the service calls like:

- service: media_player.turn_on
  data: {}
  target:
    entity_id: "{{ player }}"
- service: switch.turn_off
  target:
    entity_id: "{{ plug }}"

Be aware, if you are keeping the delay at the end, you must change the mode to parallel, or the script will not be fit for purpose because it will be locked out from activating another room while it waits for 3.5 hours first the first run to finish.

yeah, the delay was to ensure the amp wasn’t left on for days, I will remove that and set something to check if left on and turn off at night. thanks for the information…
I am terrible at yaml and not sure where in the script the variables go. have bene fumbling with it and no luck.