Automate turning on random lights

I have around 60 Philips Hue color bulbs controlled via 2 Hue Hubs through HA.

All 60 lights are installed in rice paper lamps of varying sizes in a large area visible from the street. All bulbs are named “Hue_1_xx” (xx being a number from 1-60).
I would like a couple of bulbs on at night so the area is a little interesting even when we are closed to the public.

I would like to make an automation turning on 3-5 random light bulbs and have tried adding this through an automation.

I have seen people using something like "{{ range(1,60) | random | int }}" for timed delays of random length in automations, but how would I go about adding this number to the names of my bulbs?

I imagine a line a bit like this in the actions part of an automation:

service: light.turn_on
entity_id: "light.hue_1_{{ range(1,60) | random | int }}"

But this results in an error, probably since the random number isn’t calculated when I write the YAML code (everything goes back to normal when replacing the {{…}}-part with an existing bulb number).

How would I go about doing this? Can I use an array of all bulb names and choose what to turn on randomly? I am quite the novice in HA, but I have done a fair share of programming elsewhere (Arduino’s and such).

have you got


before the service

first test

"light.hue_1_{{ range(1,60) | random | int }}"

in templates and see it does what you want.

MY understand of this is

if you have the “data_template” in you YAML file
when HA see “data_template:” nose there some Janja stuff coming next then it look for {{ }} or {% %}
does the janja thing to it and then pass that in the core to be work on.

You’re close. As @myle mentioned, you need to use data_template in the service call to tell it to render a template. Try:

service: light.turn_on
  entity_id: "light.hue_1_{{ range(1,61) | random }}"

The “range(start, stop)” function actually generates a sequence of numbers from start to stop-1. Hence why I changed it from 60 to 61. Also, no need for the last “| int” part.

Also, is the first light light.hue_1_1, or light.hue_1_01? If the former then that should work. If the latter, then the template would need slight modification.

1 Like

Okay that’s just annoying. When you answered I had already written a long reply to myle, telling him this would not work either.

But it works flawlessly when adding two spaces in front of "entity_id:"

I hate how strict YAML is about spaces, but I will probably just need to suck it up.

Thanx for your help, both of you!

If you have any idea how to make an array with bulb names and randomise those instead, this would make a great prank to make the on button on my Hue Remote turn on a random light (my home lamps are not exactly sequentially numbered :slight_smile:

It’s not the number of spaces, necessarily, but the fact that there are spaces – i.e., that it’s indented. Indentation indicates the item is a member of the other item. Python works the same way. So this:

service: blah
  entity_id: blah.blah

means that entity_id is a member of data_template.

And before you ask, this is another possibility:

service: blah
  entity_id: blah.blah

In this case entity_id is a member of data.

The difference between data and data_template is that when using data, the values of the items under data are just basic types: strings, numbers, etc., whereas when using data_template those values can also be templates that need to be rendered.

And before you ask, this:

service: blah
entity_id: blah.blah

is the same as the one above, because it’s a shortcut. Internally entity_id will be made a member of data.

{{ states.light|map(attribute='entity_id')|list|random }}

Hay I even under stood that

didn’t see it as a member of

was just winging it you make sound beater


Very impressed with all the replies so far.

I read somewhere that the number of spaces had significance, but explained like this, it actually makes sense :slight_smile:

And thanx for the tip about arrays!

1 Like

Thanx a bunch for the suggestions. I have implemented the random light feature in my private install.

I tweeked it a bit to output a string, since I only got a bunch of errors in the log with your version.
I am now addressing and turning off, a random light like this:

  entity_id: '{{ states.light|map(attribute=''entity_id'')|list|random|string}}'
service: light.turn_off

You can see the final results of the 55 bulb installation here. In its final form, I had to group lights 8 at a time, since the random distribution would not guarantee that all lights turned on or change color. So I only use randomly selected colors through the “hs_color” method. See the video description for more details about the setup…

Thanks again, everything turned out great as you can see :slight_smile:

1 Like

Wow… That’s fantastic! Very rarely do I ever see anyone running home automation software (and equipment) in a business/resource setting. Well done and those look really great!

My guess is you had a quoting issue, because the output of what I gave you is already a string:

And, for that matter, the output of any template is always a string anyway.

Try this instead:

  entity_id: "{{ states.light|map(attribute='entity_id')|list|random }}"
service: light.turn_off

You should always use one type of quote to quote the entire template, then if necessary, use the other type of quote inside the template. That is standard practice.

In any case, glad to help, and super cool! :slightly_smiling_face: