Best way to turn on multiple lights one-by-one

Have you tried to call the turn off service with all your lights IDs?

I think this way, the service switch off the lights one by one

I use this to turn on/off all lights in a room, and they do it in parallel

Nick is right. HA does not do this sort of thing well. You would be better off using Appdaemon or a python script.

Hi,
I would do it using node-red.

  1. get all entities matching a pattern
  2. a loop based on those names
  • turn on one light
  • delay x ms
  • continue

Added bonus if you add some more lights using the same name pattern or within the same group they will be added to the loop without further effort.

I have migrated all my automations to node red and I love it!!

GV

1 Like

I would think you would be able to leverage the for loop as shown in the template development section of UI, run it as a script or a scene

I mean it can be done. You could adapt this:

@Terp
Here’s a very basic python_script that iterates through a group of lights and turns on each one in sequence with a delay of 15 seconds between each activation. Modify it to suit your needs:

sequence.py

items = hass.states.get('group.my_lights').attributes['entity_id']

for item in items:
  service_data = {'entity_id':'{}'.format(item)}
  hass.services.call('light', 'turn_on', service_data, False)    
  time.sleep(15)

The order the lights are activated is based on the order they are listed within the group.


EDIT

The advantage of using this python_script, as opposed to copy-pasting code 17 times in a script, is less maintenance. If you ever add more lights to the group they will be automatically handled by the python_script.

3 Likes

Love this thread. I find myself falling into this all the time sadly.

:thinking: How can I turn a 10 second problem into a 10 minute solution?”

4 Likes

I’ve said it before and I’ll likely say it again, compact configuration does not necessarily equate to easily understandable and maintainable configuration and probably has little effect on execution efficiency (zero facts available to back that last bit up). I do understand the fascination with simplicty compaction though.

I love this table.

e.g.

Q: How often do I switch lights on and off?
A: Between 50 and 5 times a day.

Q: how much time would I save by automating it?
A: 1 Second.

Therefore I should spend no more than 2 hours to 1 day attempting to automate it to save time over 5 years.

2 Likes

“I would have to agree Captain Kirk” says Mr Spock
But… Some people have built the most beautiful objects, because they can, they take pride in it, they take pleasure from knowing that behind this cover plate (that no-one will take off in the next 20years) is an (insert object here) that has been crafted to meet and often exceed its requirement.
Also, people like to do puzzles, people have hobbies and sometimes they intersect. Should I insert a Venn diagram here ??? ; - )))

When I said I “understood” the fascination with it, I meant I suffer from it and can empathise with OP, Mr Spock.
Also. I’m. Not. Capt. Kirk.

That is quite an elegant solution @123. Just out of interest, is it possible to do it anywhere as elegantly in jinja (I do not require proof just y/n if you have the time)?

Reminds me of my French friend who said she prefers thinking in English as it is “free-er”.

If you mean can I create an equivalent solution as a YAML automation, with an appropriate template, the answer is no. Maybe it’s possible but, over the 11 months I’ve been hanging around this forum, I’ve never seen it done.

The issue is you can’t iterate (loop) in a YAML automation. It lets you define a sequence of steps, where each step can be tweaked with a template, but you can’t loop through those steps.

1 Like

So just like speaking and thinking (if you are fortunate enough to be multi-lingual) - pick the programming language for the task at hand.

Latin is much preferred by lawyers due to it’s formal structure, I heard.

I thought i would just pop in here… I have 200 lines of code for turning on my 19 lights of IKEA… Turn on … delay… turn on… delay and it works brilliant. :slight_smile:

Even though the automation is long, it is still very easy to see exactly what it does :smile:

1 Like

But loops are possible. e.g.

Iterate through a Group

NM. I see what you are saying. Iteration is not equivalent to a “while true do” loop.

YAML is not Turing Complete. Python is.

I have similar automations that I could combine at the expense of mere mortal (me) understanding, but this is not the way to go for maintainability.

Anecdotally, there’s bugger all gain to be made other than the satisfaction of solving the puzzle. The hardware we run HA on is more than capable of a few inefficiencies, even if configuration length equated to efficiency (I’m guessing it does not, much).

It’s your home automation system. Write it so you can understand it and maintain it.

I’ll be the first to admit that out of necessity I have been guilty of using provided configuration I do not fully understand. But it’s rare.

The worst thing about 5500+ lines of automations is… bugger all. Ctrl+F in most editors will get you there.

I think ‘most’ of us here suffer from it (to a greater or lesser degree or we wouldn’t be obsessing about when it’s appropriate to “turn a light off”). A good coder is hard to find. They need the right level of OCD, but not tooooo much as some of them have to turn the light off 23 times before they leave a room.
"BUT testing is a valuable use of time !!! " ; - ))))))))))))))))
Cheers

Only 10 minutes ! … You’re not trying hard enough ! ; - ))))))))))

1 Like

No. Thats just so they can charge you $600 per hour !