Using quotes with Indigo Variables & sending all off

Posted on
Wed Feb 18, 2004 1:56 pm
ddeppe offline
Posts: 23
Joined: Dec 13, 2003

Using quotes with Indigo Variables & sending all off

Hi, I'm working on my first masterpiece as a newbie to applescript that I wrote during my lunch break, although I do have experience scripting in unix korn shell - most of this is not yet tested as I have to go home before I can try it out. I've pieced it together using snippits of code from other scripts in this forum, and have guessed at some of the wording as I forgot to bring a printout of the Indigo dictionary. Let me know if you see anything that stands out as a syntax error as it may save me some time tonight. One thing I was confused on was where I needed to place Indigo variables in quotes, and where I don't - or should they always be in quotes? I know applescript local variables don't need quotes but that may not apply to Indigo variables since they would be an application variable. I'm also wondering what the syntax is for sending an all off to a house code, for example all lights off to house code F to save having to send out individual off commands during the day (as kids sometimes turn on lights during the day at the switch & forget to turn off). Thanks : )

-- note: depends on nightly enablment of FishLightActive variable
tell application "Indigo"
if daylight then
log "Andrews motion during daylight"
remove delayed actions for device "Andrews Light"
remove delayed actions for device "Andrews Closet"
turn off "Andrews Light" in 10 * 60
turn off "Andrews Closet" in 10 * 60
else -- it is nighttime
if (FishLitTime <= 120) then
if (FishLightActive = true) then
if (on state of device "Andrews Fish Light" is false) then
log "Turning on Andrews Fish Light"
turn on "Andrews Fish Light" for 10 * 60
set value of variable "FishLitTime" to "FishLitTime" + 10
remove delayed actions for variable "FishLightActive"
set value of variable "FishLightActive" to False
set value of variable "FishLightActive" to True in 8 * 60
else -- fish light is already on
log "Leaving Andrews Fish Light on additional 10 min's"
remove delayed actions for device "Andrews Fish Light"
turn off "Andrews Fish Light" in 10 * 60
set "FishLitTime" to FishLitTime + 8
remove delayed actions for variable "FishLightActive"
set value of variable "FishLightActive" to False
set value of variable "FishLightActive" to True in 8 * 60
end if -- state of fish light
remove delayed actions for device "Andrews Light"
dim "Andrews Light" 15% in 35 * 60
turn off "Andrews Light" in 40 * 60
else -- FishLitTime has reached daily limit
set value of variable "FishLightActive" to False
set value of variable "FishLitTime" to 0
log "Andrews fishlight has reached limit - disabling until next night"
end if -- daylight
end tell

Posted on
Wed Feb 18, 2004 2:01 pm
ddeppe offline
Posts: 23
Joined: Dec 13, 2003

above may be difficult to read

The copy of the script that I pasted above uses spaced indentation to make the application flow easier to read - I didn't realize the post was going to remove all the extra spaces. Sorry about it being more difficult to read the flow now : )

Posted on
Wed Feb 18, 2004 7:10 pm
ddeppe offline
Posts: 23
Joined: Dec 13, 2003

First error

Ok, I've gone home & gave it a try - first error I'm getting is:

Expected end of line, etc. but found class name.

With the below line:

remove delayed actions for variable "FishLightActive"

Posted on
Wed Feb 18, 2004 7:43 pm
matt (support) offline
Site Admin
User avatar
Posts: 21417
Joined: Jan 27, 2003
Location: Texas

Re: First error

Take a look at the Indigo AppleScript dictionary to see all of the verbs and their arguments (File->Open Dictionary from within the Script Editor app).

You do need to put quotes around Indigo variable names. You also have to extract out the value using syntax like:

if (value of variable "FishLitTime" < 120) then

The script error you mentioned above is because you cannot remove delayed actions based on a variable, only based on a device or trigger (see the dictionary for the syntax). You also cannot delay the value setting of a variable by using "to". Variable values are always instantly set. I think you want to look at the verbs "enable time date action" and maybe "enable trigger action." You aren't using either of those in the script above, but I think you'll want at least one of them. Note they both do allow the use of "in" and "for" to set a delay and duration.

Regards,
Matt

Posted on
Wed Feb 18, 2004 10:50 pm
ddeppe offline
Posts: 23
Joined: Dec 13, 2003

(No subject)

I finally got it all working, just had to do a little tweaking. Thanks for your help : )

Below is the completed code:
Code: Select all
            
-- note: depends on nightly enablment of FishActive variable & reset of FishLitTime variable.
tell application "Indigo"
   if daylight then
      log "Andrews motion during daylight"
      remove delayed actions for device "Andrews Light"
      remove delayed actions for device "Andrews Closet"
      turn off "Andrews Light" in 10 * 60
      turn off "Andrews Closet" in 10 * 60
   else -- it is nighttime
      if (value of variable "FishActive" = "true") then
         set FishLitTime to (value of variable "FishLitTime")
         if (FishLitTime > 120) then
            if (on state of device "Andrews Fish Light" is false) then
               log "Turning on Andrews Fish Light"
               turn on "Andrews Fish Light" for 10 * 60
               set value of variable "FishLitTime" to (value of variable "FishLitTime") + 10
               log "new value of FishLitTime is: " & value of variable "FishLitTime"
               remove delayed actions for trigger "Andrews Motion"
               set value of variable "FishActive" to false
               execute group "Enable FishActive" in 8 * 60
            else -- fish light is already on
               log "Leaving Andrews Fish Light on additional 10 min's " & value of variable "FishLitTime"
               remove delayed actions for device "Andrews Fish Light"
               turn off "Andrews Fish Light" in 10 * 60
               set value of variable "FishLitTime" to (value of variable "FishLitTime") + 8
               log "new value of FishLitTime is: " & value of variable "FishLitTime"
               remove delayed actions for trigger "Andrews Motion"
               set value of variable "FishActive" to false
               execute group "Enable FishActive" in 8 * 60
            end if -- state of fish light
            remove delayed actions for device "Andrews Light"
            dim "Andrews Light" by 15 in 35 * 60
            turn off "Andrews Light" in 40 * 60
         else -- FishLitTime has reached daily limit
            log "Andrews fishlight has reached limit - disabling until next night " & value of variable "FishLitTime"
            set value of variable "FishActive" to false
            set value of variable "FishLitTime" to 0
         end if -- FishLitTime
      else
         --log "FishActive is false"
         remove delayed actions for device "Andrews Light"
         dim "Andrews Light" by 15 in 35 * 60
         turn off "Andrews Light" in 40 * 60
      end if -- FishActive
   end if -- daylight 
end tell

Posted on
Wed Feb 18, 2004 11:12 pm
ddeppe offline
Posts: 23
Joined: Dec 13, 2003

(No subject)

Ooops - I had forgotten to change back a > to a < - I had played with it while trying to figure out why the if wasn't working - turned out to be a string vs integer problem. Here is the corrected code for anyone interested:
Code: Select all
-- note: depends on nightly enablment of FishActive variable & reset of FishLitTime variable.
tell application "Indigo"
   if daylight then
      log "Andrews motion during daylight"
      remove delayed actions for device "Andrews Light"
      remove delayed actions for device "Andrews Closet"
      turn off "Andrews Light" in 10 * 60
      turn off "Andrews Closet" in 10 * 60
   else -- it is nighttime
      if (value of variable "FishActive" = "true") then
         set FishLitTime to (value of variable "FishLitTime")
         if (FishLitTime < 120) then
            if (on state of device "Andrews Fish Light" is false) then
               log "Turning on Andrews Fish Light"
               turn on "Andrews Fish Light" for 10 * 60
               set value of variable "FishLitTime" to (value of variable "FishLitTime") + 10
               log "new value of FishLitTime is: " & value of variable "FishLitTime"
               remove delayed actions for trigger "Andrews Motion"
               set value of variable "FishActive" to false
               execute group "Enable FishActive" in 8 * 60
            else -- fish light is already on
               log "Leaving Andrews Fish Light on additional 10 min's "
               remove delayed actions for device "Andrews Fish Light"
               turn off "Andrews Fish Light" in 10 * 60
               set value of variable "FishLitTime" to (value of variable "FishLitTime") + 8
               log "new value of FishLitTime is: " & value of variable "FishLitTime"
               remove delayed actions for trigger "Andrews Motion"
               set value of variable "FishActive" to false
               execute group "Enable FishActive" in 8 * 60
            end if -- state of fish light
            remove delayed actions for device "Andrews Light"
            dim "Andrews Light" by 15 in 35 * 60
            turn off "Andrews Light" in 40 * 60
         else -- FishLitTime has reached daily limit
            log "Andrews fishlight has reached limit - disabling until next night - final value was: " & value of variable "FishLitTime"
            set value of variable "FishActive" to false
            set value of variable "FishLitTime" to 0
         end if -- FishLitTime
      else
         --log "FishActive is false"
         remove delayed actions for device "Andrews Light"
         dim "Andrews Light" by 15 in 35 * 60
         turn off "Andrews Light" in 40 * 60
      end if -- FishActive
   end if -- daylight 
end tell

Posted on
Thu Feb 19, 2004 12:15 am
ddeppe offline
Posts: 23
Joined: Dec 13, 2003

(No subject)

I found an interesting issue - when I tried to compare numbers 0 through 18 to see if it was less than 150 I had issues with it coming back as false. After some experimentation I found if I set the initial value to 100 rather than starting from 0, and checked to see if the numbers were less than 250 (just raised by an equal amount) - it worked fine. I'm thinking I might be having a string issue, but I'm not sure. Here is the modified code with the work around. Let me know if you have any idea what I've run into. Thanks : )


Code: Select all
-- note: depends on nightly enablment of FishActive variable & reset of FishLitTime variable.
tell application "Indigo"
   if daylight then
      log "Andrews motion during daylight"
      remove delayed actions for device "Andrews Light"
      remove delayed actions for device "Andrews Closet"
      turn off "Andrews Light" in 15 * 60
      turn off "Andrews Closet" in 15 * 60
      disable trigger action "Andrews Motion" for 10 * 60
   else -- it is nighttime
      if (value of variable "FishActive" = "true") then
         disable trigger action "Andrews Motion" for 8 * 60
         if value of variable "FishLitTime" is less than 250 then
            if (on state of device "Andrews Fish Light" is false) then
               log "Turning on Andrews Fish Light"
               turn on "Andrews Fish Light" for 10 * 60
               set value of variable "FishLitTime" to (value of variable "FishLitTime") + 10
               log "new value of FishLitTime is: " & value of variable "FishLitTime"
            else -- fish light is already on
               log "Leaving Andrews Fish Light on additional 10 min's "
               remove delayed actions for device "Andrews Fish Light"
               turn off "Andrews Fish Light" in 10 * 60
               set value of variable "FishLitTime" to (value of variable "FishLitTime") + 8
               log "new value of FishLitTime is: " & value of variable "FishLitTime"
            end if -- state of fish light
            remove delayed actions for device "Andrews Light"
            dim "Andrews Light" by 15 in 35 * 60
            turn off "Andrews Light" in 40 * 60
         else -- FishLitTime has reached daily limit
            log "Andrews fishlight has reached limit - disabling until next night - final value was: " & value of variable "FishLitTime"
            set value of variable "FishActive" to false
            set value of variable "FishLitTime" to 100
         end if -- FishLitTime
      else
         --log "FishActive is false"
         disable trigger action "Andrews Motion" for 8 * 60
         remove delayed actions for device "Andrews Light"
         dim "Andrews Light" by 15 in 35 * 60
         turn off "Andrews Light" in 40 * 60
      end if -- FishActive
   end if -- daylight 
end tell

Posted on
Thu Feb 19, 2004 8:57 am
matt (support) offline
Site Admin
User avatar
Posts: 21417
Joined: Jan 27, 2003
Location: Texas

(No subject)

AppleScript tries to cast variables between types automatically for you. Indigo variable values are always returned as strings, even if they are numbers. You can force AppleScript to cast to what you want by using "as integer" and "as string". For example:

set myIntValue to value of variable "FishLitTime" as integer

I'm not sure that this will fix the particular problem you are seeing though. I scanned through your code and I would have thought AppleScript would have been doing the type conversions for you correctly. And it doesn't really make sense to me why it would work sometimes but not others. Regardless, it might help to explicitly cast to the type you want.

Regards,
Matt

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 27 guests