For_each - create a list for single or multiple entries passed in as parameter

I’m using for_each to iterate over a list of media_players which I feed into the script as a parameter. I needed to work around an issue in not being able to use the same mechanism as I would use for other services where you can pass in either a single entity or a comma separated list of entity_ids e.g.

entity_id: media_player.my_player_1 or
entity_id: media_player.my_player_1, media_player.my_player_2

The second case doesn’t work with for_each as the parameter is a string, not a list.
If I bracket the parameter to be a list

entity_id: [ media_player.my_player_1, media_player.my_player_2 ]

when calling the script then it works.

Here’s how the code looks like, the workaround is to create a group on the fly - which accepts either a single or multiple entity_ids and then use the groups entity_id state_attribute to iterate over.

media_player_speak:
  sequence:
  - service: group.set
    data:
      object_id: media_player_temp_group
      entities: "{{ entity_id }}" # entity_id is the parameter passed intothe script

  - repeat:
      for_each: "{{ state_attr('group.media_player_temp_group', 'entity_id')|list }}"
      #  do something with repeat.item

  - service: group.remove
    data:
    object_id: media_player_tmp_group

# the calling sequence looks like this:

- service: script.media_player_speak
  data: 
    entity_id: media_player.my_player_1, media_player.my_player_2
    message: "hello"

I wonder if this can be done more elegantly - w/o the need of the temporary group.

change to

- service: script.media_player_speak
  data: 
    entity_id:
    - media_player.my_player_1
    - media_player.my_player_2
    message: "hello"

or,

- repeat:
    for_each: "{{ entity_id.split(', ') if entity_id is string else entity_id }}"
2 Likes

thank you very much. Your solution, the second way with the split (the first one I had known but wanted it to function like w/ other services) is great and make we wonder why I didn’t get to that simple solution myself (now that I see it :wink: ).