Choose random devices


#21

Ooo this is basically what I was just about to start trying to create myself! Or change another piston to do! Only difference is I was planning to have two devices randomly turned on at any given time and then randomly switched off, whereas this piston does it to one. I think I can achieve my aim by creating a ‘RandomDevice1’ and ‘RandomDevice2’ and getting the piston to check that the two are not the same light before executing the actions…

Will give it a go…


#22

So with the one light at a time logic, I’ve updated the piston to switch off all lights once one or more of us get home and then as a result of the SHM mode changing the piston shouldn’t run anymore anyway.

Will this work?


#23

Yep, that should be simple to do, if you don’t mind share when done? Also, I just purchased a light sensor so that I can change the Time test to be when its dark.


#24

Cool will do. Need to test this over the next few days and then share. I’ve not actually shared a piston before so how do I do that?!

The other bit I’m curious to test is the push notifications - my initial instinct was to put a separate notification when the light is turned on and then a separate one when it’s turned off but that might result in too many notifications!


#25

There’s a camera button under the code of the piston that takes a pic and lets you save it for posting here. I do a push of the on and off time just for testing, once its solid I’ll only have that go to Notices.


#26

Ah OK got it. What’s Notices?


#27

Sorry, Notifications in ST app.


#28

Thought I’d jump in here because @bangali seems to know what’s going on and we’re all working from the same piston. What I’m trying to achieve is to turn a single light on, randomly, only once between sunset and midnight. I’m having a hard time trying to work out the random wait, as I can’t use minutes because in a few months with the time change it will be out of sync. Is there anyway to turn as light on randomly between sunset and midnight?


#29

Hi @Alwas
This works for me with no problems.
To make this one only run once, change the wait randomly to wait 12 hours.
It will then only run once.
Bit clunky but it will work.


#30

Thanks SO much! It looks very good, WAY above my pay grade, I’ll import it now and test over a few days, I really need to get this to work.


#31

I’m trying to get to grips with webcore so can someone please explain the line below to help me on my way.

{arrayItem( random( count( AvailableDevices ) -1 ), AvailableDevices )};

I presume it is randomly picking an item from the list defined by the variable AvailableDevices but what does ‘count’ do and the ‘-1’ and why is AvailableDevices entered twice.

Many thanks in advance.


#32

I believe arrayItem index starts at zero, which is why the device count is being reduced by one. If there were 10 devices… valid index would be 0 to 9.

count is returning the number of devices inside the AvailableDevices device variable.


#33

Ahh ok I see I think. So with the above statement it is counting the number of referenced variables but instead of the first variable being one it is zero. It is then picking a random number and using the variable that corresponds to that index.
Many thanks for the explanation.