Zigbee2mqtt and Zigbee bulbs small tutorial

I also added my Ikea bulb via zigbee2mqtt and it’s working, but not completely.

I added all js files and they seems to be working, but when I set a value via the sitemap (slider or setpoint), the value jumps back to x/255. So eg, when I select 70, I get the brightness of 27, 55 gives 22 and so on…

The same happens when I remove all transformation stuff and only add

[ commandTopic="zigbee2mqtt/bulb/set/brightness_percent" ]

Setting the values via mqtt.fx (windows application) is working fine.

What am I missing? I’m running OH 2.4

You should try and upgrade to 2.5m1, there are some serious bugs in 2.4 and new mqtt binding.

Regards S

ok, maybe I wanted to upgrade to 2.5M2 when it’s out today.

I never encountered any other issues with the mqtt binding. I have some some esp modules which I connect through mqtt, but this is only to read data, not to send (exept power on/off).

Hi, thanks for this interesting post, it show me the right way
Only I don’t fully understand if transformation files are needed or not.
So, is the example correctly updated since the conversation below?
By the way it seems setting the on off switch don’t really work (for me) until the dimmer is used instead to switch on/off the bulb. I don’t understand why

Hi!

Yes transformation files are needed in order to translate like a value of 254 to 100% for hue.
The example is updated, so if you use that it should work. Transformation of value 0 - 255 to 0 - 100 could be simplified, but that’s not present in the current example, so to make it simple for yourself, stick with the above including transformation and see how it works. Once it is working you can start tweaking and rewriting code as you see fit.

Regards, S

1 Like

hi,
I’m on OH 2.5m2, zigbee2mqtt 1.3.1 firmware version: ‘20190223’, TRADFRI LED1545G12
The above examples with the js transformations are working fine for for this bulb, e.g. brightness.
I’ve tried now to implement color_temp (using the /set/color_temp) but the bulb does not react, I don’t even see a message in the zigbee2mqtt log.

tradfri.things
Type number : colortemp “Tradfri 01 Color Temp”
[ stateTopic=“zigbee2mqtt/0x000b57fffebb8eeb”, transformationPattern=“JSONPATH:$.color_temp”, commandTopic=“zigbee2mqtt/0x000b57fffebb8eeb/set/color_temp”]

tradfri.items
Number Tradfri_Lamp01_colortemp “Drahtlampe color temp” {channel=“mqtt:topic:myMqttBroker:tradfrilamp01:colortemp”}

sitemap
Setpoint item=Tradfri_Lamp01_colortemp label=“Drahtlampe Farbtemperatur” step=25 minValue=250 maxValue=454

outgoing transitions were fixed in OH2.5 I believe, which is why the brightness using JS is working I believe. So I don’t understand why the color_temp is not working.

any hints or input is much appreciated.
Andy

update:
oh I see, for non-JSON format in zigbee2mqtt a special config is required:

update2:
this seems to be the setting in zigbee2mqtt to enable the non-JSON GET:

but the SET should work even without the setting, but for me it does not, see above.
@AndreasBrett, @Andreas84 does this require a certain zigbee2mqtt version to work ?

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.