Zigbee2mqtt integration of color lights


I just got my Hue light strip working with openhab through zigbee2mqtt. I tried to follow these transforms from this post HSB color item and multiple MQTT topics - #7 by marsman147
But that didnt work for me so I wrote transform that now works with zigbee2mqtt and color items.

This is how mqtt channel looks like in openhab

      commandTopic: zigbee2mqtt/0x0017880109ba3d44/set
      colorMode: HSB
      transformationPatternOut: JS:colorcsv2json.js
      stateTopic: zigbee2mqtt/0x0017880109ba3d44
      transformationPattern: JS:colorjson2csv.js

colorcsv2json.js looks like this

(function(i) {

        var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.transform.colorcsv2json');
        var parts = i.split(',');
        var h = Math.floor(parseFloat(parts[0]));
        var s = Math.floor(parseFloat(parts[1]));
        var b = Math.floor(parseFloat(parts[2]));
        b = Math.floor(b/100 * 254);

        var response = { color: { hue: h, saturation: s }, brightness: b };
        var result = JSON.stringify(response);
        return result;


colorjson2csv.js looks like this

(function(i) {
        var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.transform.colorjson2csv');
        var colorFromMqtt = JSON.parse(i);
        var result = colorFromMqtt.color.h + "," + colorFromMqtt.color.s + "," + (colorFromMqtt.brightness/254*100);
        return result;

I hope this helps other trying out zigbee2mqtt with openhab-