Script preference syntax

hi,

have several scripts switching on/off switches and lights. Example, working just fine:

home_theater:
  alias: Home theater
  sequence:
    - service: switch.turn_on
      entity_id: switch.sw_audio_auditorium_cl
    - service: switch.turn_on
      entity_id: switch.sw_tv_auditorium_cl
    - service: switch.turn_off
      entity_id: switch.auditorium_motion_sensor_switch
    - service: light.turn_off
      data:
        entity_id: light.buffet
    - service: light.turn_off
      data:
        entity_id: light.dining_table
    - service: light.turn_on
      data:
        entity_id: light.kist
    - service: light.turn_off
      data:
        entity_id: light.cabinet
    - service: light.turn_on
      data:
        entity_id: light.home_theater
    - service: light.turn_on
      data:
        entity_id: light.outside
    - service: light.turn_on
      data:
        entity_id: light.lounge_chair_long
    - service: light.turn_off
      data:
        entity_id: light.bureau
    - service: light.turn_off
      data:
        entity_id: light.inside

because of several automations that use the same entities, but list them in an ordered list, I thought id give it a try in the script like that too:

home_theater:
  alias: Home theater
  sequence:
    - service: switch.turn_on
      entity_id: 
        - switch.sw_audio_auditorium_cl
        - switch.sw_tv_auditorium_cl
    - service: switch.turn_off
      entity_id: switch.auditorium_motion_sensor_switch
    - service: light.turn_off
      data:
        entity_id: 
          - light.buffet
          - light.dining_table
          - light.cabinet
          - light.inside
          - light.bureau
    - service: light.turn_on
      data:
        entity_id: 
          - light.kist
          - light.home_theater
          - light.outside
          - light.lounge_chair_long

And, admittedly to my surprise, this works just fine too… :wink:

Was wondering because of this experience, if there are any reasons to use either of both styles of syntax, and what the preference syntax could be. The latter is easier on the eye, and hence easier to debug/maintain, but maybe there are execution aspects that would lead otherwise? I know these sets could also be managed through calling scenes, but having a nicely formatted script like this could skip the need for dedicated ‘extra’ scenes.

Cheers,
Marius

Besides being easier to read & maintain, I think the latter is more efficient as well. You could even do this I believe:

home_theater:
  alias: Home theater
  sequence:
    - service: homeassistant.turn_on
      entity_id:
        - switch.sw_audio_auditorium_cl
        - switch.sw_tv_auditorium_cl
        - light.kist
        - light.home_theater
        - light.outside
        - light.lounge_chair_long
    - service: homeassistant.turn_off
      entity_id:
        - switch.auditorium_motion_sensor_switch
        - light.buffet
        - light.dining_table
        - light.cabinet
        - light.inside
        - light.bureau
3 Likes

Nice!, even better, thanks! Will try at once. Always better to have less do the same, and better :wink:
could i also add the input_boolean and (sub)scripts in the list? I already have several groups i turn on/off with the homeassistant call, so those could be added as well? Making it so much easier , why isn’t this documented like this?Scripts - Home Assistant

I think that anything that can be turned on or off can be turned on or off with the homeassistant turn_on and turn_off services. This should apply to input_boolean, and groups as well. In fact, when you want to turn a mixed group of things on or off, the homeassistant services are the way to go. But I’m pretty new to HA, so I don’t speak with authority (or much experience. :slight_smile:)

FWIW, I found out about using homeassistant.turn_on and homeassistant.turn_off at the bottom of this page:

And there’s also a bit here: