View unanswered posts | View active topics It is currently Wed Jun 19, 2013 9:18 am



Reply to topic  [ 7 posts ] 
 "on state of device" not working as expected. 
Author Message

Joined: Dec 02, 2006
Posts: 25
Post "on state of device" not working as expected.
I am trying to use the "on state of variable" syntax to poll a device, and I'm a little confused about the following behavior. I have the following script as an attachment.

Code: Select all
   on motion(_timedelta)
         if on state of device "Hallway Motion detector" then
            return true
         else if on state of device "2nd Floor Hall Motion detector" then
            return true
         else if ((current date) - (date value of variable "lastmotion") as real < _timedelta) then
            return true
         else
            return false
         end if
   end motion


the purpose is to check for motion within a given amount of time, and return true if there was motion, and false if there was not. I have trigger actions set up for the motion detectors to place the (current date) into the variable "lastmotion" whenever their state changes to "on." When there is a lot of motion, the motion detectors never send an off event, and therefore it could have bee a long time since their state changed to "on", but there is still motion happening. That's why I check the state of the motion detector as well. If it's still on, then there's still motion.

Now to the unexpected behavior. If the state of "Hallway Motion detector" is "on" the program returns true, as expected. However, if the state of "Hallway Motion detector" is "off" then I get the following error.

Error script error: Can't get value.
Error error dispatching event to attachment script (-1753)
Error script error: Can't get value.

I've narrowed it down to the first statement "if on state of device "Hallway etc."

if I embed the following script into an action group...

Code: Select all
if on state of device "Hallway Motion detector" then
    log "it's on"
end if



it works properly for both "on" and "off" states of the "Hallway Motion detector device"

Is there an explanation or way around this?

Thanks,

Scott.


Sun Aug 12, 2007 7:46 am
Profile
Site Admin
User avatar

Joined: Jan 27, 2003
Posts: 11814
Location: Texas
Post Re: "on state of device" not working as expected.
Hi Scott,

Could the error be in the AppleScript code that calls motion()? I tried your script snippet in an attachment and was able to call motion() regardless of the on/off state of "Hallway Motion detector." This makes me think it is the code that calls motion() that is having the problem (and it only has a problem if true is returned).

Regards,
Matt


Sun Aug 12, 2007 10:37 am
Profile WWW

Joined: Dec 02, 2006
Posts: 25
Post 
here's the script that calls the motion().

It works when "Hallway " is on, but not when it is not, returning the error in my previous posting.

Code: Select all
if (motion(60 * minutes)) then
    log "Motion detected within last hour"
else if (value of variable "Entertaining" as boolean) then
    log "Entertaining"
else
    log "No motion within past hour"
    execute group "Off at Bedtime"
end if


Note also that I can test "if onstate of "Hallway" correctly for either state of Hallway if the script is embedded, but not when it's in the attachment.

strange.... thanks for the help.

Scott.


Sun Aug 12, 2007 11:11 am
Profile
Site Admin
User avatar

Joined: Jan 27, 2003
Posts: 11814
Location: Texas
Post 
I think you problem is in the 2nd conditional then:

Code: Select all
else if (value of variable "Entertaining" as boolean) then

What is the value of the variable Entertaining? Is it really "true" or "false"? If it isn't, then AppleScript isn't going to like you trying to cast it to a boolean I think.

Regards,
Matt


Sun Aug 12, 2007 11:24 am
Profile WWW

Joined: Dec 02, 2006
Posts: 25
Post I tried this as a test.
I got rid of the second conditional. The trigger action is "Check Bedtime test"

Code: Select all
if (motion(60 * minutes)) then
    log "Motion detected within last hour"
    --else if (value of variable "Entertaining" as boolean) then
    --    log "Entertaining"
else
    log "No motion within past hour"
    execute group "Off at Bedtime"
end if


First, I turned on the motion detector, and ran the action group, and then I turned off "Hallway Motion deetector" and ran it again. Here's the log.


Sent X10 "Hallway Motion detector" on
Action Group Check Bedtime test
Script Motion detected within last hour

Aug 12, 2007 7:12:43 PM
Sent X10 "Hallway Motion detector" off
Action Group Check Bedtime test
Error script error: Can't get value.
Error error dispatching event to attachment script (-1753)
Error script error: Can't get value.

so I don't think it's the second if-test.

Thanks,

Scott


Sun Aug 12, 2007 5:15 pm
Profile
Site Admin
User avatar

Joined: Jan 27, 2003
Posts: 11814
Location: Texas
Post Re: I tried this as a test.
Hi Scott,

Okay, then I think the problem is in this conditional inside motion():
Code: Select all
else if ((current date) - (date value of variable "lastmotion") as real < _timedelta) then


Do you still get the error if you remove that?


Sun Aug 12, 2007 5:18 pm
Profile WWW

Joined: Dec 02, 2006
Posts: 25
Post What a difference a () makes....
Matt,

YES, you are right, it was the 3rd boolean (which is why it only failed when the other two fell through). I added perens around the "value of variable" and it appears to work now. I still don't iunderstand why it works when embedded, but not as an attachment, but perhaps I've done too many debugging permutations to make too much of that conclusion. The code now looks like

Code: Select all
   on motion(_timedelta)
      if on state of device "Hallway Motion detector" then
         return true
      else if on state of device "2nd Floor Hall Motion detector" then
         return true
      else if ((current date) - (date (value of variable "lastmotion")) as real < _timedelta) then
         return true
      else
         return false
      end if
   end motion


Thanks so much for the step-by-step support. Truly excellent.

Best,

Scott.


Mon Aug 13, 2007 7:18 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 7 posts ] 

Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.   Template designed by STSoftware.