Zigbee2mqtt and Zigbee bulbs small tutorial

I have updated the js script for on/off. Since 2.5m2, the previous script didn’t work.

I have also gotten an IKEA color temp bulb, so I will update this tutorial with that as well when I get some spare time. Main issue is that it’s very difficult with .js-scripts, you need to restart opehab when making changes to js-scripts, which makes it very tiresome to debug, and develop. Right now I’m using rules to control the Color-temp.

Regards, S

1 Like

could you please add how to control a color hue light by command?
with hue gateway i could send this:

     case "WZ_EIN" : {// 0-360; 0=red, 120=green, 240=blue, 360=red(again)
            WZ_Hue_LS_Re_Color.sendCommand          ("200,0,100")
            WZ_Hue_LS_Li_Color.sendCommand          ("200,0,100")
            WZ_Light_Stripe_Color.sendCommand       ("200,0,100")
            WZ_Hue_Sofa_Color.sendCommand           ("200,0,100")
            EZ_Hue_Regal_Color.sendCommand          ("200,0,100")

is there something similar for zigbee2mqtt?

@Seaside I’m currently trying to get Zigbee RGB bulbs working with OH through zigbee2mqtt. Could you maybe share your .rules to show how you converted the input and then forwarded to zigbee2mqtt?

Thanks and cheers!

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

1 Like

@kluszczyn: Nice work.:+1:

Have you tried this using mqtt 2.x version?

Thanks.
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) {
  Channels:
      Type colorHSB:color         "Ikea Color 1"              [ stateTopic="zigbee2mqtt/ikea_led_color_1/dumb", commandTopic="zigbee2mqtt/ikea_led_color_1/set", transformationPatternOut="JS:HSBtoRGB.js" ]
  }

Item:

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?

{“state”:“ON”,“linkquality”:15,“brightness”:25,“color_temp”:366,“color”:{“x”:0.4573,“y”:0.41}}

And this is the topic for a plain white bulb…

{“state”:“ON”,“linkquality”:15,“brightness”:254}

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?

Thanks.

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

{"state":"ON","linkquality":60,"last_seen":"2020-01-12T13:13:39+01:00","brightness":250,"color_mode":2,"color":{"x":0.5346,"y":0.3124}}

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
image
)

Good luck!

It dont work for me.

Can you show your Item & Thing for example?

Thing:

Thing topic philips_hue_fensterlampe “Fensterlampe” @ “MQTT” {
Channels:
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” ]
}

Item:

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.