[SOLVED] Script to control delay and duration of light

Posted on
Mon Feb 03, 2014 11:11 am
mrgoat offline
Posts: 10
Joined: Jan 03, 2012

[SOLVED] Script to control delay and duration of light

I have a need to have a light turned on, left on for a random duration, then turned off, then repeated a given number of iterations. I have no experience with applescript but what i have been able to put together so far is the following.

Tell app "Indigo"
set c to random number from a to b --define delay
set d to random number from x to y --define duration
with timeout of z --define timeout
do shell script "sleep " & d
end timeout
Toggle "Light Name" in c for d --toggle Light in "c" sec for "d" duration
end Tell

Now, will that do a single iteration of the toggling with random delay of "c" and duration of "d"? How would I integrate either a set number of iterations or have it loop until the duration count reached a preset number?

Posted on
Tue Feb 04, 2014 3:37 pm
matt (support) offline
Site Admin
User avatar
Posts: 21417
Joined: Jan 27, 2003
Location: Texas

Re: Need simple script to control delay and duration of ligh

Let's use a python action instead of AppleScript to solve this (python is recommended over AppleScript because of its robustness and flexibility). So in the action panel try this:

Code: Select all
from random import randint

iterCount = 3
dev = indigo.devices[123]     # Replace 123 with the device ID number -- right click on device in main window to copy it
randomDelay = randint(10, 20)
randomDuration = randint(15, 25)

while iterCount > 0:
   indigo.sleep(randomDelay)
   indigo.device.turnOn(dev)
   indigo.sleep(randomDuration)
   indigo.device.turnOff(dev)
   iterCount -= 1


Untested, but probably pretty close to correct. Note you do NOT want to execute the script above embedded (pasted into the Execute Script edit field) because only 1 script can execute at a time and IndigoServer will kill scripts that take too long. Instead, save it as a file (randomLight.py) and have the Execute Script action execute the file instead. That will force it to run in its own process where it cannot hang up the Indigo Server processing.

Image

Posted on
Tue Feb 04, 2014 3:42 pm
mrgoat offline
Posts: 10
Joined: Jan 03, 2012

Re: Need simple script to control delay and duration of ligh

Excellent. I'll give that a try tonight and report back.

Posted on
Wed Feb 05, 2014 7:37 am
mrgoat offline
Posts: 10
Joined: Jan 03, 2012

Re: Need simple script to control delay and duration of ligh

I am getting this error with the script in the log window.

Code: Select all
 Script Error                    randomlight.py: unexpected character after line continuation character
 Script Error                    around line 1 - "{\rtf1\ansi\ansicpg1252\cocoartf1265"



and here is the script
Code: Select all
from random import randint

iterCount = 3
dev = indigo.devices[1596241241]     
randomDelay = randint(10, 20)
randomDuration = randint(15, 25)

while iterCount > 0:
   indigo.sleep(randomDelay)
   indigo.device.turnOn(dev)
   indigo.sleep(randomDuration)
   indigo.device.turnOff(dev)
   iterCount -= 1

Posted on
Wed Feb 05, 2014 8:12 am
matt (support) offline
Site Admin
User avatar
Posts: 21417
Joined: Jan 27, 2003
Location: Texas

Re: Need simple script to control delay and duration of ligh

Download and use TextWrangler (free) to create the script. TextEdit is trying to save a RTF file, but for code/script you need a raw text file with no formatting.

Image

Posted on
Wed Feb 05, 2014 9:21 pm
mrgoat offline
Posts: 10
Joined: Jan 03, 2012

Re: Need simple script to control delay and duration of ligh

ok that fixed the previous error. Now i'm getting this.

Code: Select all

Script Error                    randomlight.py: 'module' object has no attribute 'sleep'
Script Error                    Exception Traceback (most recent call shown last):

     randomlight.py, line 9, at top level
AttributeError: 'module' object has no attribute 'sleep'

Posted on
Wed Feb 05, 2014 9:24 pm
matt (support) offline
Site Admin
User avatar
Posts: 21417
Joined: Jan 27, 2003
Location: Texas

Re: Need simple script to control delay and duration of ligh

Try this instead:

Code: Select all
from random import randint

iterCount = 3
dev = indigo.devices[1596241241]     
randomDelay = randint(10, 20)
randomDuration = randint(15, 25)

while iterCount > 0:
   indigo.activePlugin.sleep(randomDelay)
   indigo.device.turnOn(dev)
   indigo.activePlugin.sleep(randomDuration)
   indigo.device.turnOff(dev)
   iterCount -= 1

Image

Posted on
Wed Feb 05, 2014 9:43 pm
mrgoat offline
Posts: 10
Joined: Jan 03, 2012

Re: Need simple script to control delay and duration of ligh

iterations countdown is working fine. The randomint function doesn't seem to be working. The delays are whatever the lower bound is and not a random integer between lower and upper.

Posted on
Wed Feb 05, 2014 9:45 pm
matt (support) offline
Site Admin
User avatar
Posts: 21417
Joined: Jan 27, 2003
Location: Texas

Re: Need simple script to control delay and duration of ligh

Move those two lines that calculate the random values into (that is, under) the while loop, and indent them over to match the other indented lines.

Image

Posted on
Wed Feb 05, 2014 9:50 pm
mrgoat offline
Posts: 10
Joined: Jan 03, 2012

Re: Need simple script to control delay and duration of ligh

That seems to be working exactly how i want it to. Thanks!!!!

Posted on
Sun Jul 27, 2014 9:58 pm
akimball offline
Posts: 561
Joined: Aug 07, 2013
Location: Sandy, Utah

Re: [SOLVED] Script to control delay and duration of light

Sorry to open up a "solved" thread, but have a few questions for the experts. Using this thread I successfully wrote a eight separate Python scripts that execute turning strategically selected random lights in the house in what I call "Vacation Mode."

I wrote a schedule in Indigo for Vacation Mode: I have a variable "vacationMode" which if set to true, then at sunset (randomized by plus or minus 10 minutes, eight scripts are executed in the actions list, and each script controls a light in my home will be turned on after a random time period, for a random amount of time, three times each. Here is an example of one of the eight scripts and this one is called "randomFitnessRoomLight.py":

Code: Select all
from random import randint

iterCount = 3
dev = indigo.devices[109173275] # "Fitness Room - KPL"

while iterCount > 0:
    randomDelay = randint(120, 7200)
    randomDuration = randint(60, 600)
    indigo.activePlugin.sleep(randomDelay)
    indigo.device.turnOn(dev)
    indigo.activePlugin.sleep(randomDuration)
    indigo.device.turnOff(dev)
    iterCount -= 1


Here are my questions:

1) I'm assuming the units are in "seconds" and that numbers like 7200 seconds are perfectly fine (this corresponds to a delay period of up to 2 hours). Is this correct?

2) If these scripts are executed every night for several days, how do they terminate or do they keep running forever even if the iteration count reaches 0 in it's countdown? Won't they fill up memory on my indigo server eventually? I guess I'm asking about garbage cleanup. When these scripts are finished doing their thing, I would like them to report to the log they are finished and quit peacefully.

3) Is there an easy way to externally kill a script once it is executing, in case I change my mind? Lets say I get home after sunset once the scripts already executed and don't want my lights turning on and off.

Thanks, I'm still very new to python. The scripts seem to be working although they are hard to test. Any thoughts?

TIA, -Al :D

-Al

Posted on
Mon Jul 28, 2014 6:22 am
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: [SOLVED] Script to control delay and duration of light

1. Yes, seconds.

2. When iterCount reaches zero, the script will terminate.

3. In the While loop, have the individual scripts check to see if vacationMode is still set. If not, just exit. Then clear VacationMode when you get home.

One way to make them easier to test would be to pull the delay values from variables, and have the event that kicks them off set the values. That way you could put short values in that one script. Like divide all numbers by 60 and it'll be over in 8-10 minutes.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Mon Jul 28, 2014 6:39 pm
akimball offline
Posts: 561
Joined: Aug 07, 2013
Location: Sandy, Utah

Re: [SOLVED] Script to control delay and duration of light

@FlyingDiver,

Thanks for the reply. I wasn't sure about the units, and garbage collection... and I appreciate the prompting to have the script check the state of the variable. I think on that last one I supposed that a script running externally to Python would not know how to read the variables... I was wrong.

Here is the finished script, one of 8 that run...

Code: Select all
from random import randint

iterCount = 3
dev = indigo.devices[109173275] # "Fitness Room - KPL"

while iterCount > 0:
   vacMode = indigo.variables[1709090354] # "vacationMode"
   if vacMode.value == "true":
      randomDelay = randint(120, 7200)
      indigo.activePlugin.sleep(randomDelay)
      indigo.server.log("Turning on Fitness Room Light")
      indigo.device.turnOn(dev)
      randomDuration = randint(60, 600)
      indigo.activePlugin.sleep(randomDuration)
      indigo.device.turnOff(dev)
   iterCount -= 1
indigo.server.log("randomFitnessRoomLight script complete")


I believe this is working. I will try implementing a multiplier as you suggest to help out with the testing. Thanks again!

-Al

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 28 guests