Interest in the messaging service Twitter is clearly on the rise (see this Wall Street Journal article if you’re not already familiar with it), and while most people are figuring out how to use Twitter for its intended purpose–social networking–I’d like to share a few notes on my more prosaic experiment of using it as part of my home automation system.
Home automation enthusiasts, particularly those of the hobbyist bent, have come up with several clever methods of having their systems send them notifications of household events when they’re away from home . For example, the home automation system might send a message to their cell phone when the kids arrive home after school, or when the UPS delivery person has left a package at the front door. (For more about the usual methods for doing this, and other examples, see my MacDevCenter article: Macintosh Home Monitoring. )
I’ve been experimenting with using Twitter for automated notifications, and so far I really like the advantages it provides over other techniques.
First, Twitter supports several methods of delivering messages (which are called “tweets” in Twitter-speak). A tweet can be received via SMS to your cell phone, via several instant messaging services, by visiting a web page, or by using specialized apps such as the terrific Twitteriffic.
The best thing about this flexibility is that the recipient chooses how they want to receive their tweets, and changing this setting is easily done “on the fly.” The sender of the message doesn’t need to know which delivery mechanism is currently active, it’s all handled by Twitter. This simplicity is a boon for home notifications which typically either take a shotgun approach and send notifications to several places at once (home, office, and cell phone email), or try to guess (based on time of day or other data) what the best destination might be. Letting the recipient determine where they want to receive messages, at any given moment, makes delivery much simpler and more reliable.
Finally, Twitter has a simple HTTP-based interface for sending messages. Instead of having to script an email program, or an SMS utility, sending a tweet is as easy as having your home automation system open a URL.
My First Implementation
This is an example of a notification sent to me, from my XTension-based home automation system, indicating that someone has been on my front porch.
Here’s how the same message appears on my personal Twitter web page:
When I’m away from a computer, I change my Twitter preferences to send tweets to my phone, instead. Twitter can also relay messages using iChat (AIM), GoogleTalk, LiveJournal, and Jabber but I’ve had mixed success with the reliability of delivery over IM systems, so I don’t use those options.
You’ll need two Twitter accounts. A personal account, for yourself, which you’ll use to receive the messages sent by your home. If you want to receive messages via SMS and IM, you’ll have to configure, and confirm, those details in your Twitter account settings.
The second Twitter account is for your home automation to use when sending the notifications. When you create this account, you will probably want to select the “protect my updates” option so that your notifications aren’t available to the public at large. Then, add your personal account as a “friend” of the home’s account so that you can receive the messages.
Here’s how I currently have this working. Some of these details are specific to XTension, one of the home automation applications I use, but the concept is adaptable to other systems. (See Hacking the Hack, below.)
When a visitor comes to my front door, XTension receives a signal from a motion detector. (Readers of Smart Home Hacks will find the full details about how the visitor is detected beginning on page 271.)
The motion detector activity causes XTension to run a script that constructs a message telling me what happened, and copies the message to the Description field of a unit called “Notify Twitter.” (This is one method of passing variables in XTension.)
In the example below, which shows XTension’s Master List window, the Description of the Notify Twitter unit has been set by this script to “Somebody is at the front door!”
Next, the motion detector’s script sets the value of Notify Twitter to “On,” which causes the script attached to the Notify Twitter unit to execute. This script does the following:
- Retrieves the message text from the Description field.
- Formats the message and Twitter login information.
- Sends the message and login info to Twitter’s status API.
Here’s the script, which borrows some code from Tilman for using curl to send a Twitter status message:
set theMessage to description of me
set TwitterID to "yourID"
set TwitterPassword to "yourPassword"
set TwitterLogin to TwitterID & ":" & TwitterPassword
set TwitterStatus to quoted form of ("status=" & theMessage)
set results to do shell script "curl --user " & TwitterLogin & " --data-binary " & TwitterStatus & " http://twitter.com/statuses/update.json"
You’ll need to enter the login and password for your home’s Twitter account, as noted in the script.
Shortly after running this script, the message is delivered via Twitter, as illustrated earlier. It’s a simple as that.
Hacking the Hack
There are several ways in which this script can be used and adapted. For example, I’m using a variant of it with PhoneValet, to send CallerID information when someone calls my office phone. (See Growling PhoneValet for a starting point if this interests you.)
If I had a security camera on my front porch, it’s easy to imagine including a URL, in the tweet, that would lead to a snapshot taken at the time the motion was detected. Although, depending on the length of your URLs, you might need to utilize something like TinyURL to shorten it before sending. Twitter limits the size of tweets to 140 characters.
If you use Indigo, another Mac-based home automation application, the above script will work just a few modifications. Instead of passing the message text using the Description field, use an Indigo global variable.
If you use MisterHouse, an open source home automation application for Mac, Linux, and Windows, you’ll need to adapt this technique only slightly, replacing the AppleScript portions with Perl.
I’m continuing to tweak and adapt the basic idea here to new applications, if you have a suggestion or enhancement, please let me know what you come up with. Thanks, and have fun tweeting!