User Tools

Site Tools


2016:ha:control_openhab_with_alexa

CONTROL OPENHAB ITEMS USING Alexa

Original blog is here:

INTRODUCTION

Having a digital personal assistant, like Jarvis from Iron Man, is the ultimate geek dream. Over the past few years, most of the major tech companies have attempted to turn this dream into reality. Apple has Siri, Microsoft has Cortana, and Google has Google Now. Even some smaller companies have tried their hand: SoundHound recently launched their Hound app which focuses on natural language processing and a speedy response to your queries. All of these companies have something in common, though. They’re all focused on using your mobile phone as the input device.

Amazon has taken a different approach with their Echo “smart speaker”. The Echo is a stylish, cylindrical speaker which is meant to be displayed on a coffee table, kitchen counter, or your nightstand. However, the important part of Echo is what it’s doing: listening. Simply speak the trigger word at any time (“Alexa” by default) to make the Echo spring to life and await your command. That’s why it’s the perfect home automation controller. It’s hands-free!

In this post, I’m going to show you how to control your OpenHAB items using the Amazon Echo. Let’s begin!

PREREQUISITES

First thing’s first. Get your Echo all set up and decide where you’d like it to sit. I played around with mine for about 15 minutes before I decided to get down to business and set it up with OpenHAB.

You’ll need to download a great little piece of software from GitHub named Amazon Echo HA Bridge. The author, Armzilla, uses this to control a Vera from his/her Echo, but we’re going to use it to control OpenHAB. Download the latest release to your OpenHAB PC. I’m using version 0.1.2. You’ll also need Java 8 installed in order to run the software.

Finally, we’ll need a Chrome extension named Postman. We’ll use this to upload our settings into the Echo HA Bridge. You may use another REST client if you wish, but I won’t cover that in this guide.

STEP 1: RUN THE AMAZON ECHO HA BRIDGE

Okay, enough beating around the bush. Let’s get into it! The first thing we need to do is run our Amazon Echo HA Bridge. Open a command prompt window and navigate to the folder where you stored the Amazon Echo HA Bridge .jar file. (Or use this shortcut! Navigate to the directory in Windows Explorer, then hold shift and right click in the white space. Choose option “Open command window here”. See below picture for an example!)

Then, you’ll want to enter this command in the command prompt window. NOTE: You may need to replace the .jar filename and the IP address with your own! You should enter the IP address of the PC you’re running it on.

java -jar amazon-echo-bridge-0.1.2.jar --upnp.config.address=192.168.1.88

Warning: If you’re running the Bridge on the same PC as your OpenHAB server, you’ll probably need to change the port that it listens on. This is because OpenHAB and the Echo HA Bridge both default to port 8080. To change the port the Echo HA Bridge is listening on, add this flag to the end of the command:

–server.port=<port>


After you run the command, there will be a bunch of output which you can ignore unless you run into issues.

STEP 2: LOAD CONFIGURATION DATA INTO THE AMAZON ECHO HA BRIDGE

This is where the Postman Chrome extension comes into handy. We need to upload our configuration data in JSON format using a RESTful API. Don’t freak out! It’s not that hard. I’ll walk you through it.

When you open Postman, you need to make sure the settings are correct. You need to make sure you’re in POST mode, have the correct address filled in. It should be

http://<ip address>:8080/api/devices)

and have raw data and JSON selected. Questions? Refer to the picture below! I’ve highlighted all of the critical areas.

Now we need to post the data. Refer below for an example of a setting I’m using. Lights is an OpenHAB Group that turns all of my lights either on or off. “All the lights” is the string that your Echo will react to. Make sure this is a good description for your light or item.

Examples could be “deck light”, “living room light”, “hallway light”, or even something cool like “netflix” if your OpenHAB is able to control your TV!

Note that if you’re running the Echo HA Bridge on a different PC than your OpenHAB, you’ll need to replace both spots where it says “localhost” with your OpenHAB IP address. You also may need to change the ports from 8081 to something different, depending on which port your OpenHAB is listening on.

{
"name" : "all the lights",
"deviceType" : "switch",
"onUrl" : "http://localhost:8081/CMD?Lights=ON",
"offUrl" : "http://localhost:8081/CMD?Lights=OFF"
}


When you are finished making your changes, click “Send” on Postman. It will scroll down and show you some output from the HA Bridge so you know it worked. You’ll need to scroll back up and repeat the process for each and every device you want to control with your Amazon Echo.

When you’re finally done uploading devices, go to Chrome or your browser of choice and navigate to

http://<Amazon HA Bridge IP>/api/devices.  

You should see all of your loaded devices there.

STEP 3: LOAD SETTINGS INTO YOUR AMAZON ECHO

On your phone, launch the Echo app. Open the Settings page and scroll all the way down where it says “Connected Home”. Click the button that says “Add new devices” and wait! The Echo should find all of the devices you uploaded into the Echo HA Bridge. Congrats! Now you can tell your Echo to “turn the kitchen light on” or “turn all the lights on”, or whatever you configured! Kick back and tell Alexa what you want her to do while you’re watching TV, entertaining friends, or doing the dishes!

Comments

One thing to add is on Linux at least you have to open port 1900 UDP for Alexa to pick up the device list.

Why aren’t you just using the built in configuration tool?

http://yourip:8080/configurator.html

(Honest question – I’ve seen the POST approach elsewhere and don’t understand why all these tutorials neglect to mention the easier config method. Seems to work fine for me.)

It’s probably because the Echo HA Bridge isn’t documented very well :P. I didn’t even know about a Configuration page! On the GitHub readme, the instructions are to POST JSON to /api/devices, but it doesn’t make any mention of a Configuration tool. That’s why I used Postman.

I’ll check it out. I’d love to update the article with a simpler process to make it more accessible. Thanks for the tip!

All I want to accomplish is the ability to control actions on my Harmony Hub like turn on TV, etc. I think I have the echo-ha-bridge running but I cant wrap my head around installing the OPENHAB and adding the Harmony, whats the easiest way to do this? I have the following items as well to choose form if needed: MAC OSX Server box, Physical HUE Hub, WINK Hub, Harmony Hub, Android Tablet.

Any direction is appreciated. I take direction well and I am pretty tech savvy.

This question is probably too broad to answer in a comment (especially because I don’t have a Harmony Hub), but I’ll do my best.

This is a somewhat ugly solution, but since I don’t have a Harmony Hub it’s the best I can come up with for now. I’ll be using a very simple example of two activities “Power Off” and “Watch TV”. These activities should already be defined on your Harmony Hub. I have no way of testing this so there may be an error or two. Come back if it doesn’t work and I’ll try to help.

Here’s a general overview of what you’ll need to do:

  1. Download the OpenHAB runtime with demo configuration and unzip it
  2. Download the OpenHAB Addons and place org.openhab.binding.harmonyhub-1.7.0.jar into your /openhab/addons folder.
  3. Open openhab.cfg in the /openhab/configuration/ folder and add the following lines:
harmonyhub:host=<local ip address of your hub>
harmonyhub:username=<your logitech username>
harmonyhub:password=<your logitech password>
  1. Open demo.items in /openhab/configuration/items/ and add the following.
Switch HarmonyTvSwitch

Open demo.rules in /openhab/configuration/rules and add the following two rules to the bottom

rule "TV On"
when
    Item HarmonyTvSwitch received command ON
then
    harmonyStartActivity("Watch TV")
end
 
rule "TV Off"
when
    Item HarmonyTvSwitch received command OFF
then
    harmonyStartActivity("Power Off")
end

Now upload the configuration into your HA Bridge. Your onUrl property should be

http://<OpenHAB IP>:<OpenHAB Port>/CMD?HarmonyTvSwitch=ON

and offUrl property should be

http://<OpenHAB IP>:<OpenHAB Port>/CMD?HarmonyTvSwitch=OFF

Here are some good resources: OpenHAB Harmony Hub Binding wiki Configuring the OpenHAB Runtime – Visit here when you want to learn more about OpenHAB. The learning curve is a little steep, but it’s by far the best home automation software I’ve ever used. Stick with it!

I downloaded the files you linked above but none of the specific files your want me to edit exist. (openhab.cfg, demo.items, demo.rules) The directories you refer to are there but not those files. Am I to be making new files with these values?

SOrry!

Reply

I made a mistake. You’ll want to download the OpenHAB demo setup instead of the OpenHAB Runtime that I originally linked. That will include the demo setup files. I’ll edit my comment to correct it.

rich says:

What am i missing here?
First I fail to see how openhab connects to the bridge. Did I miss a step. It seems like alexa connects to the bridge running and can sent rest URLs directly to the zwave devices. i don’t see where opehhab comes into play(which is the part I’m probably missing)

The bridge runs on another port, and the URL is just the rest URL of the On/Off command for the zwave device. When i ask alexa to turn off dining room light, it says it is not responding. I have the bridge set up and listening, I have it in the echo app.

OpenHAB doesn’t really connect to the Bridge. The Bridge does an HTTP GET call to OpenHAB at the appropriate time. I see that I did a poor job of explaining that, and will go back and edit the article when I get a chance.

Is your OpenHAB at http://172.16.1.241:8080? Is your item named DiningRoom_Light? If the answer to those questions are both “yes”, what happens when you open http://172.16.1.241:8080/CMD?DiningRoom_Light=100 in a browser on the same PC that the Echo HA Bridge is running on?

Can anyone confirm the server port command works? I have

java -jar amazon-echo-bridge-0.1.3.jar –upnp.config.address=192.168.1.200-server.port=8081

And the server continues to start with 8080.. did I miss something??

Thanks ahead of time

I can confirm the server port command works. I tried it before I put it in the article.

Make sure the flag has 2 hyphens in front. –server.port=8081

Learned a lot in this process and am answering my own questions for those who may have similar questions:

1) The only real way to do it is to open the Echo App, go to Settings–>Connected Home, and scroll to the bottom. Hit “Forget All Devices” (don’t worry, it will preserve persistent devices like *actual* HUE hardware). Then you can start over with posting the soft devices.

2) Definitely fully-qualified *localhost* URLs (not on port 8080), with codes in place of special characters.

Example: “onUrl” : “http://localhost/ph-cgi/formula?formula=ph_macroparm(%27IRR_ON_MANUAL%27,6,15,0,0,0)”

Aaron, this is really nice!! thanks for the write-up!! I now have my first insteon light switch working now with the echo and openhab..

I am not sure how to edit the device list. Is there an easy way to edit the api/devices? or is it easier to clear the data folder and start over?

Now if I could just get my insteon sprinklers setup in openhab, I will be set to go!!

Thanks again.

Reply
Aaron Tinsley says:

Jay,

No problem! I’m glad it helped. As far as editing the device list for the Bridge, the honest answer is I don’t know. I did my initial setup and have mostly left it alone since then. I may pore over the source code and look for the answer. The only time I’ve spent in the source code thus far was to figure out how to change the listening port.

All of the commenters have made one thing clear… I’m way behind some of you in home automation! Good luck with your sprinkler system!

Other people probably figured out how to have Alexa dim lights without needing it explained to them, but I thought I would comment and potentially save someone some time:

As of 0.1.3, the Echo-HA-Bridge supports dimming. In the Configurator page, use these URLs:

On URL: http://:8080/CMD?Light=${intensity.percent}
Off URL: http://:8080/CMD?Light=OFF

I got it working tonight with 0.2.1 and OpenHAB 1.7.1, both running on Windows 10.
Controlling TV
I’m going bananas over this :) Probably to much fiddling around. I got Openhab to work with my LG TV. Wehn I try Armzilla to connect Echo it either gives me a blanc page while testing or XML error. In my items file in openhub I have:

Group TV_Living “Tv Room” (LgTv)
Switch LgTvPower “Power Command” (GF_Living) {lgtv=”ON:Tv Room:POWER”}
Switch LgTvMute “Mute” (GF_Living) {lgtv=”ON:Tv Room:KEY_MUTE, OFF:Tv Room:KEY_MUTE”}
Number LgTvVolume “lgVolume [%.1f]” (GF_Living) {lgtv=”INIT:Tv Room:VOLUME_CURRENT, INCREASE:Tv Room:VOLUME_UP, DECREASE:Tv Room:VOLUME_DOWN, *:Tv Room:VOLUME_SET”}
Number LgTvChannel “Channel [%.1f]” (GF_Living) {lgtv=”INIT:Tv Room:CHANNEL_CURRENTNUMBER, INCREASE:Tv Room:CHANNEL_UP, DECREASE:Tv Room:CHANNEL_DOWN, *:Tv Room:CHANNEL_SET” }
String LgTvChannelName “Channelname [%s]” (GF_Living) {lgtv=”INIT:Tv Room:CHANNEL_CURRENTNAME”}
String LgTvGetChannels “getchannels [%s]” (GF_Living) {lgtv=”INIT:Tv Room:GET_CHANNELS, ON:Tv Room:GET_CHANNELS”}
String LgTvGetApps “getapps [%s]” (GF_Living) {lgtv=”INIT:Tv Room:GET_APPS, ON:Tv Room:GET_APPS”}
String LgTvAppExecute “excuteapp” (GF_Living) {lgtv=”*:Tv Room:APP_EXECUTE”}
String LgTvAppTerminate “terminateapp” (GF_Living) {lgtv=”*:Tv Room:APP_TERMINATE”}
Switch LgTvConnStatus “connstatus ” (GF_Living) {lgtv=”*:Tv Room:CONNECTION_STATUS”}

Can anyone please give me an example URL I should use to get this working? Openhab, Armzilla and Lovely Alexa so she will start Netflix :)

I tried:

{
“name” : “Netflix”,
“deviceType” : “switch”,
“onUrl” : “http://127.0.0.1:8080/CMD?TV_Living=excuteapp”,
“offUrl” : “http://127.0.0.1:8080/CMD?TV_Living=terminateapp”
}

But also:

http://localhost:8080/rest/items/LgTvAppExecute?jsoncallback=callbackapp

In my sitemap I have: (works great via webpage)

Switch item=LgTvAppExecute label=”AppExecute” mappings=[Netflix=Netflix]
Switch item=LgTvAppTerminate label=”AppTerminate” mappings=[Netflix=Netflix]

I must say, I have 0 coding experience :)

Kind regards,
Frank

Aaron Tinsley says:

Frank,

Your URL should directly address the item, not the group it’s in. I haven’t tried controlling string items (just switch items), but you can try this.

http://localhost:8080/CMD?LgTvAppExecute=[value]

[value] should be a URL-encoded string. To URL encode a string, go to this link, type your string in the “Plain” box, and then hit the button that says “URLEncode”. It will output the URL-encoded string in the “Encoded” box.

2016/ha/control_openhab_with_alexa.txt · Last modified: 2016/05/11 16:45 by 75.177.137.2