New to Indigo scripting - best way to code a complex trigger

Forum rules

This is a legacy forum which is locked for new topics. New topics should be started in one of the other forums under Extending Indigo

Posted on
Sun Oct 21, 2012 8:27 pm
bap offline
Posts: 33
Joined: Sep 25, 2012

New to Indigo scripting - best way to code a complex trigger

Although I've never programmed with python I'm a former professional software developer and have lots of experience with languages ranging from VB to c/c++ to perl, bash, etc. so I'm quite comfortable with programming and more than willing to take the dive into Python.

I've set up some basic triggers in Indigo to monitor our washer & dryer in our basement using iMeters. In a nutshell when the power consumption goes above a certain level Indigo knows they're running, and when they stop running it sends a page to me & my wife to let us know a wash/dry cycle is complete. It's extremely helpful in reminding us to check on the laundry.

I'd now like to modify this a bit to make its paging of us a bit smarter. I've installed the Smartphone Radar plugin and would like to integrate both it and some timing rules into how we get paged. In a nutshell I'd like it to only page us if either of us is home, and if neither is home then wait until one or both are home. And if it's after say midnight then don't bother paging us at all (or perhaps wait until the next morning to page us). So in pseudo-code I'm thinking of something along these lines:

Code: Select all
while !paged ; do
  if (after midnight) ; then
    paged = true;
  endif
  if (bruce or heather is home) ; then
    page whoever is home;
    paged = true;
  endif;
  sleep 15 minutes if !paged;
done


So the big question I have is whether a loop like this is kosher in a script used in a condition or not. I can see where the loop could remain active for a few hours waiting for one of us to show up to get paged. Would that potentially get Indigo into any sort of bad state? Also does it make sense to have an action within part of the condition like this? I assume I'd just set the action in the dialog to "None" in that case. Any comments, suggestions, etc. would be greatly appreciated.

-Bruce

Posted on
Mon Oct 22, 2012 10:40 am
jay (support) offline
Site Admin
User avatar
Posts: 18255
Joined: Mar 19, 2008
Location: Austin, Texas

Re: New to Indigo scripting - best way to code a complex tri

So you're right to ask - you shouldn't try to run a script like that in any of the embedded script fields (the condition script field or the Execute Script action). AppleScripts, for a variety of reasons, must run on the main application thread. So if you run a script that has delays (or even just takes a while to run) you'll be holding up the rest of the server's main processing.

Given the complexity of what you're wanting to do, I'd suggest writing an external Python script as the action (rather than a condition). The script could then check the time, whether anyone is home, etc., and determine the correct course of action. If you want to proceed with the notification, then just execute an Action Group that does the notification.

If you wanted a delayed page, then whenever the script runs and it's between midnight and morning then increase the value of a variable. At the appointed time in the morning, have a schedule that runs - the condition would be if the value of the variable > 0, then the action would be the same Action Group as above (just add another action to it to set the value of the variable to 0).

That should get you started. Python should be very simple for you to learn since it's syntactically very similar to c/c++.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Oct 22, 2012 7:44 pm
bap offline
Posts: 33
Joined: Sep 25, 2012

Re: New to Indigo scripting - best way to code a complex tri

jay (support) wrote:
you shouldn't try to run a script like that in any of the embedded script fields (the condition script field or the Execute Script action). AppleScripts, for a variety of reasons, must run on the main application thread. So if you run a script that has delays (or even just takes a while to run) you'll be holding up the rest of the server's main processing.


Thanks. That's pretty much what I was expecting to hear, and exactly why I asked.

-Bruce

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests