MacDevCenter    
 Published on MacDevCenter (http://www.macdevcenter.com/)
 See this if you're having trouble printing code examples


Macintosh Home Monitoring

by Gordon Meyer, author of Smart Home Hacks
04/04/2006

Editor's note: In this article you'll learn how to use a few simple home automation techniques to have your Mac send you a message when your mail is delivered, your kids come home, or your dog uses the pet door to go into the backyard.

When I'm away from home but expecting something to happen there--like an important FedEx delivery--my concentration will suffer because of that little voice that occasionally reminds me about what I'm waiting for. In the parlance of Getting Things Done, this is called an "open loop" and it's detrimental to personal productivity. One way to eliminate the distraction is to put a trusted servant in charge of worrying, or at least watching, on your behalf. And that's exactly what this article will describe: a home sentry, made from your Mac and a few home automation pieces, that sends a message to your cell phone the instant your important event occurs.

This approach is quite flexible, you could use it to learn when your kids come home from school, when your mail carrier opens the mailbox, or even when your dachshund uses the pet door to visit the backyard. Next time you find yourself thinking, "I wonder if that has happened yet..." you just might have the solution to stop wondering, and start knowing, right at hand.

Now don't let the words "home automation" frighten you. The approach here only scratches the surface of home automation, and uses sensors and software that are inexpensive and easy to use. In addition, we're sticking with wireless devices, so no pulling of cables or replacing existing wiring will be necessary. You won't quite be living like George Jetson, but your house will be a tad bit smarter.

The Hardware You Need

In addition to a broadband-connected home computer that you leave running while you're away from home, you need:

You have a few choices to make for each of these, so let's go over the options. You should make your choices based on your budget, what you want to monitor, and how much effort you want to put into customizing your setup.

Choose a Sensor

Briefly put, a sensor is a device that, well, senses when something has happened and then relays that information to the home automation controller connected to your Mac. If you want to know about general activity in an area, such as your front porch, then a motion detector will do the trick. On the other hand, if you want to know that your kids are home safe, you'll want to monitor something that's inside the house, and something that you know they're going to interact with when they come home. For example, you might install a door sensor in their bedroom, or on the fridge.

Motion Detectors

Motion detectors are about the size of an Altoids tin and function by sensing movement with passive infrared detection. That is, they constantly scan for moving objects that are of significantly different temperature than the surrounding area. When you walk into a room, the sensor sees you as a moving 98-degree blob of heat. (Don't take it personally.) When this happens, the unit wirelessly transmits an On signal and then continues to transmit another every few seconds until you've left its sight, or have stood very still for several minutes. When it finally decides that all is quiet, it sends an Off signal.

The X10 Indoor Wireless Motion Sensor is the best choice for this project. It can mount on the wall with double-stick tape and its AAA batteries last months, if not longer, before having to be replaced. Figure 1 shows a motion detector positioned to watch a front porch for visitors.

figure 1
Figure 1: A sensor in the eaves of a front porch

There are some things to keep in mind when deciding where to mount a motion detector. Because they work by detecting movies bodies of heat, they can be fooled by hot air from a heating vent, moving cars, or similar objects. Also, if you're trying to detect that a person has entered a room, position the sensor so that it "sees" the person walk past. A little experimentation will be needed to find the best position; remember that you can mount the sensor orientated in any direction, such as sideways on the ceiling, if that's what works best for you.

Smart Home Hacks

Related Reading

Smart Home Hacks
Tips & Tools for Automating Your House
By Gordon Meyer

Door Sensors

If you want to be notified of something being opened, such as a bedroom door, the mailbox, or the liquor cabinet, you need a door and window sensor. Up until recently you pretty much had to hack together your own door sensor, by modifying a motion detector, but thanks to a new home automation controller (discussed shortly) you can use wireless door and window security sensors instead.

The security sensors aren't especially attractive, but they're small and easy to install. They have a two-piece magnetic switch, one half of which you mount to your door, the other half to the door jamb. A short set of wires runs to the battery-powered transmitter. When the door opens, the transmitter blips out a signal to your home automation controller. Another signal is sent when the door has closed. It's simple, yet very effective. And since these sensors are meant for use with a security system, they even transmit a warning beacon when their battery is low.

Door sensors are sold individually but the best price is usually in a multi-pack of four or more sensors, if you think you need that many. Make sure the ones you buy are compatible with X10 security systems; other types of wireless sensors won't be able to talk to the home automation controller.

Wireless-to-USB Home Automation Controller

It wasn't very long ago that connecting a home automation controller to your Mac required a jumble of adapters. Luckily, X10 Corporation's CM15 Active Home Pro controller puts all that in the past. It's USB-based and, best of all, it includes a wireless receiver that works with the motion detectors and door sensors we've been discussing. It provides everything you need for this project and more.

While you can buy the Active Home Pro by itself, if you have any inkling that you might want to try automating a few lights too, it's a better deal to get the ActiveHome Pro Starter Kit instead. It adds a motion detector and a few other modules that you can use to do more than what's described in this article. For help with putting those extras to use, I hope you'll consider turning to my O'Reilly book, Smart Home Hacks.

Home Automation Software

There are a few good home automation applications for the Mac, but I think that the best choice for this project is to download the "light" version of XTension. The full version is a versatile home automation toolkit, but the free version allows you to work with up to five units (sensors), which is plenty for what we're doing here. Another choice is Indigo, but its demo version expires after 30 days. However, if you insist that your Mac experience consist of nothing but Cocoa applications, using Indigo is sure to make you happy.

If your tastes run more toward free and open software, then gander at MisterHouse. Written in Perl and driven with a web interface, it runs on Unix, Mac OS X, and Windows systems. Unfortunately, it doesn't (yet) support the CM15 Active Home Pro controller. You'll have to use a alternate controllers, and those adapters I mentioned. Check out the MisterHouse docs for what it supports, and read my article Hacks for Smart Homes for tips about getting the software up and running on Mac OS X.

Setting It Up

Whew, getting the hardware was the hardest part, you're in the home stretch now. To get going, install your home automation software, connect the Active Home Pro to a nearby wall socket and to a USB port on your Mac, and mount a sensor in an appropriate location. Let's say it's a motion detector over the front porch.

The next step is to set the detector's X10 address. Every X10 device has an address that consists of an alphabetic house code (A through P) followed by a unit number (1 through 16). On the motion detector, the address is set by pressing two tiny buttons underneath its front door. For the sake of example, let's say that you set the address to C14.

Now you can add the detector to your home automation software. If you're using XTension, choose File -> New Unit, then fill in the unit's address (C14), click the checkbox to indicate that it is an X10 Unit Type, and then check the Allow Wireless option under Unit Behavior. Give it a descriptive name too, such as "Front Door Motion" as shown in Figure 2.

figure 2
Figure 2: Tell XTension about the sensor.

Click OK and the unit is added to the Master Unit List. Repeat this process for each of your sensors. Or, if you're anxious to see some results, keep reading and come back to configure the others later.

If you're using a door and window sensor, be sure to set the Unit Type to Security, so XTension will know about the different type of signals it sends. Also, security units don't have an address that you set directly. When you're first putting batteries in the unit, stand near your computer, then push the button on the sensor and XTension will walk you through the process of adding the new unit.

Baby Steps

Let's get back to our example. Next you're going to tell XTension to run an AppleScript when it receives an On command from unit C14, which is your motion detector. To enter this script, select the motion detector in the Master List window, then choose Edit -> Edit Unit. In the Edit Unit dialog that appears, click the Edit button next to the On Script label.

XTension uses AppleScript for its programming language, so you can pretty much trigger any automated task that tickles your fancy. I use the Create New Message script from Apple to send an email to my cell phone's email address. You just need to change the script to customize the message and specify the details of your email account.

If you're less handy with scripting, fire up Automator and create an application that sends an email, then use the On Script to "tell application Finder" to open the application you created.

Regardless of where you get your script, paste it into the XTension editor window, then click Check Syntax to make sure it compiles successfully. To test your script you can simulate an On command from the sensor by double-clicking the unit in the Master Unit List window. If all goes well, you'll get the message on your cell phone. Bask in your success!

Getting Smarter

Eventually you'll discover that this simplified setup has some drawbacks. As mentioned earlier, the motion detector sends an On signal every few seconds while motion is still being detected. This means you'll soon overflow your mailbox if the detector is in a busy area. Additionally, the way it's set up now, an email gets sent at all hours of the day, not just during the periods that you're really interested in monitoring.

Luckily, there are ways to address this. The simplest is to only run XTension when you want to receive the notifications. If XTension isn't running, the signals from your detector will be ignored. That works fine if you're confident that you'll remember to start XTension before you leave home. Sometimes, the simple approach really is the best.

However, if you want something smarter you can create a scheduled event so that signals are acted upon only during certain hours of the day. Scheduling is one of XTension's most powerful features, so this won't be tough at all.

Choose File -> New Event, and create an "Ignore Front Door" event as shown in Figure 3. This event executes every weekday at 6:30 p.m., the time you're normally home from work. Its action is set to "block" the unit Front Door Motion. Blocking means that while XTension will still receive the signal from the motion detector (there's no way to stop the sensor from sending the signal), the signal will be ignored and the email script will not run.

figure 3
Figure 3: An event that ignores the motion detector

Similarly, create a second event called "Watch Front Door" whose action unblocks the unit in the early afternoon, or whatever times you want to watch for motion. With these two events, you've created a window in time during which you'll be notified of the motion at your front door.

If you find that a time window is too inflexible, here's another option. Create a pseudo-unit (it's like a variable) whose value determines if the email will be sent. For example, create a pseudo-unit called "Out Of The House" and turn it On, with a mouse click, when you leave the home. In your mail script, check to see if the value of Out Of The House is On, and if it is, send the email.(See the XTension user's guide for more about pseudo-units and checking the state of other units in a script.)

Doing More

If you're not using XTension, this approach is easy to adapt to other home automation software. The concepts of scheduled events and triggering scripts in response to sensors are core to all of the packages discussed. Just start simple, celebrate your early successes, and carefully build a more complex system as time and needs arise.

Now that you're familiar with home automation scheduling, you might find some other uses for it, too. For example, I use XTension as my alarm clock, with different alarm times set for weekends and holidays, all handled automatically. Once you've got the concept, you might just keep doing more. I'm looking forward to hearing about where you end up.

Gordon Meyer writes and speaks about personal technology and instructional design from his automated home in Chicago.


Return to the Mac DevCenter

Copyright © 2009 O'Reilly Media, Inc.