This is my working code for turning ON/OFF, setting color + brightness of IKEA Tradfri color bulbs using Z2M only with colorpicker in Openhab. I do not have access to Philips HUE bulb, so can’t assure that this is also working for them.
Items (MQTT 1.x binding):
Color LG_IKEA_Color_1_RGB "Color lamp [%s]" (gI,gLights) [ "Lighting" ] { mqtt=">[wrt:zigbee2mqtt/ikea_led_color_1/set:command:*:JS(HSBtoRGB.js)]" }
String LG_IKEA_Color_1_LWT "Color LWT [%s]" (gLights) { mqtt="<[wrt:zigbee2mqtt/ikea_led_color_1/availability:state:default]" }
Extract from sitemap:
Default item=LG_IKEA_Color_1_RGB visibility=[LG_IKEA_Color_1_LWT=="online"]
JS Transformation file HSBtoRGB.js:
(function (x) {
if (x=="ON" || x=="INCREASE") {
return '{"state":"ON"}'
} else if (x=="OFF" || x=="DECREASE") {
return '{"state":"OFF"}'
} else {
var tex = x.split(',');
s = parseInt(tex[1]);
v = parseInt(tex[2]);
h = parseInt(tex[0]);
h=(!h ? 0 : h/360.0);
s=(!s ? 0 : s/100.0);
v=(!v ? 0 : v/100.0);
i = Math.floor(h * 6);
f = h * 6 - i;
p = v * (1 - s);
q = v * (1 - f * s);
t = v * (1 - (1 - f) * s);
switch (i % 6) {
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
return '{"brightness":'+ Math.round(v * 255)+',"color":{"rgb":"'+ Math.round(r * 255)+','+ Math.round(g * 255)+','+Math.round(b * 255)+'"}}'
}
})(input)
This way, with only one item for brightness and color, I can change color/brightness/on/off using Google Assistant (Hey G, set color lamp to yellow
).
Please notice, that Openhab command ON
and INCREASE
are treated in the same way (and OFF
and DECREASE
too), so there is no possibility to brighten/darken light saying Hey G, brighten the color lamp
. I do not know Z2M command to just increase/decrease brightness without providing brightness value.