How to control Hue color Lights with zigbee2mqtt and mqtt2.4

I’m running Openhab 2.4.0-1 and i would like to switch from Philips Hue Gateway to zigbee2mqtt. in the old MQTT Binding it looked like this:

Switch      BK_Pflanzenregal_Toggle     "Pflanzenregal [MAP(active.map):%s]"                <light>     (Hue,BK,Lights)         { channel="hue:0210:00178847fafb:11:color" }  
Dimmer      BK_Pflanzenregal_Dimmer     "Helligkeit"                                        <light>     (Hue,BK)                { channel="hue:0210:00178847fafb:11:color" }  
Color       BK_Pflanzenregal_Color      "Farbwahl"                                          <light>     (Hue,BK)                { channel="hue:0210:00178847fafb:11:color" }  
Dimmer      BK_Pflanzenregal_Colortemp                                                      <color>     (Colortemp)             { channel="hue:0210:00178847fafb:11:color_temperature" }
String      BK_Pflanzenregal_alert                                                          <light>     (Alert)                 { channel="hue:0210:00178847fafb:11:alert" }
Switch      BK_Pflanzenregal_effect                                                         <light>     (Effect)                { channel="hue:0210:00178847fafb:11:effect" }

in the new MQTT-Binding im struggling with the color, colortemp, and brightness, i can not control the brightness. it is not working.
and how do i control the color?

i created this thing:

Bridge mqtt:broker:mosquitto "Mosquitto" [ host="localhost", port=1883, secure=false, username="xxxx", password="xxxx", clientID="openHAB2" ]
{
    Thing topic zigbeebridge "Zigbee2mqtt Bridge" {
    Channels:
        Type switch : permitJoin         [ commandTopic="zigbee2mqtt/bridge/config/permit_join", on="true", off="false" ]
        Type string : state              [ stateTopic="zigbee2mqtt/bridge/state" ]
        Type string : logType            [ stateTopic="zigbee2mqtt/bridge/log",  transformationPattern="JSONPATH:$.type" ]
        Type string : logMessage         [ stateTopic="zigbee2mqtt/bridge/log",  transformationPattern="JSONPATH:$.message" ]
    }
    Thing topic dresden_elektronik "Dresden Elektronik" {
    Channels:
        Type switch : switch "Dresden Elektronik AN/AUS" [ stateTopic="zigbee2mqtt/dresden_elektronik", commandTopic="zigbee2mqtt/dresden_elektronik/set", on="ON", off="OFF", transformationPattern="JSONPATH:$.state"]
        Type dimmer : dimmer "Dresden Elektronik Dimmer" [ stateTopic="zigbee2mqtt/dresden_elektronik", commandTopic="zigbee2mqtt/dresden_elektronik/set", min=0, max=100, step=1, transformationPattern="JS:huewhite2openhab.js" ]
        Type number : linkquality "Dresden Elektronik LinkQuality" [ stateTopic="zigbee2mqtt/dresden_elektronik",  transformationPattern="JSONPATH:$.linkquality" ]
    }
}

and this is the items.file

Group  Zigbee                           "Zigbee Geräte zigbee2mqtt"

Group  ZigbeeBridge                     "Zigbee2mqtt Bridge"                        (Zigbee)
String ZigbeeBridgeState                "Zigbee2mqtt Bridge State"                  (ZigbeeBridge) { channel="mqtt:topic:mosquitto:zigbeebridge:state" }
Switch ZigbeeBridgePermitJoin           "Zigbee2mqtt Bridge Permit join"            (ZigbeeBridge) { channel="mqtt:topic:mosquitto:zigbeebridge:permitJoin" , expire="10m,command=OFF" }
String ZigbeeBridgeLogType              "Zigbee2mqtt Bridge LogType"                (ZigbeeBridge) { channel="mqtt:topic:mosquitto:zigbeebridge:logType" }
String ZigbeeBridgeLogMessage           "Zigbee2mqtt Bridge LogMessage"             (ZigbeeBridge) { channel="mqtt:topic:mosquitto:zigbeebridge:logMessage" }

Group  Dresden_Elektronik               "Dresden Elektronik"                        (Zigbee)
Switch Dresden_Elektronik_State         "Dresden Elektronik"                        (Dresden_Elektronik) { channel="mqtt:topic:mosquitto:dresden_elektronik:switch" }
Dimmer Dresden_Elektronik_Dimmer        "Dresden Elektronik Dimmer"                 (Dresden_Elektronik) { channel="mqtt:topic:mosquitto:dresden_elektronik:dimmer" }
Number Dresden_Elektronik_Signal        "Dresden Elektronik Signal"                 (Dresden_Elektronik) { channel="mqtt:topic:mosquitto:dresden_elektronik:linkquality" }

Group  Hue_Aura                         "Hue Aura"                                  (Zigbee)
Switch Hue_Aura_1_State                 "Hue Aura 1"                                (Hue_Aura) { channel="mqtt:topic:mosquitto:hue_aura1:switch" }
Dimmer Hue_Aura_1_Dimmer                "Hue Aura 1 Dimmer"                         (Hue_Aura) { channel="mqtt:topic:mosquitto:hue_aura1:dimmer" }
Number Hue_Aura_1_Signal                "Hue Aura 1 Signal"                         (Hue_Aura) { channel="mqtt:topic:mosquitto:hue_aura1:linkquality" }

i can only switch the Hue Aura and the Dresden Elektronik on and off. but i don t know how to change the color? i can’t find any example for controlling Hue color Lamps. how can i control the brightness. i do not want update to openhab 2.5 unstable
br dave

Bump, I want to know how to control IKEA Tradfri bulbs color.

What have you tried already?

After pairing devices with MQTT I usually fire up MQTT Explorer and see what sort of topics are exposed. I also make sure I can publish the correct commands to the bulb too via MQTT Explorer.

See post 54 of thread #86362, and then post 56 underneath it.

I struggled the same way Dave :slight_smile:

but now I finally solved it for my home. I started to copy wild some code snippets, but finally it worked. :slight_smile: I hope my solution is helping you, or not totally rubbish as soon someone else is looking at it. On the alert and effekt problem I don’t have a solution, as my device is not supporting this.

Item

Dimmer      LightbulbE27RGBW1GFLivingRoomColorTemperature                      "Color temperature"  (ZigbeeKitchenBulb) { channel="mqtt:topic:MQTTBroker:Lightbulb_E27_RGBW_1_GF_LivingRoom:color_temperature" }

Thing

	Thing topic Lightbulb_E27_RGBW_1_GF_LivingRoom "Lightbulb_E27_RGBW_1_GF_LivingRoom" {
    Channels:
        Type switch : switch "GF_LivingRoom_Lightbulb_E27_RGBW_1" 							[stateTopic="zigbee2mqtt/GF_LivingRoom_Lightbulb_E27_RGBW_1", commandTopic="zigbee2mqtt/GF_LivingRoom_Lightbulb_E27_RGBW_1/set", transformationPattern="JSONPATH:$.state", transformationPatternOut="JS:switch2Zigbee2mqtt.js"]
        Type dimmer : dimmer "GF_LivingRoom_Lightbulb_E27_RGBW_1_Dimmer" 					[stateTopic="zigbee2mqtt/GF_LivingRoom_Lightbulb_E27_RGBW_1", commandTopic="zigbee2mqtt/GF_LivingRoom_Lightbulb_E27_RGBW_1/set", min=0, max=100, step=1, transformationPatternOut="JS:openhabdimmer2zigbeebridge.js", transformationPattern="JS:huewhite2openhab.js" ]
		Type colorHSB : color  "GF_LivingRoom_Lightbulb_E27_RGBW_1_Color"        			[stateTopic="zigbee2mqtt/GF_LivingRoom_Lightbulb_E27_RGBW_1", commandTopic="zigbee2mqtt/GF_LivingRoom_Lightbulb_E27_RGBW_1/set", transformationPatternOut="JS:HSBtoRGB.js" ]
		Type number : color_temperature "GF_LivingRoom_Lightbulb_E27_RGBW_1_Color Temp" 	[stateTopic="zigbee2mqtt/GF_LivingRoom_Lightbulb_E27_RGBW_1", commandTopic="zigbee2mqtt/GF_LivingRoom_Lightbulb_E27_RGBW_1/set", transformationPatternOut="JS:openhab2hueColortemp.js"]
    }

openhab2hueColortemp.js

(function(x) {
    var result = new Object();
    result.color_temp = x*350+150
    return JSON.stringify(result);
})(input)

Can you please share HSBtoRGB.js file ?

I’m having an issue converting the color into openhab color.

Here’s the one I’m using, it’s based on what I found but I added some debugging to see how it works.

(function (x) {

    var debug = false

    if (debug) {
        var logger = Java.type("org.slf4j.LoggerFactory").getLogger("HSBtoRGB.js");
        logger.warn("DEBUG: HSBtoRGB.js received: "+x);
    }

    if (x=="ON" || x=="INCREASE") {
        if (debug) logger.warn("Performing ON|Increase")
        return '{"state":"ON","transition":0.3,"brightness":255, "color_temp_startup":"warm"}'
    } else if (x=="OFF" || x=="DECREASE") {
        if (debug) logger.warn("Performing OFF|DECREASE")
        return '{"state":"OFF","transition":0.3}'
    } else {
        if (debug) logger.warn("Performing calculation...")
        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;
        }
        v = Math.round(v*255)
        r = Math.round(r*255)
        g = Math.round(g*255)
        b = Math.round(b*255)

        if (Math.min(v,r,g,b) == 255) {
            if (debug) logger.warn("Looks like full-on, overriding values");
            return '{"brightness":' + v + ', "color_temp":"neutral", "transition":0.2}'; 
        } else
            return '{"brightness":'+ v +',"color":{"rgb":"'+ r +','+ g +','+ b +'"},"transition":0.2}'
    }
})(input)