#Running a dynamic list of parallel actions programmatically?

1 messages · Page 1 of 1 (latest)

bleak grotto
#

Hi! Before I spend time trying to build the right structures, I wanted to check if it's possible: can you run a dynamic list of actions in parallel?

Using lights as a simple example, if I had a script that took a parameter "lights" that was a list of entities, e.g., ["light.living_room", "light.kitchen"], and I wanted to run a block that looked like this, is it possible to do that on the fly so that it works with different length lists?

    - action: light.turn_on
      target:
        entity_id: light.living_room
      data: {}
    - action: light.turn_on
      target:
        entity_id: light.kitchen
      data: {}```

Totally open to solutions other than a parallel: block; that just seemed the most straightforward.  (I'm also aware that this is probably not really useful or necessary for turning on lights; I'm doing something more complicated but harder to explain, so I'm using lights as an example.)

Thanks!
bleak grotto
#

I wrote a terribly janky solution that has a parallel: block consisting of multiple blocks that essentially say: if at least 1 light, turn on light 0; if at least 2 lights, turn on light 1. I made more than the maximum number I'll need to set. It appears to work, though I really hope there's a better solution. Partial:

      - if:
          - condition: template
            value_template: "{{ lights | length > 0 }}"
        then:
          - action: light.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: "{{ lights[0] }}"```
#

Another option: fire events to trigger the turn_on and have the event handling automation set to parallel. That might be cleaner if event handling is truly asynchronous (i.e., the event firing commandalways immediately returns).

bleak grotto
#

Initial testing suggests the event approach works, though it's a bit indirect. Still looking for ideas/improvements/confirmation this is the best i can do. Thanks!

deep lodge
#

Why are you so eager to do it parallel? Turning on a light should not take any time at all. So even sequential should be fine. Only on slower networks like Zigbee you may see some light turning on befor the other. But that's more in the network and will probaby also happen with parallel. A zigbee group would be a better solution for it. And for other stuff, a regular group will do.