HSB color item and multiple MQTT topics

I use openHAB 2.5 with the MQTT binding and koenks Zigbee2mqtt. In the files mqtt.things and mqtt.items I made corresponding entries for some Philips Hue and Innr devices. So far this works quite well. I also managed to display a color wheel on the sitemap and control the Hue light strip accordingly when the color changes. Unfortunately I can’t manage to transport a change of color from the light strip back to the HSB Color Item. For example, if I change the color of the light strip with another app, the item on the site map should also change. Specifically, the problem is that changing the color results in 2 to 3 MQTT Topics. But then these have to change a single channel and item. How do I solve this problem?

mqtt.things:

Type colorHSB  : color "Color" [ commandTopic="zigbee2mqtt/<friendlyname>/set/color", formatBeforePublish="{\"hsb\":\"%s,%s,%s\"}"]

mqtt.items:

Color Kitchen_Worktop_Light_Color "Color" {channel="mqtt:topic:mosquitto:<friendlyname>:color"}

MQTT topic setting color:

zigbee2mqtt/<friendlyname>/set/color: {"hsb":"40,10,50"}

MQTT answers:

zigbee2mqtt/<friendlyname>/color_h = 40
zigbee2mqtt/<friendlyname>/color_s = 10
zigbee2mqtt/<friendlyname>/color_v = 50

At this point I have to mention that I use Zigbee2mqtt with the output: attribute option. This is currently considered an improvement. But I have the feeling that this could be the cause that I do not get the 3 HSB values back into the item. What do you think?

Have you tried the attribute_and_json setting for the output parameter in your zigbee2mqtt configuration.yaml?

Yes, I have. But don’t know how to transform topic back to color state. Probably I need a little bit support on that.

What does zigbee2mqtt send to your MQTT broker with the attribute_and_json set? Use a 3rd party program like MQTT Explorer or mqtt.fx to check.

1 Like

I’m using MQTT explorer from Thomas Nordquist. After changing to attribute_and_json the MQTT response is:

{"brightness":0,"color":{"h":40,"hue":38,"s":10,"saturation":20,"v":50},"color_temp":null,"state":"OFF"}

Now you’ve got the detail in one MQTT payload, you can use a javascript transform to extract individual elements and collect as a single string to be returned to an openHAB Item

Thank you so much. This was exactly what I needed. I completed my code with corresponding outbound transformation:

mqtt.things

Type colorHSB  : color "Color" [ stateTopic="zigbee2mqtt/<friendlyname>", commandTopic="zigbee2mqtt/<friendlyname>/set/color", transformationPattern="JS:colorjson2csv.js", transformationPatternOut="JS:colorcsv2json.js"]

mqtt.items

Color Kitchen_Worktop_Light_Color "Color" {channel="mqtt:topic:mosquitto:<friendlyname>:color"}

colorjson2csv.js

(function(i) {
    var hsb = JSON.parse(i);
    return hsb.color.h + "," + hsb.color.s + "," + hsb.color.v;
})(input)

colorcsv2json.js

(function(i) {
    return "{\"hsb\":\"" + i + "\"}";
})(input)