Welcome to Brian's Bits, where Brian gets to share at length about various topics stirring inside of him.
Using Siri to Control WeMo Devices
12 November 2014
Recently I wrote a long, detailed article about how you can remote control your Mac with Siri. But there was so much to say that I wasn’t able to fit it all into that one article.

So in today’s part two, I’m going to explain how you can use the Siri / AppleScript system that was described in the first article to remote voice control any Belkin WeMo devices you may have.

As much as possible, I’m going to avoid repeating myself here, so you absolutely need to read and understand the previous article before you continue here. You will also need to have a copy of my AppleScript which you will modify for your own uses — download it here: SiriListener.scpt. If you are feeling a bit more adventurous, you might like to download the new and improved version two: SiriListener2.scpt.
In addition to the pieces of the puzzle we put together last time — — we now need to add a few more puzzle pieces in order to gain the ability to remote voice control WeMo devices:
Of course, I’m assuming that you already have at least one WeMo device, and that you have the free WeMo app installed on your iOS device (iPhone, iPad, iTouch). I’m also assuming that your WeMo hardware and software are functioning properly. If you have not gotten at least that far, the rest of this article will be mere theory rather than practical application. It is very unfortunate that Belkin’s WeMo app is designed for the iPhone instead of being a universal app which runs as a native iPad app. Because of this, the app runs in a simulated iPhone screen, and only in portrait orientation. Therefore it feels very clunky and out of date.

But the real problem is that the WeMo app does not always run properly in this iPhone-compatibility mode. The screen shots to the right are a good illustration of this. The left screen shot is the normal appearance of the app when you are setting the time for a rule. The lower half of the screen is a separate subwindow overlay for choosing a time. Once you pick the time you want, you tap on the Done button (with the green text, to the right of the “Set time” text).

The right screen shot shows how very often, but not always, the left edge of the app is truncated — a slice 80 pixels wide is missing! Even worse, the Done button that you need to tap on when you are finished choosing a time is completely gone! Therefore it is impossible to get out of the “Set time” subwindow, so you have to force-quit the app and relaunch it.

Needless to say, you for sure can’t set up any time-based rules when the interface is malfunctioning like that. The customer reviews for the WeMo app on iTunes are FILLED with similar complaints. Surely Belkin can afford to hire a decent iOS programmer or two who know what they are doing, so that all of these bugs can be fixed and a universal app can be rolled out. Come on, Belkin, get your act together! I had heard about the Web-base service If This Then That (IFTTT) for a couple of years, but I never had any compelling reason to use it — until I wanted to use Siri to remote voice control my WeMo switches. It was then that I found out what millions of other users had already discovered: IFTTT is awesome!

IFTTT has both a Web site and an iOS app. As far as I know they both have the same functionality, but because I find the Web site less cluttered and easier to use, I will be using their Web site in my examples.

If you don’t have a free account, join IFTTT now! Once you are all signed up and signed in, go to the Channels section. If the Email Channel is not yet activated (I can’t remember if it is by default), you’ll need to activate it.

Notice in the instructions that “the Email Channel address may be separate from the address associated with your IFTTT account.” For potentially better security, you might want to set up a new e-mail account which you use only for this Email Channel, as I have done.

Now find and activate the WeMo Channel(s) that match the WeMo device(s) you own. I own two WeMo Switches, so that is the only WeMo Channel I have activated. You will notice that in order to activate a WeMo Channel, you need a PIN generated by the WeMo app on your iOS device. So, if you can get your WeMo app to work OK, tap over to the More screen, as shown to the right. You need to do two things here. First, tap on the “Remote Access” option. On the resulting screen, enable remote access.

After remote access has been successfully enabled, you will also need to tap on the “Connect to IFTTT” option. On the resulting screen will be a PIN which you can now enter on the WeMo Channel Web page. Then click the Activate button and you should be all set.

To verify your IFTTT setup, click over to the Channels page, and then click on the Activated tab. You should see something similar to the screen shot in the previous section above, listing active Channels for Email and your WeMo device(s). The Date & Time Channel, although useful, is not relevant to what I’m discussing in this article. Now that you have an IFTTT account and have activated the necessary Channels, the next piece of the puzzle is to give IFTTT some instructions about what you want it to do for you. IFTTT calls these instructions a Recipe.

In an attempt to make it REALLY simple, IFTTT leads you baby step by baby step through the seven-step process of creating a Recipe. First you are going to “choose a trigger” by selecting the EMail Channel. For step two, be sure to choose "Send IFTTT an email tagged” and NOT “Send IFTTT any email.”

Step three is very important. This is where you choose what will be the subject of the e-mail that you will be sending to IFTTT in order to have it perform some action with your WeMo device. For turning on and off the lamp plugged into the WeMo Switch in my home office, I chose the tag “#office” (without the quote marks, of course, but WITH the hashtag).

As I am writing this, I am going through the process of creating a Recipe both on the IFTTT Web site and with the IFTTT iOS app. The steps are pretty much the same on both platforms, but I did find one important difference. On the Web site, you have to include the hashtag (#) with your keyword. But in the iOS app, IFTTT adds the hashtag for you, so if you enter a hashtag yourself, you will end up with TWO hashtags.

Now that we have finished setting up the “If This...” part of the Recipe, in step four you will “choose an action” by selecting the appropriate Channel for your WeMo device. In my case, I chose the WeMo Switch Channel.

In step five, you choose one of the actions (there are five of them at the time of this writing) which you want IFTTT to perform for you. Rather than having one Recipe to turn on my lamp and another Recipe to turn it off, it seemed much more convenient to go with the “Toggle on/off” Action. That way, if my lamp is off, this Recipe will turn it on. And if my lamp is on, this same Recipe will turn it off.

Because I have more than one WeMo Switch, step six involves choosing which Switch I want to control with this Recipe. If you have only one WeMo device of a certain type, IFTTT may skip this step, but I have no way of testing that, since I have two devices. Here I chose the WeMo Switch for my office lamp. IFTTT also gives me the option to perform the chosen action on “All Switches.”

If there are NO WeMo devices listed here, then it’s likely you don’t have the “Remote Access” or “Connect to IFTTT” options set up properly in your iOS WeMo app. Go back to the previous section and make sure that you have things in working order. As part of the troubleshooting process, you may want to edit your IFTTT WeMo Channel(s) and re-enter the PIN generated by the iOS WeMo app.

The seventh and final step is to accept or modify the Recipe Title which IFTTT generated, decide if you want to receive notifications when this Recipe runs, and then click on the Create Recipe button. Voilà! You have just cooked up your first IFTTT Recipe! Bon appétit! Before we go on to the next section, where we will learn how to use Siri to execute our IFTTT Recipe, it would be good to take a few moments to verify that the Recipe you just created actually works.

Open up your e-mail app, and from whichever e-mail account you told IFTTT about when you set up the Email Channel, send an e-mail to trigger@recipe.ifttt.com. For the subject, put the hashtag you created in step three above (in my case, the subject would be #office). It is important to include the hash character (#), because if I leave it off and put only office as the subject, the Recipe will not be executed.

You don’t need to put anything in the body of the e-mail. After you send the e-mail, give IFTTT a bit of time to perform your request. In some cases your WeMo device will be controlled almost immediately, but in other cases I’ve noticed it can take up to 30 to 60 seconds, or even longer. I think it must depend on how many other Recipes IFTTT is processing right at that moment — perhaps thousands every minute?

If you were able to execute your Recipe via e-mail, and your WeMo device switched on, or off, or whatever, then you have successfully put together all of the pieces of the puzzle thus far. That means you are ready to use Siri and AppleScript to send the e-mail to IFTTT for you, rather than you having to send it manually. Let’s dig into the details! Because you have already copied or downloaded my SiriListener AppleScript, and because you have already completed the tutorial in the first article of this series, you should already have the following section of AppleScript code:
    ***  SECTION OF COMMANDS TO CONTROL WEMO DEVICES VIA IFTTT  *********************************************************
    else if exists note "Office" then
      delete note "Office"
      tell application "Mail"
        set theMessage to make new outgoing message with properties {subject:"#office", visible:false}
        tell theMessage to make new to recipient with properties {address:"trigger@recipe.ifttt.com"}
        -- tell theMessage to set sender to "FirstName LastName <username@gmail.com>"
        send theMessage
      end tell

    else if exists note "Bedroom" then
      delete note "Bedroom"
      tell application "Mail"
        set theMessage to make new outgoing message with properties {subject:"#bedroom", visible:false}
        tell theMessage to make new to recipient with properties {address:"trigger@recipe.ifttt.com"}
        -- tell theMessage to set sender to "FirstName LastName <username@gmail.com>"
        send theMessage
      end tell
If for some reason you had deleted this section from SiriListener as you were modifying it for your own use, you can simply select and copy the above code, and then paste it back into your script. Now let’s consider this code line by line.

As we saw last time, the statement  else if exists note "Office"  checks with the Notes app on your Mac to see if there is a note containing the single word “Office”. If so, the next statement deletes that note. Of course, you will want to replace my word “Office” with whatever word you want to tell Siri so that this operation is performed.
I have a confession to make. In the five and a half years that I have owned a Mac, I have never used the Apple Mail app. That’s because during the many years before I switched to a Mac, I had been using Mozilla Thunderbird on my Microsoft Windows machines.

Unfortunately, you can’t use AppleScript to send an e-mail with Thunderbird. Therefore, the easiest solution was to use Apple Mail for sending e-mails to IFTTT. Because I didn’t want my everyday e-mail accounts to be used in both Thunderbird and Apple Mail, I decided to set up a totally separate e-mail account just for use with IFTTT and Apple Mail.

On the other hand, if you are one of the multitude who use Apple Mail as their e-mail app, that is no problem either. And it doesn’t matter if you have a dedicated e-mail account for use with IFTTT, or if you are using your normal, everyday account. The  tell application "Mail" ... end tell  block of code creates our e-mail message and sends it to IFTTT. The first statement inside the block creates a new message with a subject of “#office”. Be sure to change this hashtag to whatever you chose while setting up IFTTT in the earlier steps.

This new message is assigned to a variable I’ve called “theMessage”, so it can be manipulated by the following statements. Also, notice that we are giving this message some properties, which are enclosed in { } curly braces, and separated by commas.

The property “visible” requires a little bit of explanation. If the Apple Mail app is hidden behind other app windows, you won’t notice any difference between “visible” being set to true or false. But if you can see the Apple Mail app when this script is run, then if “visible” is set to true, you will see a new mail message very quickly created and then sent. If “visible” is set to false, the e-mail is sent without any visible indication. I prefer the more discreet, hidden method.

Because the new e-mail message we just created (but have not sent yet) can be references via the variable “theMessage”, the statement — which starts with tell theMessage — is directed to the message itself. In this statement we are setting the recipient of the e-mail, which MUST be trigger@recipe.ifttt.com. The next statement was not in my original SiriListener script, but I added it as I was writing this article. You will notice that it beings with two hyphens  -- , which means it has been commented out. That means this statement has been disabled.

If you need to use it, you can delete the two hyphens so that the statement will be executed. If you don’t need it, you can leave the hyphens as they are. You could delete the entire statement if you don’t need it, but it might be better to leave it there in case you need it in the future.

How do you know if you need to uncomment this statement or not? I’m glad you asked! If you are using only one e-mail account with Apple Mail, then you can leave the comment hyphens as they are. But if you have two or more e-mail accounts in Apple Mail, you must remove the hyphens and modify the statement details.

In order for IFTTT to run your Recipe, you MUST send it an e-mail from the SAME address that you gave IFTTT when you set up the EMail Channel. Because there is no guarantee that Apple Mail will choose the correct address, you need to specify it in the script.

You need to modify the statement, replacing “FirstName” and “LastName” with the name on the e-mail account you are using with IFTTT. Then change the “username@gmail.com” text to the actual e-mail address you are using. Make sure NOT to erase the angled brackets < >. The final statement of the  tell application "Mail" ... end tell  block actually sends the e-mail message. Save and run you SiriListener.app script. Then give Siri the command that executes this block of code — in my case that would be “Make a note about office.”

If everything has been set up properly, Siri will make a note, then Apple’s iCloud service will copy that note to the Notes app running on your Mac.

Next, the SiriListener.app on your Mac will find that note, match it to relevant section of the script, and give the Apple Mail app the details of the e-mail message to send. Apple Mail will send that message through your e-mail provider’s outgoing mail server. The e-mail will be routed over the Internet until it reaches the IFTTT mail server. Then their system will match the e-mail to your IFTTT Recipe.

I’m not quite sure about the rest of the journey through cyberspace, but I would assume that Belkin runs some sort of WeMo Internet service which knows — because you have enabled Remote Access in your iOS Wemo app — how to get in touch with your WeMo device. IFTTT sends the appropriate command to that WeMo server, which then passes it on to your WeMo device, which executes the command.

Through all that sci-fi-like cyber-journey, your voice command to Siri is able to turn on and off a lamp in your house! I hope you have fun playing with and expanding upon all of the amazing possibilities!

In the upcoming third part of this series, I will explain how you can use SiriListener to remote voice control Bluetooth devices used with your Mac, as well your Mac’s audio output devices — see Control Mac Bluetooth and Audio With Siri. In that article I will also be offering a new, updated version of SiriListener which incorporates these new features, plus other recent improvements I have made to the script.

This series ends with part four: AppleScripting the Mac User Interface, in which I explore some of the techniques I used to write the SiriListener script, and some of the programming challenges I had to overcome. But before that, I want to take a little detour by exploring a little-known capability of the Mac to create virtual aggregate audio output devices — see Mac Aggregate Speakers For Super Stereo.

Once you have mastered the voice control of your WeMo devices, it will be time to apply your newly-developed skills to a more challenging task: flying an airplane!
Read the accompanying article on CNN’s Web site: Ready for pilots to fly your airliner by voice commands?
This article is 12th a series of articles on this Web site related to Technology and Computing which also includes (scroll to see the entire list):
26  Oct  2010
11  Jan  2014
29  Jan  2014
5  Feb  2014
7  Feb  2014
14  Feb  2014
15  Feb  2014
16  Feb  2014
17  Feb  2014
1  Nov  2014
Using Siri to Control WeMo Devices
12  Nov  2014
20  Nov  2014
22  Nov  2014
2  Dec  2014
6  Dec  2014
Reader Comments
There are no reader comments for this blog entry.
Article Index     |     Search     |     Site Help