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