Choose random devices


#1

Is there a way to randomly select 3 devices from a list of 20? I know to list the eligible lights and switches in a device variable. I would then choose 2 or 3 lights at random and save them in another variable. But I don’t know how to do that step.

I am trying to make a vacation lighting piston that will select 2 or 3 lights at random from a list of all of my lights to turn on. I plan to run the piston every few minutes to make it look like my home is occupied.

Thanks in advance!


#2

Here’s my thoughts, but I don’t think my way of picking devices is correct:


#3

heres an example. you would replace lightsOnVacation with your variable VacLights and add the condition for location mode is Vacation.

Note that both the for and with statements, Task Cancellation Policy is set to On piston state.


#4

Not exactly what I was looking for, but interesting idea! I’ll give it a try.


#5

true. the concept is similar but different. :slight_smile:


#6

I understand all of this except TCP. Do I want automatic piston state enabled or disabled? Wouldn’t it be better to just change TCP to never? Please explain this to me. This is one area I don’t really understand.


#7

Best explanation of TCP here
https://wiki.webcore.co/Task_Cancellation_Policy

Automatic piston state is how to create custom dashboard info. Otherwise it is true or false based on how the piston is evaluated.


#8

to add to what @c1arkbar said, using automatic piston state to cancel the tasks creates further randomness. setting it to never would work, but random would theoretically be less random? :slight_smile:


#9

Whoops jumped into the middle of this


#10

hey, we all got to start somewhere :wink:


#11

Thanks to both of you!


#12

sure thing. if you would still like to select 3 random devices from a list of devices, happy to provide a snippet for that as well.


#13

Please do!


#14

using your variables … off the top of my head …

set RandomLight1 = arrayItem( random( count( VacLights ) -1 ), VacLights)
do
   set RandomLight2 = arrayItem( random( count( VacLights ) -1 ), VacLights)
while
   RandomLight2 == RandomLight1
end do
do
   set RandomLight3 = arrayItem( random( count( VacLights ) -1 ), VacLights)
while
   RandomLight3 == RandomLight1
   or
   RandomLight3 == RandomLight2
end do

My "vacation lights" turning on at wrong time
#15

Hi bangali, where can I find array functions? I searched the wiki for arrayItem and nothing found.


#16

probably not added to wiki yet :slight_smile: open up a piston in edit mode and type it in the expression field.


#17

way ahead if you, used it as soon as I saw your post and makes a randomized away lighting routine very simple. I went searching in hopes there was a place I missed with other cool stuff :slight_smile:


#18

sorry, didnt realize that snippet had worked for you … it has been a while :slight_smile:

all the cool stuff is here spread out over this forum.


#19

Yeah I read stuff I’m not necessarily interested in just to discover things lol. What I used from your snippet was the arrayItem function, here’s what I have running now (tested but not used when we were away yet, that comes tomorrow).


#20

cool. sounds like fun :slight_smile: