Smart Home Hacks Excerpt

We wrote this article for the excellent Smart Home Hacks book by Gordon Meyer. For additional tips, ideas, and hacks to help you boost the intelligence of your home you can purchase Smart Home Hacks on Amazon.com.

Control Your Home with Phlink

The Phlink Telephone Adapter for Mac OS X provides a way to control and monitor your home over the phone, and it’s easy to integrate with Indigo.

How would you like to check on your house while at the office or on the road? There are a few ways to do this, such as using a web browser or having your home send you email. One of the most useful methods, especially in a situation in which you don’t have access to a computer, is to control your home over the phone using touch-tone commands. But beyond just sending commands to your home, it’s even better when you can have the home speak its status using voice synthesis.

To do this you need Indigo and the innovative Ovolab Phlink Telephone Adapter. The Phlink, which connects to your phone line and computer (via USB), can answer telephone calls, identify callers based on CID information, record voicemail messages, and make outgoing telephone calls. Additionally, Phlink has terrific AppleScript support, which allows for integration with other Mac OS X applications. Perceptive Automation has written a set of AppleScripts that integrate Indigo and Phlink, giving you a speech-synthesized menu system that enables you to control Indigo in the following ways, all over the phone:

   - Perform complex actions by executing Indigo action groups

   - Hear the status of devices (e.g., “Hot tub heater is on”)

   - Turn devices on or off

   - Hear the brightness level of lamps and light modules

   - Brighten or dim lamps and light modules

   - Installing the Phlink Scripts for Indigo

After you download the Phlink scripts for Indigo, follow the Read Me instructions to place the scripts and folders in the correct locations. Copy the folders 1 and 2 and the file greeting.scpt into the /Library/Application Support/Phlink Items folder. If you already have folders of the same name in that location, create a new folder with a different single-digit name, such as 3, and copy the 1 and 2 folders from the Indigo download inside there.

Phlink uses the names of the folders to build a menu system that you navigate by dialing the associated number. For example, you’ll dial 1 to access the items that are in the 1 folder, after you get everything set up.

Next, copy phlink call out attachment.scpt into your Indigo attachments folder:

    /Library/Application Support/Perceptive Autoination/Indigo 2/Scripts/Attachments/

Indigo 1.x users will find their attachment folder here:

    ~/Documents/Indigo User Data/Scripts/Attachments/

This attachment script defines two functions, MakeEmergencyCallToNumber() and MakeEmergencyCall(), which can be called from any Indigo trigger or time/date action to have Phlink call your mobile or office phone in an emergency.

If Indigo is running already, choose Reload Attachments from the Scripts menu to make it recognize the new script you added.

Phoning Home

Now that we have everything installed, let’s call into the system to see what happens. Make sure the Phlink application is open and set to answer calls. Then, dial your phone number using a cell phone or another line.

When the phone rings, Phlink answers the call and immediately executes the greeting.scpt AppleScript that you installed in the Phlink Items folder. That script uses speech synthesis to speak to the caller. It says:

   Indigo home control server.

   Press 1# to execute an Action Group.

   Press 2# to control a Device.

When the user presses 1#, Phlink will traverse into the 1 folder and execute its greeting.scpt AppleScript. Likewise, when 2# is pressed, Phlink traverses into the 2 folder and executes the script found in that location.

The greeting script in the 1 folder queries Indigo to get a list of the action groups you’ve set as being eligible for execution over the phone. Similarly, the script in the 2 folder determines which devices can be controlled over the phone.

To set which action groups and devices can be accessed, you change its settings in Indigo. To edit an action group or device, check the Display in remote UI checkbox:

All action groups and devices that have this option selected will be available to the Phlink scripts. For example, if you select this option for the action groups away from house, be home soon, and guest party arrival, Phlink will speak the following menu:

   To execute Action Group away from house press 1#.

   To execute Action Group be home soon press 2#.

   To execute Action Group guest party arrival press 3#.

Similarly, the device menu will include those you’ve selected for remote access:

   To control device Porch Light press 1#,

   To control device Entryway Light press 2#,

   To control device Hot Tub Heater press 3#.

When you dial a number, Phlink passes the number to the default.scpt script in the current folder (inside folder 1 for action groups or inside folder 2 for devices). These scripts tell Indigo to execute the appropriate action group, or they get the status of the selected device. In our example, if the user presses 3#, she hears:

   Hot Tub Heater is off.

   To turn on press 1#.

   To turn off press 2#.

The script also handles the user’s response, sending an On or Off command to the selected device.

Custom Status Reporting

In addition to controlling devices and executing action groups, you can use AppleScript and Phlink to retrieve information about your home’s status. For example, let’s say you are interested in the number of times a motion detector on your front porch has been triggered today, to see if you’ve had any visitors while you’ve been out.

The first step is to set up Indigo so that it increments a variable each time front porch motion detector turns on. Create a new trigger action named front motion trigger and set the trigger so that it’s executed when the device front porch motion detector is turned on:

Click the Action tab and choose Modify Variable from the Type pop-up menu. Set the options so that the variable frontMotionCount is incremented by 1 when this trigger is executed. If you haven’t already created frontMotionCount, choose New from the Variable pop-up menu to create a variable.

This counter will continue to increment each time the motion detector is activated, but we’re interested only in the count for the current day, so let’s set up a time/date action that resets the counter every night. Add a new time/date action that executes every day, just after midnight:

Click the Action tab, and select Modify Variable from the Type menu. Then, choose frontDoorMotionCount and set its value to 0:

Now we have an Indigo variable, frontMotionCount, that automatically counts the motion triggers that occur over a 24-hour period. Next, we need a way to report the value of this variable, via Phlink, to the caller on the phone.

Open the greeting.scpt script, located in the Phlink Items folder, and insert four lines into the script:

on do_action given call:c
     tell application "Phlink 1.4.1"
         tell c
             speak "Indigo home control server. Press 1 pound"
             tell application "Indigo"
                 set motionCount to get value of variable "frontMotionCount"
             end tell
             speak "front motion count is " & motionCount
             set the require pound sign to true
             set the email recordings to false
         end tell
     end tell
end do_action

Now, we have a customized main greeting script that speaks the current motion detector trigger count every time it’s executed. The next time you call your house you will hear:

   Indigo home control server.

   Press 1# to execute an Action Group.

   Press 2# to control a Device.

   Front motion count is 9

Remote Notification

All the work thus far has created a system that you can call remotely to control and query the state of your house. But in addition to answering incoming calls, Phlink can place outgoing calls. From a home automation perspective, this is exciting because now we can have Indigo call our mobile or office phone whenever a critical trigger occurs, such as after a power failure, an intruder is detected, or a freeze/flood sensor has gone off.

The phlink call out attachment.scpt file that you installed earlier makes it easy. It’s an attachment script that defines two new functions you can use from any Indigo trigger or time/date action. The first function, MakeEmergencyCall(), calls the emergency number you specify in the script. The second, MakeEmergencyCallToNumber(), enables you to specify a number to call when you call the function. In both cases, you also provide the message you want Phlink to speak to whomever answers the calls.

Before you can use the AppleScript, you need to add your preferred emergency phone number to the script. Open the phlink call out attachment.scpt file in Script Editor and change the properties as shown here:

   -- Default telephone number to call. Only used when MakeEmergencyCall() function is called.
   -- Use MakeEmergencyCallToNumber() to override this number for a particular action.
   property defaultEmergencyNumberToCall : "555 555 0101"

   -- The number of times the message should be repeated. We repeat because there currently
   -- is not a way for Phlink to detect exactly when the call has been answered.
   property speakRepeatCount : 10

The speakRepeatCount property can stay at 10, unless you find that having the phrase repeated 10 times does not allow enough time for the message to be delivered.

After you’ve saved your changes, choose Reload Attachments from the Scripts menu in Indigo to register your changes with the program.

Now, let’s put it to use. Let’s say you want to be notified when the outside temperature in your hen house reaches 32 degrees Fahrenheit. You have a freeze sensor connected to a Powerflash, so Indigo will receive an X10 On command when the sensor detects that it’s too cold.

Create a new Indigo trigger action that is executed when the command from the freeze sensor is received. The action for this trigger is to run an embedded AppleScript that consists of a single function call that calls the emergency function you updated with your phone number:

To test this trigger action, either put the freeze sensor in your icebox for a few minutes, or select the trigger action in the Indigo main window and click Execute Action Now. Phlink will call the phone number you defined and repeat the phrase “Freeze sensor triggered” 10 times.

If you want to have Phlink dial a number other than the one you defined in the attachment script, use the MakeEmergencyCallToNumber() function instead. It accepts an additional argument that specifies the phone number to dial:

   MakeEmergencyCallToNumber("freeze sensor triggered", "555 555 0102")

Final Thoughts

This hack should give you a good foundation for integrating your home automation system with your telephone. You can modify the scripts you installed in the first few steps to do more. Examining how these scripts work is a great way to find hints on how to add additional functionality. If you come up with an interesting script, visit the Indigo online support forum and share it with other Indigo and Phlink users.