hi - i’ve been losing my mind trying to create an automation/script that can loop through a dynamic list of entities and run the same service call on all of them. i’ve found several threads explaining the process but i just cannot get past one issue that i assume is something i’m doing wrong with my syntax. hoping someone can help out!
the code is below, but the takeaway is: no matter what i do, the automation seems to be treating my entity_id list as a string and, therefore, list[0] shows up as just the opening “[”
i’ve tried a number of variations - including reading from a sensor and just directly templating within the automation. same results either way. i’ve also tried defining the variable as a comma separated string and then converting to a list item – same thing.
here’s my sensor yaml, which works perfectly:
- platform: template
sensors:
schedule_entities:
unique_id: schedule_entities
friendly_name: "Schedule Entities"
value_template: "{{ states.switch | selectattr('object_id', 'match', 'schedule_') | map(attribute='entity_id') | list | count }}"
attribute_templates:
schedules: >
{{ states.switch | selectattr('object_id', 'match', 'schedule_') | map(attribute='entity_id') | list | join(', ') }}
schedule_list: >
{{ states.switch | selectattr('object_id', 'match', 'schedule_') | map(attribute='entity_id') | list }}
and here’s the relevant part of my automation yaml:
- variables:
list: "{{ state_attr('sensor.schedule_entities', 'schedules_list') }}"
counter: "{{ states('sensor.schedule_entities') | int(0) }}"
- repeat:
sequence:
- service: homeassistant.turn_off
target:
entity_id: |
{{ list[repeat.index - 1] }}
count: "{{ counter | int(0) }}"
enabled: true
the counter is working correctly, and the list variable is correctly pulling from the sensor, it’s just the individual list elements that i can’t figure out how to pull. like i said - i’ve tried a million different variations, but every time, the automation just treats each individual character as a list item.(“[”, “{” or “s” - depending on the exact variation i am using). i’ve also tried the alternate “from_json” approach – same issue. i’ve also tried quoting the list item index number – both directly/statically – {{ list[‘0’] }} and as an actual variable {{ list[‘repeat.index -1’] }} – also doesn’t help.
every single time it just keeps spitting out a 1 character long entity id. i’ve spent hours spinning my tires on this. anyone have any idea what i’m doing wrong?
last thing – i did notice that if i statically define the variable as a line-by-line list of entities it works, but even statically definining the list using the brackets format does NOT work:
so this works:
- variables:
list:
- entity_a
- entity_b
but this doesn’t work:
- variables:
list: ' [ "entity_a", "entity_b" ]'
the threads i’ve been looking at are all several years old – is there some reason the bracket array format doesn’t work in an automation even though it seems to work to create the underlying sensor itself?
in any case, those are just examples for troubleshooting purpose – but the hyphen-separated list isn’t actually an option because the whole point of this is to create a dynamic list.
clearly i’m losing my mind trying to figure this out. would appreciate any guidance anyone might have!