Using Bluetooth Proximity Detection with Home Automation

In the movies, smart homes automatically greet you when you return after a hard day at work. And while that does sound like a neat trick, until recently it just hasn't been very easy to actually do. In reality, most home automation setups are based around button pushing.

That's because home automation, in its most basic form, is about re-locating switches. For example, instead of having to get up from your chair to turn off the porch light, you can press a button on a remote control instead. The continued popularity of the famous sonically-controlled switch, "The Clapper," gives testimony to the appeal of the simple remote control.

One step beyond button relocation is doing several things with one button press -- such as turning off all of the lights upstairs and adjusting the thermostat at the same time. This is possible when you use a home automation program to create macros that trigger a series of actions when you wield your remote control. But, in the end, you're still pressing a button to spur your home into action, and it just doesn't quite live up to the Hollywood vision.

Fortunately, technology and clever programmers are catching up with the fantasy. With the Mac you already have, your cell phone, and a little scripting, you can be living in a house that automatically does things for you when you arrive; no buttons required.

What's the new ingredient that makes this possible? Bluetooth. Although it wasn't designed for this use, it turns out that Bluetooth provides a way for your computer to know when you're home by inferring your presence from your cell phone or other Bluetooth-equipped device. And that's good enough to start putting the "smart" in smart homes.

The Hardware You Need

You need a Mac with Bluetooth and at least one Bluetooth device, such as a cell phone or PDA. Virtually any portable Bluetooth-equipped device will work. Unlike with iSync, no special Bluetooth support is necessary, since your Mac doesn't have to actually exchange any information with the device, it only has to be able to detect that it exists. I've used the technique described here with a Nokia 6600 phone, a Motorola Bluetooth Headset, and an Apple iPhone.

The Software You Need

There are several programs that allow you to trigger scripts from a Bluetooth device, including the venerable Salling Clicker ($23.95) and Home Zone (in Beta). But I think the one best suited for this project is Proximity (Donationware). It's not as complex as the others, it provides exactly the functions necessary for this project, and it is unobtrusive when running so you can continue to use your Mac for other tasks if you don't have a computer dedicated to home automation.

If the things you're interested in automating only happen on your Mac, such as automatically checking your personal email when you come home from work, you don't need anything else. But if you want to control the lights in your home, or otherwise reach out beyond your computer, you'll need home automation equipment and software. Setting up the equipment is beyond the scope of this article, but it's easier than you might think. See "Old Mac, New Tricks" for details.

For home automation software, you can use Indigo ($179), XTension ($149.95), or MisterHouse (freeware). In this example I'll use XTension, but the techniques can be used with the others, too.

Mac, Meet Phone

The first step is to formally introduce your Mac and your cell phone to each other, a process called "pairing." To do this, make sure your phone's Bluetooth support is switched on and the device is set to "discoverable" mode. (On my Nokia, this is done in the Communications settings. On the iPhone, it's in the General settings menu.) Next, go to your Mac and follow these steps:

  1. Choose System Preferences from the Apple menu, then click Bluetooth.
  2. In the Settings tab, select the Discoverable check box. This allows your phone to see that your Mac is available via Bluetooth.
  3. Click the Devices tab, then click Set Up New Device. The Bluetooth Setup Assistant opens.
  4. Use the setup assistant to pair your device with your Mac.

Depending on the device, this might involve entering a series of numbers to authorize the connection. If you have trouble with this, try changing the settings under Passkey Options.

For the purposes of this project, you don't need to set up Address Book sharing, syncing, or data access. You can ignore, or turn off, those options if the setup assistant presents them to you.

When you've finished the setup assistant, your device will be added to the list of Bluetooth Devices in the Bluetooth preferences pane.

Preparing Proximity

Next, open the Proximity application on your Mac. Choose Preferences from the Proximity menu item in your menu bar (look towards the right end of the menu bar to find it), then do the following:

  1. In the General tab, check Monitoring and enter how often you want Proximity to check for the presence of your Bluetooth device. Choose a scan interval that makes sense for your situation. For example, if you want your Mac to turn on the room lights as soon as it detects your presence, you'll want to have Proximity look for your cell phone every minute or so. But if you simply want to adjust your automated thermostat, or to announce how many mails you have waiting, then a scan interval of every 10 minutes might be sufficient.
  2. In the Device tab, click Change Device then select the Bluetooth device that you paired with your Mac in the earlier set up steps, as shown in Figure 1.
  3. Figure 1
    Figure 1. Proximity watches for a Bluetooth equipped device, then runs a script when it finds it nearby.

  4. When you're done, click Check Connectivity to make sure that Proximity is able to detect your device.
  5. In the Scripts tab, select an "In Range Script". The script is executed when Proximity detects that your device, and by inference, you, are within range of your Mac. We'll discuss the script in more detail soon.

Proximity can also execute a script when it stops detecting your device, which you configure with the "Out of Range Script" option, but I haven't found this useful for home automation. The reason is that Bluetooth's range is rather limited, so you can't reliably infer that you've left home simply because your device has moved out of range. You might have only gone to the kitchen for another cup of coffee.

Instead of using Bluetooth to guess when I'm gone, I manually tell my home automation system that I have left. This is nearly automatic for me because my home alarm system sends a signal to XTension when I turn it on. But, before I had the alarm system, I accomplished the same thing using an AppleScript that was triggered by pushing a button on a remote control I kept near the front door.

Putting it in Action

The AppleScript that you specified in Proximity is where all the magic happens, and here is your opportunity to really get creative. Anything you can accomplish with AppleScript you can have automatically done when you, carrying your Bluetooth device, get within sensing range of your Mac.

Figure 2
Figure 2. AppleScript lights your way home and tells you what you missed while you were out.

My Proximity script is shown in Figure 2. It performs several actions in sequence, some of which use conditions that are tracked for me by XTension. The first thing that happens is that the current time is after sunset, a lamp in the entryway is turned on. Not only is this welcoming, it provides a subtle visual cue that my Mac has recognized me and has started the script.

Next, the Mac uses text-to-speech to tell me how many phone calls I missed while I was gone. This information is tracked by XTension using PhoneValet ($169.96) to monitor incoming calls. The extra spaces before words that you see in the sayString variable are necessary to allow text-to-speech to correctly read the assembled message after the variables are substituted. If you accidentally leave the spaces out, parts of your messages will be spelled letter-by-letter instead of spoken.

Then, finally, the Mac announces how many unread email messages I have waiting. It determines this by asking Mail for the contents of a mailbox that only contains messages sent to me by people that I've assigned to a group in Address Book. This is accomplished by a rule that I have defined in Mail. These are all standard techniques but, as with much of home automation, it's the ability to connect existing pieces in new and convenient ways that creates value and adds convenience.

Doing What You Want

Now that you know to trigger actions automatically, the hard part is deciding on the most useful things to have happen. Perhaps you want to have Safari load your favorite web sites, or you could have appointments read from iCal. It's also good to think about the other people who share your home, perhaps the script should do something different if their cell phone is detected, too. The limit is your imagination, AppleScript ability, and the fun you find in experimenting with different approaches.