Sunday, January 29, 2017

Apple added Siri to MacOS but Siri on the Mac cannot issue HomeKit commands or commands to devices that are not HomeKit compatible. Siri can be used to send SMS messages to an IFTTT phone channel which can trigger an IFTTT applet that sends commands to your Harmony or other devices and activities. Here is what you have to do to setup Harmony activities, IFTTT applets, and MacOS commands to control your activities and devices from the command line on a Mac/Macbook.

Any platform that provides IFTTT support can be used in a similar manner.

In order for IFTTT to issue commands to the Harmony system you must first create Harmony Activities. Use the Harmony application on your iPad or iPhone to create activities. I have created activities to "Watch TV", "Play Blu-Ray", "Watch Apple TV", and so on. These activities combine the devices and commands to those devices necessary to start and end the activity. For instance, to start the "Watch TV" activity the TV is turned on and set to HDMI 1, the Receiver is turned on and set to TV/SAT, and the TiVo Roamio Pro is powered on. Similar sets of devices and actions are used to create the other activities.

Step 3 - Connect IFTTT Services
For this demonstration we will need to connect the SMS and Harmony services to our IFTTT account. Login at IFTTT, click the Search link and enter "SMS" in the search text field. Configure and connect the IFTTT SMS service. Similarly, search for "Harmony" (or the service that controls your home automation platform), configure and connect the Harmony IFTTT service.

Step 4 - Create IFTTT Applets

You can create IFTTT Applets that use the IFTTT services you have connected to your account. In this example we use the SMS and Harmony services. Login at IFTTT, click your IFTTT user name and select "New Applet" from the dropdown menu or go to https://ifttt.com/create

Click the "+ this" link and search for the SMS service. Click the SMS service box. Click the "Send IFTTT an SMS tagged" box. Make sure it is the "tagged" box and not the "... any SMS" box. Enter the tag you want to use to trigger the activity. For example, I used the tag "WatchTV" to trigger my Watch TV activity. No need to put a "#" at the beginning of the string here, just the string to use as a tag. Click "Create trigger".

This should bring you back to the "If this then that" page with the "this" part filled out with SMS. Now click the "+ that" link and search for the Harmony service. Click the Harmony service box. With the Harmony service you need to now select either the "Start activity" or "End activity" box. This may vary with other home automation platforms. Click the "Start activity" box for this example.

If you have correctly connected your Harmony IFTTT service then you should now see a "Start activity" box with a dropdown menu that lists the Harmony activities you have created and enabled. Select the Harmony activity you want to start with this applet and click "Create action".

Edit the title of your new applet and click "Finish".

You now have an IFTTT account, an IFTTT phone channel, IFTTT services that connect to your phone and home automation platform, and IFTTT applets that are triggered by SMS tags sent to your IFTTT phone channel.

Step 5 - Send an SMS Message to Your IFTTT Phone Channel

You should now be able to activate an IFTTT applet by sending the appropriate SMS message to your IFTTT phone channel. From the phone number you used to configure your IFTTT phone channel (see https://ifttt.com/services/phone_call/settings) send an SMS with one of the tags you used to create an IFTTT applet. For example, I used "WatchTV" as the tag to trigger my Watch TV activity. I can send the SMS message "#WatchTV" to my IFTTT phone channel and that activates my Watch TV IFTTT applet. Note that the SMS message must contain the "#" symbol at the beginning of the tag.

Try it with the Messages app on your Mac/Macbook after configuring an "IFTTT Phone Channel" contact to use. You can also try it with Siri on your Mac/Macbook by asking Siri to send an SMS message to IFTTT phone channel. Unfortunately, for me, Siri often misunderstands what I say and changes "Play Blu-Ray" to "Say Tuesday" or some nonsense. The SMS message tag must correspond exactly to the tag you entered when creating the IFTTT applet. It's even case sensitive so you may have to go back and modify the IFTTT applet to match what Siri sends (e.g. Siri will capitalize the 'A' and the 'TV' in AppleTV and does not insert a space. I played around with it but soon decided to use a command line script rather than the finicky Siri voice commands.

Step 6 - Create Scripts to Send SMS Messages

I found it easier to simply type in a simple command than try to get Siri to understand exactly what I wanted to send. I'm a programmer and I almost always have several terminal windows open so I chose to write some simple scripts to allow me to easily type a command to control my devices and activities.

Here is the "ifttt" Bash script I wrote to send SMS messages to my IFTTT phone channel. You should be able to modify this slightly, setting the IFT variable to your IFTTT phone number and changing the tag strings to match those you have used. Or you may choose to write your own shell script or AppleScript - all I'm really doing here is executing the following AppleScript with the osascript command in a shell script:

tell application "Messages"
send "$MSG" to buddy "$IFT" of service "SMS"
end tell

Where the MSG variable contains the message I want to send and the IFT variable contains the IFTTT phone number.

My ifttt shell script:

The ifttt script can be invoked directly but to simplify further I wrote additional scripts which then run the ifttt script with the appropriate arguments. For example, to run the Watch TV activity I can execute the ifttt command directly as:
# ifttt tv on
or
# ifttt tv off
or with no arguments it turns the activity on
# ifttt tv
I also wrote a shell script "tv" which does just that:

#!/bin/bash
ifttt tv $*

So now I can also start the Watch TV activity by typing:
# tv on
or
# tv
And end the Watch TV activity by typing:
# tv off

The latest version of my ifttt script and associated convenience scripts are available in the IFTTT subdirectory of my Scripts github repository at https://github.com/doctorfree/Scripts