What's the quickest way to set 60 select entities at once?

I’ve got 60+ lights with the startup behavior set to “On” and I want them all set to PreviousValue (because having them all come on when there’s a power cut is mad)

What’s the quickest and easiest way to do it as a one off action?

1 Like

If that doesn’t work for you, please explain, perhaps with code for one light, what you’re trying to do.

Every light has an entity called select.blah_start_up_behavior which can be any of On,Off,PreviousValue. They default to “On”

I want to set them all to “PreviousValue”. I only want to mass change them once. Is it possible to do this from a script using a wildcard?

Create a script that uses a Repeat for each action which contains your select.select_option service call. You can use a template to create the list of entities.

sequence:
  - repeat:
      for_each: >
        {{ states.select | selectattr('entity_id', 'search', 'start_up_behavior' )
        | map(attribute='entity_id')| list }}
    sequence:
      - service: select.select_option
        target:
          entity_id: "{{ repeat.item }}"
        data:
          option: PreviousValue

Note that this will act on every select entity that has “start_up_behavior” in its entity ID. If it’s possible that there are entities that match those criteria that you don’t want changed, it might be worthwhile to paste the template into the Developer Tools > Template editor to check the list. If you find entities you want to leave alone you can remove them from the list by adding a reject list:

{{ states.select | selectattr('entity_id', 'search', 'start_up_behavior' )
| map(attribute='entity_id') 
| reject('in', ['select.example_1_start_up_behavior', 'select.example_2_start_up_behavior'])
| list }}

Awesome, many thanks to both of you. I’m just getting started learning about templates so thanks for the code.

This is my script that did the job in the end.


alias: Change Device Startup Behavior
sequence:
  - service: select.select_option
    data:
      option: PreviousValue
    target:
      entity_id: >
        {{ states.select|selectattr('entity_id','contains','start_up_behavior')|map(attribute='entity_id')|list }}
mode: single

Are there advantages to using the repeat method?

Nope, it’s just what came to mind first for me. If you have multiple services you wanted to call or a more complicated action to perform, the repeat might be necessary… but for this relatively simple, single-use script the way you did it is probably better.

1 Like