Hi,
I’m trying to write a script that will perform an action in parallel for a dynamic list of entities.
So far I couldn’t get the for_each to work using the documentation and reading posts.
Can it be done? Any advice?
area_on:
alias: Switch area on
mode: single
fields:
area:
description: Name of the area
example: woonkamer or 'woonkamer,keuken'
variables:
entities: "{{ area.split(',') | map('area_entities') | list | sum(start=[]) }}"
lights: "{{ expand(states.light)|selectattr('entity_id', 'in', entities)|selectattr('state','ne','unavailable')|map(attribute='entity_id')|list }}"
media_player: "{{ expand(states.media_player)|selectattr('entity_id', 'in', entities)|selectattr('state','ne','unavailable')|selectattr('attributes.device_class','eq','speaker')|map(attribute='entity_id')|first }}"
sequence:
- parallel:
- repeat:
for_each: "{{ lights }}"
sequence:
- service: light.turn_on
target:
entity_id: "{{ repeat.item }}"
- if:
- condition: template
value_template: "{{ states(media_player) != 'playing' }}"
then:
- service: script.sonos_play
data:
player: "{{ media_player }}"