Iterate through gruop to call an action per entity

Since its getting colder outside I want to implement an automation that will controll my covers based on the temperature. The Idea is to open only closed covers and set them to a particular level (10%) if temperature drops below 1°C.

But how can I iterate through my group.all_covers and only set the level on those that are currently closed? I didn’t find any hints (besides some service_template stuff that doens’t seem to fit).

So what I need is something like (in semi-pseudo code):

trigger:
  platform: numeric_state
  entity_id: sensor.temperature_outside
  below: 1
action:
   for entity in group.all_covers
     if entity.state = 'closed' 
       entity.level = 10%
     endif
   endfor

How can I do this?

1 Like

Maybe this helps you:

Greetings

But I’m not using a template to display something. I want it to be in automation. Or do I get templates wrong?

?!
Well, all this stuff that goes beyond simple calls or conditions need to be handled in templates or scripts (at least this is how I understand that).
A for-loop is such “complicated” stuff as it is a conditional loop with a variety of actions in it.

Did you take a look into my link above? It seems to me as if it covers your usecase exactly (even the title is almost the same).

ok, doing some advanced stuff. how can I make the same for conditions?
So condition: and, then iterate trough all entities of a group and return true if a particular state?

ok so this works perfectly for doing stuff like sending messages. But I’m still stucked when it comes to doing a service call for a list of devices that have a particular state.

Lets say, I have an automation that checks if temperature dropped below 0°C and if, open only fully closed covers to 10%. If the cover is open (everything higher then 15%), then ignore it.

I couldn’t find a way to call a service for all devices that match a given condition. Any ideas?

Hi,
well, I personally (without being a pro in this) do not have any “conditions” in that specific automation. I only have the trigger.
Then the python script gets called and I do all the conditional actions/case differentiation in the script.
Have a look here:

There might be better ways for sure, but this works for me.
Sidenote: I had a logical bug in the script, so do not use it by copy/paste. But the basic functionality of how I handle the conditions in the script should be very clear now.

1 Like

Thank you very much. This helps me as now I have an idea how to tackle it down with pure python. I’ll post my script once I had time to write and test it.

Ok, great. Posting your own script/code would be appreciated, as it might show people a wider variety of solutions, as this “problem” pops up regularly from time to time.

I was able to accomplish this by using a repeat action and calling the script for each entity in the group by using the index. Here is an example of this.

  action:
  - repeat:
      count: '{{ expand("group.auto_dim_lights") | length() }}'
      sequence:
      - service: script.set_light_brightness_if_on
        data:
          light_entity: '{{ expand("group.auto_dim_lights")[repeat.index].entity_id }}'

1 Like