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.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s