I needed a script that would reliably close my garage door. Using the I/O Linc (Model 2450 -- 1 input & 1 output set to momentary ON) and the magnet from the garage kit I had all the info required. I wanted the script to address situations where a momentary ON (i.e. button press) would not result in the door closing. This could occur if the door were partially opened (button press may open the door fully instead of closing it) or if the door sticks occasionally and 'rebounds' to the open position. I limit the number of close attempts to five so that the motor doesn't burn out if things go haywire.
The script is a standard external AppleScript.
The script tests the state of Binary input 1. If it's FALSE, the door is open AND if the loop counter indicates fewer than 6 attempts, it proceeds.
After incrementing the counter, it sets an Indigo variable I created called 'vMsgBody' with text. This causes a completely different trigger to send me a text message. I have commented this text out since most users won't have things set up this way. The script then sets the binary output 1 to TRUE. This causes a momentary closing of the output circuit (momentary because the unit was configured that way). This is the equivalent to a button press that activates the door. Usually this will close the door.
After waiting 30 seconds, binary input 1 is polled again to confirm that the door actually closed. If it hasn't, the loop repeats until it has OR until 5 attempts are completed.
This is a generic door-closing script but I actually call it from 2 different triggers to accomplish the desired behavior. I want the garage door to auto- close after 15 minutes, but only if I'm not out in the garage. If I'm in the garage, I'd like it to stay open until 15 minutes after I leave. I accomplish this by adding a motion detector set up with a 15 minutes to OFF delay.
Trigger 1 is activated the moment the door opens (i.e. when binary input 1 goes to FALSE). By setting a Condition that 'if the state of the motion detector is OFF' (as returned by a short embedded AppleScript under the Conditions tab), I ensure that this script doesn't close the door if I'm moving around in the garage. This trigger is set up with a 15 minute delay after which my generic Door Close script is called.
Trigger 2 is activated by the motion detector going to the OFF state after the detector's built-in 15 min delay. When this happens the generic close script is called immediately. Since the script begins by checking the state of binary input 1, it doesn't do anything if the door happens to be already closed.
So far, this has worked perfectly. But I have thought of a scenario where it would 'fail'. If I open the garage door from the driveway using the garage door remote, when Trigger 1 activates it sees no motion in the garage. It would then begin the 15 minute delay based on this no-motion status. However if I hang out in the garage, although my movements set the motion detector to ON, this status is not tested again by Trigger 1. Therefore the garage door will close with me moving in the garage. The solution is to test the motion state again immediately before calling the generic door close routine (or as another criteria for the repeat while loop although that would make the script a little less generic.