Using Alexa and openHAB 2 to Control Your Home

Today we’re going to have some fun and try using Amazon Alexa to control our light.

You will need:

 

This update is going to be quite simple as openHAB has done pretty much all the work for us. There is also a comprehensive guide here.

myopenhab.org

The way openHAB have implemented their Alexa integration is via their cloud hosted service https://myopenhab.org/ this is great for us as most of the hard work is done. Some people may not like the fact we’re using an online service, but hey why are you using Alexa?

First we need to install the openHAB Cloud Connector

  1. So back to the Paper UI at http://openhabianpi:8080.
  2. Navigate to Add-ons in the left hand menu, then the MISC tab.
  3. Find openHAB Cloud Connector in the list and install it.cloud connector
  4. Lastly we need to check configuration Configuration>Services>IO>openHAB Cloud.cloud connector config

Now the cloud connector is installed we can create our account over at https://myopenhab.org/.

To register we’re going to need a couple of bits of information the UUID and Secret.myopenhab register Fortunately they are easy to find.

  1. To get the UUID ssh into your openHABian machine.
  2. Execute the following command cat /var/lib/openhab2/uuid.
  3. Copy the string of characters shown.
  4. Next get the secret in a similar way cat /var/lib/openhab2/openhabcloud/secret.
  5. You can now register for your account at https://myopenhab.org/.
  6. Annoyingly I needed to reboot in order for my device to show up.

You can now remotely access your openHAB UI.

Items

The Alexa integration uses a tagging syntax to identify which items it can control.

We need to edit our items file: \\OPENHABIANPI\openHAB-share\openhab2-conf\items\sonoff.items, and add the Lighting tag:

Switch My_Light "My Light" <light> (gLight) ["Lighting"]
 { mqtt=">[mybroker:cmnd/sonoff-mylamp/power:command:*:default],
 <[mybroker:stat/sonoff-mylamp/POWER:state:default]" }

Alexa

Lastly in you Alexa app navigate to Skills and search for openHAB, and tap Enable. You will be prompted to enter the username and password you just used to create your myopenhab.org account, and authorise the Alexa to use the service.

Alexa will spend a few minutes searching for devices then you’re good to go.Alexa Items.PNG

Try saying “Alexa turn on my light”.

Alexa Groups

One last tip. The Alexa app will allow you to create smart home groups. These can be used to associate a number of devices with a specific Echo device.

Whats the point? Well lets create a group for the room we’re in, in my case the study. Then add the Echo Dot located in my study to the group, and also our light.

Now we can control all the lights in a room together. Even better the Echo knows which group it’s part of so can control the lights without you having to define which ones.study group.PNG

Try saying “Alexa turn the lights on”.

Lastly if you change Alexa’s wake word to Computer, you can say “Computer turn the lights on”. It’s almost we’re living Star Trek!

DIY Home Automation with openHAB 2 – Part 4

This is my first blog of 2018 and it’s a real struggle getting back into things. So let’s start the year gently by taking a look at some of the configuration we did last time.

openHAB Items

Last time we created an items file: \\OPENHABIANPI\openHAB-share\openhab2-conf\items\sonoff.items, and added the following lines:

Switch My_Light "My Light" <light> (gLight)
 { mqtt=">[mybroker:cmnd/sonoff-mylamp/power:command:*:default],
 <[mybroker:stat/sonoff-mylamp/POWER:state:default]" }

Lets examine what this actually does.

Switch My_Light "My Light" <light> (gLight)
 { mqtt=">[mybroker:cmnd/sonoff-mylamp/power:command:*:default],
 <[mybroker:stat/sonoff-mylamp/POWER:state:default]" }

Switch – defines an item of type switch, in our case a light.

Switch My_Light "My Light" <light> (gLight)
 { mqtt=">[mybroker:cmnd/sonoff-mylamp/power:command:*:default],
 <[mybroker:stat/sonoff-mylamp/POWER:state:default]" }

My_Light – is the unique nae of our item. I suggest you devise a naming convention to make locating items easier.

Switch My_Light "My Light" <light> (gLight)
 { mqtt=">[mybroker:cmnd/sonoff-mylamp/power:command:*:default],
 <[mybroker:stat/sonoff-mylamp/POWER:state:default]" }

“My Light” – is the textual label for our item. This is used by UIs and, as we’ll find out later, by voice control interfaces like Amazon Alexa.

Switch My_Light "My Light" <light> (gLight)
 { mqtt=">[mybroker:cmnd/sonoff-mylamp/power:command:*:default],
 <[mybroker:stat/sonoff-mylamp/POWER:state:default]" }

<light> – tells openHAB which icon set to use in the UI. Icon sets can use the item state to alter which icon is displayed, you may have noticed a lit or unlit bulb displayed, depending on whether the Sonoff is on or off.

Switch My_Light "My Light" <light> (gLight)
 { mqtt=">[mybroker:cmnd/sonoff-mylamp/power:command:*:default],
 <[mybroker:stat/sonoff-mylamp/POWER:state:default]" }

(gLight) – tells us that this item is part of the group gLight. Groups are used to access collections of items together, in our case all the lights, but it could also be all the items in the living room. Items can be members of multiple groups, and also inherit group membership from their parents. I suggest you give some thought to how you intend to structure your smart home, I may publish a blog on this at some point.

Switch My_Light "My Light" <light> (gLight)
 { mqtt=">[mybroker:cmnd/sonoff-mylamp/power:command:*:default],
 <[mybroker:stat/sonoff-mylamp/POWER:state:default]" }

{…} – lastly the statements in the curly braces are the binding. In our case these are an MQTT command to toggle the state of our lamp and and MQTT status update so we can see whether it’s on or not.

Hopefully the above has given you some insight into what we did at the end of the last blog. There is loads more you can do with items in openHAB, take a look here for the complete guide.

Other Items

What other things can we do with items?

WiFi Signal Strength – RSSI

By adding the following to the items file: \\OPENHABIANPI\openHAB-share\openhab2-conf\items\sonoff.items, we can view the signal quality between the Sonoff and your wifi access point. Note the use of the JSONPath binding to transform the returned JSON into a number.

Number My_Light_RSSI "My Light: RSSI [%d %%]" <qualityofservice> (RSSI)
 { mqtt="<[mybroker:tele/sonoff-mylamp/STATE:state:JSONPATH($.Wifi.RSSI)]" }

Adding this to your sitemap file: \\OPENHABIANPI\openHAB-share\openhab2-conf\sitemaps\default.sitemap,

Text item=My_Light_RSSI label="My Lamp Signal"

Will result in something like this, in the basic UI http://openhabianpi:8080. It may take a few minutes to get a value, as state updates are sent periodically.

signal

Firmware Version

Display the current firmware version.

\\OPENHABIANPI\openHAB-share\openhab2-conf\items\sonoff.items

String  My_Light_Version "My Light: version [%s]" (Version)String  My_Light_Version "My Light: version [%s]" (Version)
 { mqtt="<[mybroker:stat/sonoff-mylamp/STATUS2:state:JSONPATH($.StatusFWR.Version)" }

\\OPENHABIANPI\openHAB-share\openhab2-conf\sitemaps\default.sitemap,

Text item=My_Light_Version label="My Light Version"

Unfortunately going to the basic UI http://openhabianpi:8080 will not display  any value yet. This is because status2 messages are only sent on request.

So we also need to prompt our Sonoff to send a satus2 message.

\\OPENHABIANPI\openHAB-share\openhab2-conf\items\sonoff.items

Switch My_Light_Version_Query "My Light: version query" (Version)
 { mqtt=">[mybroker:cmnd/sonoff-mylamp/status:command:*:2]" }

\\OPENHABIANPI\openHAB-share\openhab2-conf\sitemaps\default.sitemap,

Switch item=My_Light_Version_Query label="My Light Version Query"

Clicking this switch will now prompt the Sonoff to return a status2 message, from which we can retrieve the firmware version.

version

Reachable

Something we may need to do, is see which of our devices are offline. This is handled by the LWT last will and testament message.

 \\OPENHABIANPI\openHAB-share\openhab2-conf\items\sonoff.items

Switch My_Light_Reachable "My Light: reachable" (Reachable)
 { mqtt="<[mybroker:tele/sonoff-mylamp/LWT:state:MAP(reachable.map)]" }

\\OPENHABIANPI\openHAB-share\openhab2-conf\sitemaps\default.sitemap

Switch item=My_Light_Reachable label="My Lamp Reachable"

You may notice the problem, the LWT message payload “Online” or “Offline” is not understood by the Switch item type. The way we get round this is to create a map to convert the text.

Create a map file: \\OPENHABIANPI\openHAB-share\openhab2-conf\transform\reachable.map, and add:

Online=ON
Offline=OFF

Now you can display which of your Sonoff devices are connected, their signal strength and firmware version.