Zigbee2mqtt and Zigbee bulbs small tutorial

I have exactly the same error.

When I run

I got the following output:

zigbee2mqtt/Test_Lampe/set 0 for Off
zigbee2mqtt/Test_Lampe/set 1 for ON

I thing, the transformation does not happen.

Any ideas?

Happy New Year All!

@Seaside, @goddib, see my solution for controlling color ikea bulb (brightness, color, of, off) with just one Openhab color item: Zigbee2mqtt revisited: No more ugly transformations.

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)+'"}}'

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.

1 Like

@kluszczyn: Nice work.:+1:

Have you tried this using mqtt 2.x version?

I’m still on MQTT 1.x, but with MQTT 2.x it should be something like this:

Things file:

Bridge mqtt:broker:b51_1 "WRT"@"B51" [ host="192.168.x.x", secure=false]

Thing mqtt:topic:lg_ikea_color_1 "Ikea Color 1" (mqtt:broker:b51_1) {
      Type colorHSB:color         "Ikea Color 1"              [ stateTopic="zigbee2mqtt/ikea_led_color_1/dumb", commandTopic="zigbee2mqtt/ikea_led_color_1/set", transformationPatternOut="JS:HSBtoRGB.js" ]


Color   LG_IKEA_Color_1_M24 "Color M24 lamp [%s]" (gLights) [ "Lighting" ]  { channel=" mqtt:topic:lg_ikea_color_1:color" }

JS Transformation file unchanged.

Sitemap with new item LG_IKEA_Color_1_M24:

Default item=LG_IKEA_Color_1_M24

Please notice dummy stateTopic zigbee2mqtt/ikea_led_color_1/dumb as item is not receiving updates from bulb (new transformation from XY to HSB color space would be required, as Z2M reports from ikea bulb this way {"state":"OFF","linkquality":94,"last_seen":"2020-01-01T22:45:21+01:00","brightness":254,"color_mode":2,"color":{"x":0,"y":0}})

1 Like

@kluszczyn thats for posting your setup… I am also using MQTT v1…

Here is the MQTT topic for the Hue RGB bulb… Got any pointers for making it work the way you have?


And this is the topic for a plain white bulb…


UPDATE : Actually your example works just fine for the Hue RGB bulb so that is a massive bonus!!!

1 Like

@TommySharp, I’m glad that it worked for you!
For plain white bulb you can rewrite transformation to keep only brightness channel.

Thread was about avoiding transformation so why?
Perheaps it’s not valid with this kind of info ?

You are right, solution is not in line with topic thread. I should rather post in Zigbee2mqtt and Zigbee bulbs small tutorial. Unfortunately, I don’t know how (and if possible) to move existing post to another thread. Sorry.

Moved from: Zigbee2mqtt revisited: No more ugly transformations

With your solution my Hue and Tradfri Bulbs only starts with 10% Brightness. Is there a way to start them with the old state at turn off?


Hi @R-Rosenow, welcome to the openHAB community!

JS transformation only transforms information provided by openHAB color item to json format expected by Z2M. 10%, at least in Ikea bulb case, is minimum brightness value bulb can ‘produce’. So I suspect, when bulb receives command ON without color/brightness information then switches to minimum brightness. You can extend json string for command ON adding key brightness and desired value (or color key too). This is workaround, not real solution, but at least you will have control over initial brightness (or color).

That don’t work. I have tested this:

return ‘{“state”:“ON”, “brightness”:215}’

With MQTT.fx this message work, but not at the JS at Openhab.

In the Openhab Documentation you can read this, that could be the problem, but there is no explain how to do it.

" Channel Type “colorRGB”, “colorHSB”

  • on : An optional string (like “BRIGHT”) that is recognized as on state. (ON will always be recognized.)
  • off : An optional string (like “DARK”) that is recognized as off state. (OFF will always be recognized.)
    *** onBrightness : If you connect this channel to a Switch item and turn it on,**

color and saturation are preserved from the last state, but the brightness will be set to this configured initial brightness (default: 10%).

You can connect this channel to a Color, Dimmer and Switch item.

This channel will publish the color as comma separated list to the MQTT broker, e.g. “112,54,123” for an RGB channel (0-255 per component) and “360,100,100” for a HSB channel (0-359 for hue and 0-100 for saturation and brightness).

The channel expects values on the corresponding MQTT topic to be in this format as well.

Do you have an Idea?

Thank you.

I’ve just checked JS modification as below with IKEA color bulb and it works as expected

    if (x=="ON" || x=="INCREASE") {
        return '{"state":"ON","brightness":250}'

Message from Z2M zigbee2mqtt/ikea_led_color_1


Please pay attention to quotation marks " is not the same as - see https://www.w3schools.com/html/html_charset.asp

PS. To preserve code formatting use code fences ``` around code
(or marked button in editor

Good luck!

It dont work for me.

Can you show your Item & Thing for example?


Thing topic philips_hue_fensterlampe “Fensterlampe” @ “MQTT” {
Type colorHSB:color “Farbe” [ stateTopic = “zigbee2mqtt/philips_hue_fensterlampe/state”,
commandTopic = “zigbee2mqtt/philips_hue_fensterlampe/set”, transformationPatternOut=“JS:HSBtoRGB.js” ]
Type number : linkquality “Verbindung” [ stateTopic = “zigbee2mqtt/philips_hue_fensterlampe/linkquality” ]


Switch wohnzimmer_fensterlampe “Fensterlampe” (glichter_gesamt,glichter_wohnzimmer) { channel=“mqtt:topic:Mosquitto:philips_hue_fensterlampe:color” }

Color wohnzimmer_color_fensterlampe “Fensterlampe” [“Lighting”] { channel=“mqtt:topic:Mosquitto:philips_hue_fensterlampe:color” }

Dimmer wohnzimmer_fensterlampe_dimmer “Fensterlampe” { channel=“mqtt:topic:Mosquitto:philips_hue_fensterlampe:color” }

Number wohnzimmer_fensterlampe_verbindung “Fensterlampe [%.1f %%]” { channel=“mqtt:topic:Mosquitto:philips_hue_fensterlampe:linkquality” }

Thats the output of Zigbee2Mqtt:

Jan 12 13:54:02 openHABianPi npm[2760]: zigbee2mqtt:info 2020-01-12 13:54:02: MQTT publish: topic ‘zigbee2mqtt/philips_hue_fensterlampe/state’, payload ‘ON’
Jan 12 13:54:02 openHABianPi npm[2760]: zigbee2mqtt:info 2020-01-12 13:54:02: MQTT publish: topic ‘zigbee2mqtt/philips_hue_fensterlampe/linkquality’, payload ‘55’
Jan 12 13:54:02 openHABianPi npm[2760]: zigbee2mqtt:info 2020-01-12 13:54:02: MQTT publish: topic ‘zigbee2mqtt/philips_hue_fensterlampe/brightness’, payload ‘26’
Jan 12 13:54:02 openHABianPi npm[2760]: zigbee2mqtt:info 2020-01-12 13:54:02: MQTT publish: topic ‘zigbee2mqtt/philips_hue_fensterlampe/color-x’, payload ‘0.4062’
Jan 12 13:54:02 openHABianPi npm[2760]: zigbee2mqtt:info 2020-01-12 13:54:02: MQTT publish: topic ‘zigbee2mqtt/philips_hue_fensterlampe/color-y’, payload ‘0.3669’
Jan 12 13:54:02 openHABianPi npm[2760]: zigbee2mqtt:info 2020-01-12 13:54:02: MQTT publish: topic ‘zigbee2mqtt/philips_hue_fensterlampe/color-saturation’, payload ‘55.1’
Jan 12 13:54:02 openHABianPi npm[2760]: zigbee2mqtt:info 2020-01-12 13:54:02: MQTT publish: topic ‘zigbee2mqtt/philips_hue_fensterlampe/color-hue’, payload ‘46.2’

For things/items definition please see post#34

I see there no difference.

OK, so it looks like binding mqtt1.x and mqtt2.x behaves differently. I did my tests with modified JS file return '{"state":"ON","brightness":250}' on mqtt1.x

When I switched to mqtt2.x it started behaving like you described. To fix it please update channel definition as follow:

    Type colorHSB   : color         "Ikea Color 1"              [ stateTopic="zigbee2mqtt/ikea_led_color_1/dumb", commandTopic="zigbee2mqtt/ikea_led_color_1/set", transformationPatternOut="JS:HSBtoRGB.js", onBrightness=70 ]

adding onBrightness=70, where 70 is desired brightness when switching on.

Hope this helps also in your case.

1 Like

Thats the solution that I was search for.

It works great! Thanks.

Add into configuration.yaml

    output: attribute

and stop to use transformationPatternOut