Hey guys, im trying to learn some stuff i templating so im fiddling around in the template editor but i need some guidance please.
My current project is as follows:
My living room has 8 spotlights (named livingroom_1 - livingroom_8).
I want to build a script to turn on a random number of spotlights on command
I want it to be at least 2 and max 7 (any number between them)
So i was thinking something like:
{% set number_of_lights_to_open=
(range(2, 7)|random)
%}
Then say this gives us the random value X.
I want a template to select X random different numbers from 1 to 8 and have them stored as random_lights.light_number (?)
Then iterate X times to turn on each switch.livingroom_{{random_lights.light_number}}
How would i go about writing something like this?
Also am i thinking this right? or is there some better way to do it?
Thanks in advance
First time ill fiddle with this but i have some minimal experience with python. I guess it was about time to try it on HA too. Will go study and come back with any questions. Thank you!
Works like a charm, thank you!
BTW, the (1,9) was not intentional right? as i only have 8 lights?
To expose it to alexa I created a HA script to call it, like this:
It was intentional. The range function produces a sequence that starts with the first number, and ends with the last number minus one. So, range(1,9) produces [1,2,3,4,5,6,7,8].
And while we’re on the topic, random.randint produces a random number between the first and second numbers, inclusive.
I would like the upper code you gave me, to turn on a random set of switches, and at the same time turn off the rest (not selected by the random)
This way i could run the script even if all the lights are on and end up with a random group set to on. Or repeat the script if i dont like the result untill i get some nice set.
Hey there, cominig back for some more help after 3 years
How would i edit this block of code to work on an already set group of lights?
In this case we actually “build” the group right? by selecting similar entities with different numbers at the end
lights = []
for i in range(1,9):
lights.append('switch.livingroom_{}'.format(i))
How can i make it work on a group called light.dn_lr_lights?
something that would work like this:
How did you create that group of lights? Can you show me its attributes from the STATES page? Unless it has an attribute that lists the lights in the group, I don’t think there is a way.
Thing is that it’s contents are two other light groups (that contain the actual switch and light entities).
I could remake the group in order to include all the individual entities in it, or could we get the names from the attributes of the sub-groups?