openHAB and lightwaveRF light dimmer, Philips Hue, node red & ha-bridge

Dimming a LightwaveRF with MQTT (Node Red and openhab integration)

When I bought my emonpi, I had no real idea about some of the installed software, but became aware of the nodeRed and openHAB installs and realised I could integrate lighting with it so I went out an fitted a lightwaveRF dimmer socket (ok so first I bought a cheap slave unit rather than the master I should have in the first place, maybe I should do more background reading).

I ordered and soldered in the 433Mhz transmitter (although it’s not clear to my mind in the instructions about the solder bridge for the antennae). The integration went fairly well, although no-one really documents how to pair the device - it just seemed to work. I did a crash course in node red following the
[blog posting (remote-control-of-lightwave-rf-plugs)] (Remote Control of LightWave RF plugs via MQTT using emonPi with OOK Tx - Blog | OpenEnergyMonitor) and was then able to turn the light on and off and read the debug messages. I also (eventually) got the light to turn off when our power consumption went over a certain threshold and back on when it fell below, but the family quickly got tired of that little trick.

Downloading the openHAB client for my phone and connecting to emonpi was fairly straightforward and it just worked with the default sitemap so that was great.

However, what I wanted to do was dim the light via openHAB. According to various notes the MQTT message should be something like “0 1 31” - however in nodeRed that didn’t work - it wasn’t until I looked at the code for the code for the arduino integration part in git I wondered what the right shift & was doing:

void lw_cmd(byte level, byte channel, byte cmd, byte* id) {
  byte msg[10];
  
  msg[0] = lw_nibble[level >> 4];
  msg[1] = lw_nibble[level & 0xF];

I tried to calculate what it should be doing with the lw_nibble array, but sort-of guessed the range was about 128 to 159

Sending to MQTT in nodeRed “0 1 128” did indeed dim the light, and “0 1 159” un-dimmed it.

So all I have to do is learn openHAB …

I thought the Dimmer/Slider was the best choice but couldn’t get it to work, but anyway - what follows, should it be of interest, is my wild stab and getting something to work - and bear in mind I have to translate 0-100 percent to 128-159

There’s plenty more to do to finesse this, like turn off the light at the bottom of the dimming range (you can see the attempt to update the icon in the rule), maybe a gradual fade - and to understand why it doesn’t seem to work in the browser version of openHAB rather than just in the mobile client. Ideally I’d make the rule more general so it didn’t apply only to a fixed device ID (“0” in this case).

My way of mapping the MQTT output to a string item is really me just showing the limits of my openHAB knowledge and prodding it with a stick.

Hope it might help somebody…

Sitemap:

Slider item=lwrf0d     label="Room Dimmer [%d%%]" icon="slider"

items:

Dimmer lwrf0d  "Dimmer Level [%d%%]"
String lwrf0dstr  {mqtt=">[mosquitto:lwrf:command:*:default]"}


Rule:

rule "Dimmed Light"
        when
                Item lwrf0d received update
        then
                if(lwrf0d.state instanceof DecimalType) percent = lwrf0d.state as DecimalType

                if(percent == 0 {
                  lwrf0.sendCommand("OFF");
                } else {
                  lwrf0.sendCommand("ON");
                }

  val input_range = 100; // 100 - 0 percentage
  val output_range = 31; // 159 - 128 dimming range

  var Number output = (percent - 0)*output_range / input_range + 128;
//   logInfo("openhab", "Output value is: " + output.intValue, lwrf0d.state);
    sendCommand(lwrf0dstr, "0 1 " + output.intValue);
end

Setting up OpenHab with Philips Hue Binding

Okay so I’ve just added a Philips Hue bridge and bulb to OpenHab.

Here is the OpenHab part (note this is for the current openHab 1.x version as installed on the SD card).

in /usr/share/openhab/addons you can see the current addons, so you need to add a binding.

rpi-rw
sudo apt-get install openhab-addon-binding-hue

Add to ~/oem_openhab/openHab.cfg

################################ HUE Binding #########################################

# IP of the Hue bridge
hue:ip=192.168.1.68
hue:secret=<enter secret here>
hue:refresh=10000

Now you need to obtain the secret -

Here is the Philips documentation:

The way to get the authorised user or “string” or whatever you call it from the hub came from this posting:
https://community.openhab.org/t/openhab-not-authorized-to-access-hue-bridge-solved/10204/4

Basically you go to your Phillips Hub IP address (find the IP from your router) and go to the following URL and filling the following details and copy and paste the string into your openhab.cfg and restart openhab

http:///debug/clip.html

|URL| /api|
|Body|{"devicetype":"my_hue_app#my_username"}|
|Method|POST|

sudo service openhab restart

Watch the logs
tail -f /var/log/openhab/openhab.log
Ctrl-C to exit once you’ve seen it start without error.

I added a fairly simple configuration in ~/oem_openHab/oem.items
/* Philips Hue Devices */
Switch Light_MYNAME (Switching) {hue=“1”}
Dimmer Light_MYNAME_B (Dimming) {hue=“1;brightness;20”}

Set up the oem.sitemap (under the existing light section):

Switch item=Light_MYNAME  label="Hall Light Switch"
Slider item=Light_MYNAME _B label="Hall Light Brightness"

And modified the all-on/off oem.rules (eg):

rule "Switch all LWRF sockets"
when
    Item lwrf_all received command
then
    lwrf0.sendCommand(receivedCommand)
    Light_MYNAME .sendCommand(receivedCommand)

HA-Bridge with Alexa to control Philips Hue bulbs

Here is the less successful part - I’ve not been able to discover (using an Echo Dot gen-2) my ha-bridge.

You might note “Why would you use an HA-Bridge if you already have a Philips Hue Bridge?” - well the original idea was to control a lightwareRF device via MQTT - which was fairly straightforward to setup as a device. I had issues coming up with the right setup to add Philips Hue bulbs to the bridge in an attempt to see if it might prompt the Echo Dot to discover the Lights - turned out that was a completely separate problem (see next post), and if your ha-bridge is working adding an MQTT device is fairly straightforward.

Install instructions are here:https://github.com/openenergymonitor/emonpi-ha-bridge

I’ve upgraded by pulling down later .jar files and updating the /etc/systemd/system/habridge.service file to reference it.

And the bit I found a bit tricky was setting up the devices.

I add the following for how to do it via HTTP (and note the creation of the secret in the post above):

On Items (light 1 - for Off, change the body to {“on”:false}):

Item: HTTP Device
Target Item: http://<bridge-ip>/api/<secret>/lights/1/state
HTTP Verb: PUT
HTTP Body: {"on":true}
Content Type: application/json

Dim Items:

Item: HTTP Device
Target Item: http://<bridge-ip>/api/<secret>/lights/1/state
HTTP Verb: PUT
HTTP Body: {"on":true,"bri":"${intensity.percent}"}
Content Type: application/json

I don’t know the “proper” way of doing it, so any notes gratefully received.

So ha-bridge eventually worked - but I was having trouble with discovery - I had to do a full reset of my Echo Dot in order for it to do discovery via voice. It works now.

Node Red with Alexa Skill to control MQTT

In the meantime I used Node Red to control the lightwaveRF devices after enabling the node red alexa home skill by Ben Hardill - and note this won’t work without upgrading node red.

I nearly killed my emonpi doing this so approach with extreme caution and don’t blame me if you do this and it kills your existing setup. Indeed once I had done it I had to go back and re-install npm packges and setup my config nodes again YOU HAVE BEEN WARNED.

Note first I did a apt-get install nodered to the later version, and killed off the install half-way - I shouldn’t have it takes a very long time - it took a bit of effort to unwind that error.

For reference here is the emonpi nodered readme:
https://github.com/openenergymonitor/oem_node-red

For the Node Red upgrade information, see: https://nodered.org/docs/hardware/raspberrypi.html

For the Alexa Skill, see and follow the documentation (and perform these steps first to enable the skill within Alexa and do the pre-requisite setup, it requires linking an account):
https://alexa-node-red.bm.hardill.me.uk/

rpi-rw

Update Node red THIS TAKES A VERY LONG TIME (and places a large amount of load on your pi) - let it complete:

update-nodejs-and-nodered
cd ~/data/node-red

Re-install the old flows/nodes/modules/nodes/whatever they’re called:

sudo npm install node-red-node-emoncms
sudo npm install node-red-node-weather-underground

Install the new nodes into the palette

sudo npm install node-red-contrib-alexa-home-skill

Here is a very primitive outline I used, and requires much finessing - and note you’ll have to setup your config nodes with appropriate values.

[{“id”:“a57c9491.129f18”,“type”:“function”,“z”:“7e01e2a7.13695c”,“name”:“On”,“func”:“return {payload:‘0 1’}\n”,“outputs”:1,“noerr”:0,“x”:690,“y”:260,“wires”:[[“eb0a2eea.1b36a”]]},{“id”:“9ffe98d6.7b4128”,“type”:“function”,“z”:“7e01e2a7.13695c”,“name”:“Off”,“func”:“return {payload:‘0 0’}”,“outputs”:1,“noerr”:0,“x”:690,“y”:320,“wires”:[[“eb0a2eea.1b36a”]]},{“id”:“aedfd891.c587b8”,“type”:“debug”,“z”:“7e01e2a7.13695c”,“name”:"",“active”:true,“tosidebar”:true,“console”:false,“complete”:“false”,“x”:370,“y”:220,“wires”:[]},{“id”:“f49a689d.2471e8”,“type”:“switch”,“z”:“7e01e2a7.13695c”,“name”:"",“property”:“command”,“propertyType”:“msg”,“rules”:[{“t”:“eq”,“v”:“TurnOnRequest”,“vt”:“str”},{“t”:“eq”,“v”:“TurnOffRequest”,“vt”:“str”},{“t”:“eq”,“v”:“SetPercentageRequest”,“vt”:“str”}],“checkall”:“true”,“repair”:false,“outputs”:3,“x”:350,“y”:320,“wires”:[[“a57c9491.129f18”],[“9ffe98d6.7b4128”],[“c96ca97f.b9e5c8”]]},{“id”:“7b949556.328cdc”,“type”:“debug”,“z”:“7e01e2a7.13695c”,“name”:"",“active”:true,“tosidebar”:true,“console”:false,“tostatus”:false,“complete”:“command”,“x”:380,“y”:160,“wires”:[]},{“id”:“c96ca97f.b9e5c8”,“type”:“range”,“z”:“7e01e2a7.13695c”,“minin”:“0”,“maxin”:“100”,“minout”:“128”,“maxout”:“159”,“action”:“scale”,“round”:true,“property”:“payload”,“name”:"",“x”:530,“y”:380,“wires”:[[“b8e01cdb.6a52f”]]},{“id”:“b8e01cdb.6a52f”,“type”:“function”,“z”:“7e01e2a7.13695c”,“name”:“Dim”,“func”:“return {payload: '0 1 ’ + msg.payload}”,“outputs”:1,“noerr”:0,“x”:690,“y”:380,“wires”:[[“eb0a2eea.1b36a”]]},{“id”:“eb0a2eea.1b36a”,“type”:“mqtt out”,“z”:“7e01e2a7.13695c”,“name”:“Lightwave MQTT”,“topic”:“lwrf”,“qos”:"",“retain”:"",“broker”:“396670b0.0ef31”,“x”:960,“y”:320,“wires”:[]},{“id”:“b7f75364.ec43c”,“type”:“alexa-home”,“z”:“7e01e2a7.13695c”,“conf”:"",“device”:"",“acknoledge”:true,“x”:110,“y”:320,“wires”:[[“f49a689d.2471e8”,“aedfd891.c587b8”,“7b949556.328cdc”]]},{“id”:“396670b0.0ef31”,“type”:“mqtt-broker”,“z”:"",“name”:“MQTT localhost”,“broker”:“localhost”,“port”:“1883”,“clientid”:"",“usetls”:false,“verifyservercert”:true,“compatmode”:true,“keepalive”:“15”,“cleansession”:true,“willTopic”:"",“willQos”:“0”,“willRetain”:“false”,“willPayload”:"",“birthTopic”:"",“birthQos”:“0”,“birthRetain”:“false”,“birthPayload”:""}]