How to iterate through a list of entities and call service on each entity?

Hi!

even if this is a very basic question, I was not able to find a solution.

I want to write an automation, that loops over a list of entities and then makes a service call on every single entity. For clarification: I do not want to do one service call which takes a list of entities, I really want to do a service call for each entity in the list. Could anyone please provide a simple example for that?

Greetings
GW

You will need to explain what you mean by:

Most automation triggers in HA are event-based, not based on continuous polling of end devices or entities.

Why?

So okay, I have the following case:

My lights are organized in groups (“normal” groups, not light groups). I control every group with a zigbee remote. Therefore I have an automation, to handle the button presses and call the light.turn_on / light.turn_off services. This works fine so far, also for increasing / decreasing the brightness.

But: Everytime I decrease the brightness and the brightness value for one light becomes 0, the light turns off. This is not the behaviour, I want to have. Instead I want the light to stay on, but at a minimum brightness value (lets say 10%). Therefore I want to write a loop inside my automation, which expands the group, iterates over all the light entities and sets the brightness value for every single light and, if neccessary limits the decrease, to not fall below my minimum brightness value.

1 Like

You’d do that with a repeat, however you should realize that your lights will now perform the actions in order and you’ll notice this in real time.

Thank you for your answer. After reading a bit, now I have an idea, how it would work. Unfortunately, the noticeable delay, @petro described, is something I would prevent, if possible.

Are there maybe any other suggestions, how I could limit the mimimum brightness to a certain value above 0?

There’s no other way. You perform the action all at once, or individually.

You could filter the list out when the lights are at or below 10 already.

Thank you for the answer. The filtering idea is good, I think. I will try this!

{{ expand('group.xyz') | selectattr('state','eq','on') | rejectattr('attributes.brightness', '=<', 25) | map(attribute='entity_id') | list }}

EDIT: This will only work on light domains. If your group has anyother domain in it, it will error.

{{ expand('group.xyz') | selectattr('state','eq','on') | selectattr('domain','eq','light') | rejectattr('attributes.brightness', '=<', 25) | map(attribute='entity_id') | list }}
3 Likes

also, keep in mind that brightness is 0 to 255, so 25 is roughly 10%

That’s not true. The entity_id attribute is always a list. You can omit the | list. There is a chance at startup that attributes are set to None, which is jinja’s variation of null or empty.

Secondly, climate.set_hvac_mode can work on multiple climate devices. No for loop needed.

        - action: climate.set_hvac_mode
          target:
            entity_id: "{{ state_attr('group.climates','entity_id') }}"