Math fun with Indigo and Applescript!

Posted on
Sat Jan 05, 2013 9:35 am
Dewster35 offline
Posts: 1030
Joined: Jul 06, 2010
Location: Petoskey, MI

Math fun with Indigo and Applescript!

Looking to get this to work and having some trouble.

I have four variables in indigo... DistanceAway, Away ReturnTime, CurrentHour and AVGSpeed. Trying to use those variables to determine how long before I arrive home after being away to start up the heat, etc.

I plan to use an additional variable AwayStartup as a trigger when it becomes 1 to do this.

I thought I was on the right track for a little while and then I got stuck.

Any help would be greatly appreciated :)

Code: Select all
tell application "IndigoServer"
   set DistanceAway to the string of variable "DistanceAway"
   set AwayReturnTime to the string of variable "AwayReturnTime"
   set CurrentHour to the string of variable "CurrentHour"
   set AVGSpeed to the string of variable "AVGSpeed"
end tell
set HoursAway to DistanceAway / AVGSpeed
set LeaveTime to AwayReturnTime - HoursAway
if LeaveTime - CurrentHour < 3 then
   set AwayStartup to the value of 1
   tell application "IndigoServer"
      set AwayStartup to the value of variable "AwayStartup"
   end tell
end if

Posted on
Sat Jan 05, 2013 1:41 pm
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Math fun with Indigo and Applescript!

Doing math with string values will often result in strange results. You probably want to change

Code: Select all
the string of variable "XXX"


to

Code: Select all
variable "XXX" as number


Not tested, but you get the idea. All Indigo variables are stored as strings (making the "the string of" part of your script redundant) so you have to explicitly convert them to numbers. In AppleScript, that's what "as number" will do.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sat Jan 05, 2013 2:12 pm
Dewster35 offline
Posts: 1030
Joined: Jul 06, 2010
Location: Petoskey, MI

Re: Math fun with Indigo and Applescript!

jay (support) wrote:
Doing math with string values will often result in strange results. You probably want to change

Code: Select all
the string of variable "XXX"


to

Code: Select all
variable "XXX" as number


Not tested, but you get the idea. All Indigo variables are stored as strings (making the "the string of" part of your script redundant) so you have to explicitly convert them to numbers. In AppleScript, that's what "as number" will do.

Jay,
Made changes as suggested, getting further but still running into errors in the indigo event log.

Error script error: Can’t make «class Vrbl» "DistanceAway" of application "IndigoServer" into type number. (-1700)


Also tried changing the beginning portion before the applescript variable to "set value of XXX" rather than "set XXX".

Code: Select all
tell application "IndigoServer"
   set value of DistanceAway to variable "DistanceAway" as number
   set value of AwayReturnTime to variable "AwayReturnTime" as number
   set value of CurrentIndigoHour to variable "CurrentHour" as number
   set value of AVGSpeed to variable "AVGSpeed" as number
end tell
set HoursAway to DistanceAway / AVGSpeed
set LeaveTime to AwayReturnTime - HoursAway
if LeaveTime - CurrentIndigoHour < 3 then
   tell application "IndigoServer"
      set value of variable "AwayStartup" to true
   end tell
end if

Posted on
Sat Jan 05, 2013 2:32 pm
matt (support) offline
Site Admin
User avatar
Posts: 21417
Joined: Jan 27, 2003
Location: Texas

Re: Math fun with Indigo and Applescript!

Use:

Code: Select all
(value of variable "DistanceAway") as number

Image

Posted on
Sat Jan 05, 2013 4:34 pm
Dewster35 offline
Posts: 1030
Joined: Jul 06, 2010
Location: Petoskey, MI

Re: Math fun with Indigo and Applescript!

Another step forward and another error :/

I discovered one of the problems was that I wasn't declaring the variables beforehand. It is now giving me an error of:

"script error: Can’t set «class Valu» of 1 to 240. (-10006)" Which is related to the set value of distanceAwayAS line within the tell block. I really do appreciate the help and any further help anyone can provide!

Code: Select all
set distanceAwayAS to 1
set awayReturnTimeAS to 2
set currentIndigoHourAS to 3
set aVGSpeedAS to 4
tell application "IndigoServer"
   set value of distanceAwayAS to (value of variable "DistanceAway") as number
   set value of awayReturnTimeAS to (value of variable "AwayReturnTime" as number)
   set value of currentIndigoHourAS to (value of variable "CurrentHour") as number
   set value of aVGSpeedAS to (value of variable "AVGSpeed") as number
end tell
set hoursAwayAS to distanceAwayAS / aVGSpeedAS
set leaveTimeAS to awayReturnTimeAS - hoursAwayAS
if leaveTimeAS - currentIndigoHourAS < 3 then
   tell application "IndigoServer"
      set (value of variable "AwayStartup") to true
   end tell
end if

Posted on
Sat Jan 05, 2013 4:51 pm
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Math fun with Indigo and Applescript!

You syntax for setting the local (AppleScript) variables is wrong. This:

Code: Select all
 set value of distanceAwayAS to (value of variable "DistanceAway") as number


should just be:

Code: Select all
 set distanceAwayAS to (value of variable "DistanceAway") as number


for instance. You only need to use "value of" when talking to an Indigo variable.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sat Jan 05, 2013 5:46 pm
Dewster35 offline
Posts: 1030
Joined: Jul 06, 2010
Location: Petoskey, MI

Re: Math fun with Indigo and Applescript!

Got it. For those looking for this:

Code: Select all
set distanceAwayAS to 1
set awayDepartTimeAS to 1
set currentIndigoHourAS to 1
set aVGSpeedAS to 1
set AwayReturnTime to 1
set ChecktimeAway to 1
tell application "IndigoServer"
   set distanceAwayAS to (value of variable "DistanceAway") as number
   set awayDepartTimeAS to (value of variable "AwayDepartTime" as number)
   set currentIndigoHourAS to (value of variable "CurrentHour") as number
   set aVGSpeedAS to (value of variable "AVGSpeed") as number
end tell
set hoursAwayAS to distanceAwayAS / aVGSpeedAS
set AwayReturnTime to awayDepartTimeAS + hoursAwayAS
set ChecktimeAway to AwayReturnTime - 2.5
if ChecktimeAway < currentIndigoHourAS then
   tell application "IndigoServer"
      set (value of variable "AwayStartup") to true
   end tell
end if


Thank you Jay and Matt! I really appreciate the help! Offering your time, regardless of how much on a Saturday afternoon during NFL wildcard weekend is above and beyond the call of duty ;)

For those curious, when my house goes into away mode, I have the google voice plugin text me each morning and ask me if I plan to return that day. If I respond yes, I get prompted for an estimated time of departure. This enables or disables a few triggers and schedules.

Once it receives that, it passes it to an indigo variable. I have another indigo variable populated with the find my idevice plugin distance away. I'm doing a rough estimate of travel time by the crow flies of that distance divided by my average speed... say 65.

Once it knows I am about two hours out from the house, based on my earlier response and a check to see that I am indeed coming home (a little bit earlier and later than my estimated time of departure), it readies the house for my arrival. Turns on some lights right before I get there, gets the heat bumped up to a comfortable temperature.

The easy way to do this would be to fire off the FMID plugin all the time when I am away, or even just for the day, but I tend to get on the low side of battery life, especially when traveling, so this pings my device the minimum number of times with still reliably getting everything fired up at the optimal time.

My wife asked me tonight why she can't just turn on the heat on our way home... my response, because I'm lazy and don't want to think... which is what home automation is all about :)

Posted on
Sat Jan 05, 2013 9:53 pm
matt (support) offline
Site Admin
User avatar
Posts: 21417
Joined: Jan 27, 2003
Location: Texas

Re: Math fun with Indigo and Applescript!

Neat. Glad you got it working. Sounds pretty smart. :-)

Image

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 6 guests