Zigbee2mqtt revisited: No more ugly transformations

Hi ,

have someone a solution for RGB Colors ?
I have seen that there is an option option

But when I sent from the transform the ligth goes to OFF

return “255,0,0,255”;

zigbee2mqtt/0x00124b001ba703f8/set 0,255,0
zigbee2mqtt/0x00124b001ba703f8/state OFF
zigbee2mqtt/0x00124b001ba703f8/brightness 0
zigbee2mqtt/0x00124b001ba703f8/color-x 0.1652
zigbee2mqtt/0x00124b001ba703f8/color-y 0.7846

1 Like

Hi, thanks for sharing this discovery.

I made it works with an Aqara white bulb (ZNLDP12LM) and a Mijia switch (WXKG01LM), here is my config files and a simple rule if it can help.

test.things :

Bridge mqtt:broker:broker "MQTT Broker" [ host="127.0.0.1", clientid="OpenHab" ]
{
    Thing topic 0x00158d0002c67ab4 "Bulb_aquara"
    {
        Channels:
            Type switch : state "state" [ stateTopic = "zigbee2mqtt/0x00158d0002c67ab4/state", commandTopic = "zigbee2mqtt/0x00158d0002c67ab4/set/state", on="ON", off="OFF" ]
            Type dimmer : brightness "brightness" [ stateTopic = "zigbee2mqtt/0x00158d0002c67ab4/brightness", commandTopic = "zigbee2mqtt/0x00158d0002c67ab4/set/brightness", min=0, max=255, step=1 ]
            Type dimmer : color_temp "color_temp" [ stateTopic = "zigbee2mqtt/0x00158d0002c67ab4/color_temp", commandTopic = "zigbee2mqtt/0x00158d0002c67ab4/set/color_temp", min=150, max=500, step=1 ]
            Type number : linkquality "linkquality" [ stateTopic = "zigbee2mqtt/0x00158d0002c67ab4/linkquality" ]
    }

    Thing topic 0x00158d00015aa9b5 "Switch_mijia"
    {
        Channels:
            Type string : click "click" [ stateTopic = "zigbee2mqtt/0x00158d00015aa9b5/click" ]
            Type number : voltage "voltage" [ stateTopic = "zigbee2mqtt/0x00158d00015aa9b5/voltage" ]     
            Type number : battery "battery" [ stateTopic = "zigbee2mqtt/0x00158d00015aa9b5/battery" ]     
            Type number : linkquality "linkquality" [ stateTopic = "zigbee2mqtt/0x00158d00015aa9b5/linkquality" ]               
    }
}

test.items :

Group gAll

Switch Bulb_aquara_state        "Bulb [%s]"             <light> (gAll)  {channel="mqtt:topic:broker:0x00158d0002c67ab4:state"}
Dimmer Bulb_aquara_brightness   "Bulb brightness [%d]"		    (gAll)	{channel="mqtt:topic:broker:0x00158d0002c67ab4:brightness"}
Dimmer Bulb_aquara_color_temp   "Bulb color_temp [%d]"         (gAll)  {channel="mqtt:topic:broker:0x00158d0002c67ab4:color_temp"}
Number Bulb_aquara_linkquality  "Bulb linkquality [%d]"         (gAll)  {channel="mqtt:topic:broker:0x00158d0002c67ab4:linkquality"}


String Switch_mijia_click      "Button [%s]"           <button>    (gAll) {channel="mqtt:topic:broker:0x00158d00015aa9b5:click"}
Number Switch_mijia_voltage    "Button Volt [%d mV]"               (gAll) {channel="mqtt:topic:broker:0x00158d00015aa9b5:voltage"}
Number Switch_mijia_battery    "Button bat [%.1f %%]"  <battery>   (gAll) {channel="mqtt:topic:broker:0x00158d00015aa9b5:battery"}
Number Switch_mijia_linkquality "Button linkquality [%d]"          (gAll) {channel="mqtt:topic:broker:0x00158d00015aa9b5:linkquality"}

test.rules :

rule "Turn on light from button"
when
    Item Switch_mijia_click received update single
then
    logInfo("broker trigger debug", "triggered single clic")
    sendCommand(Bulb_aquara_state, OFF)
end

rule "Turn off light from button"
when
    Item Switch_mijia_click received update double
then
    logInfo("broker trigger debug", "triggered double clic")
    sendCommand(Bulb_aquara_state, ON)
end

I’m not satisfied with the way I managed brightness and color_temp, if someone has a better way…

@the.neon I’ll try with a colored bulb soon (I have a spare Hue), but reading the code, it looks like the payload is only RGB value subPayload = [subPayload.r, subPayload.g, subPayload.b]; so you should send something like 100,0,102 and not 255,0,0,255 ?
Not sure but in doubt I will also try something like this zigbee2mqtt/0x00124b001ba703f8/set/state/color/hex #547CFF

An other question is how can we change the transition time (see transition here https://github.com/Koenkk/zigbee2mqtt.io/blob/351f24ce4a77ceae7a0fc22c436fa710a64e4b1e/docs/information/mqtt_topics_and_message_structure.md#zigbee2mqttfriendly_nameset) as the payload is not a JSON anymore…

Hi,
details about color are here : https://github.com/Koenkk/zigbee-herdsman-converters/blob/edd4ce980975e76792b153c1df8d4e2c2b04e0e4/converters/toZigbee.js#L376 (also this Pull Request https://github.com/Koenkk/zigbee2mqtt/pull/2441).

That mean that we can set the color with an MQTT command like :
zigbee2mqtt/0x0017880102320dd8/set/color #FF0000 because of the condition || (typeof value === 'string' && value.startsWith('#')))
But we can’t do zigbee2mqtt/0x0017880102320dd8/set/color 100,100,100 because the condition require a Property (a JSON key).
As OpenHAB can only send colorRGB and colorHUE values, for now, there is no solution to control the color of a bulb without ugly transformation.

I would love to write a Pull Request to z2m but there is no easy way to distinguish RGB (255,255,255) values from HSB one (360,100,100).
Other solution would be to found a simple way to send color as Hex (#FF0000) from OpenHAB.

1 Like

Yeah I’ve just been struggling with how to set the colour of a Philips Hue RGB bulb… I’m keen to see if anyone has any smart ideas…

What does the structure in zigbee2mqtt look like?

7 posts were merged into an existing topic: Zigbee2mqtt and Zigbee bulbs small tutorial

Nobody knows the structure of color bulbs in zigbee2mqtt with attribute output enabled? So nobody connected a color bulb yet?

It works multiple ways, I think RGB is the easiest:
zigbee2mqtt/item/set/color with the value in RGB hex (so #FF0000 will be full red). I’m currently trying to figure out how to make it work with the colorpicker because it works from mqtt but the colorpicker wants to send these hue & saturation values that I don’t fully understant.
It also works with JSON providing the values in this format
{r:“255”,g:“0”,b:“0”}

Funny, no matter what combination I use: I have Closed when I should have OPEN and OPEN when I should have CLOSED.

the new version zigbee2mqtt 1.9.0 supports: "color values in HSB/HSV/HSL notation"

    // Hue, saturation, brightness (in HSB space)
    "hsb": "360,100,100"

At the moment it seems you can not set HSB/HSV/HUE value directly. You still have to use JSON with surrounding ‘color’ tag. So it’s now working with a small formatting, but without ugly transformation.

here is the working configuration for my Osram RGBW bulb:

.things

    Thing topic osramrgbbulb "osramrgbbulb" {
    Channels:
        Type switch : power "Power" [ stateTopic = "zigbee2mqtt/0x7cb03eaa00ada0df/state", commandTopic = "zigbee2mqtt/0x7cb03eaa00ada0df/set/", on="ON", off="OFF" ]
        Type string : color "Color" [  stateTopic = "zigbee2mqtt/0x7cb03eaa00ada0df/color-hsb", commandTopic="zigbee2mqtt/0x7cb03eaa00ada0df/set" , formatBeforePublish="{\"color\":{\"hsb\": \"%s\"}}" ]
    }     

.items

String WZ_OSRAM_Bulb_Color  "WZ_OSRAM_Bulb_Color"  <light> { channel="mqtt:topic:mosquitton:osramrgbbulb:color"}

.sitemap

Colorpicker item=WZ_OSRAM_Bulb_Color
9 Likes

The new version 1.9.0 now also supports output of payloads as JSON and plain attributes with the following option in configuration.yaml:

experimental:
  # Optional: MQTT output type: json, attribute or json_and_attribute (default: shown below)
  # Examples when 'state' of a device is published
  # json: topic: 'zigbee2mqtt/my_bulb' payload '{"state": "ON"}'
  # attribute: topic 'zigbee2mqtt/my_bulb/state' payload 'ON"
  # json_and_attribute: both json and attribute (see above)
  output: 'json'

There is a error in the documentation, the option is called “attribute_and_json” and works as expected, payload is transmitted in both flavours.
That will help me with migrating from MQTT 1 to MQTT 2.x as well as from JSON to plain attributes.

5 Likes

How i can define Trigger type channel?
I try to use:

Type trigger  : action  "action"  [ stateTopic = "zigbee2mqtt/xiaomi_cube/action"]

but i have an error:
20:34:27.098 [ERROR] [l.thing.internal.GenericThingProvider] - Channel type mqtt:trigger could not be resolved.

I need to use it in rules like this:

when

Channel "mqtt:topic:be6e02f3:Cube:action" triggered

then

Have a look at the Ikea remote. This has an action channel as well. This is a simple string channel, the string is related to the type of action.

1 Like

Thank you very much, @job. Please share your rule example how catch events from action channel? What is event to catch action? item update or changed?

i try this, but it does not work-(

when
    Channel "mqtt:topic:be6e02f3:Cube:action" triggered
then

Try it with single quotes:

when
    Channel 'mqtt:topic:be6e02f3:Cube:action' triggered
then

I’m not 100% sure if that will make a difference but it’s what I use and it works so worth a try. Also what is triggered? You may need to add that after the word triggered like below where START is used.

when

Channel 'astro:sun:local:civilDusk#event' triggered START

then
1 Like

I don’t use trigger channel, i just trigger on item change. Just create the item based on the channel and trigger on the item.

Check the string within the rule.

1 Like

Thank you,friends. I used item based driven rules and this work fine for me.

 rule "Zigbee2Mqtt Cube action"
when
    Item Cube_Action received update
then
    logError("xiaomicube", "cube action fired")
    switch(Cube_Action.state.toString) {
        case "slide": {

Newb here, how does channel referencing work? Where can I RTFM it?

First post and

Sorry, I should have been clearer when asking: I don’t get the ITEMS part specifically.
I understand how “thing” references mqtt channel.
E.g. zigee2mqtt//… etc

But having defined things, why does one need to reference items like this:
“mqtt:topic:broker:0x00158d00015aa9b5:click”
And sometimes like this:
“mqtt:topic:mosquitton:osramrgbbulb:color”
?