Zigbee2mqtt revisited: No more ugly transformations

Yes, that helps a lot und pointed me into the right direction! I wrote a second script to also transform the incoming value. To summarize for other users, here is my complete solution (looks little different cause i use config files:

Thing:

Thing mqtt:topic:zbltbed01 "Zigbee - Light Schlafzimmer 01" (mqtt:broker:mosquitto) @ "Bedroom"
 {
     Type color  : color         "Farbe" [
        stateTopic="zigbee/light_bedroom_01",
        transformationPattern="JS:tradfri_brightness_in.js",
        colorMode="XYY",
        commandTopic="zigbee/light_bedroom_01/set",
        transformationPatternOut="JS:tradfri_brightness_out.js"
    ]
 }

tradfri_brightness_in.js

(function (x) {
    var tmp = JSON.parse(x);
    // The brightness value is received as integer 1-254 but needed as percentage
    var brightness = Math.round(tmp.brightness / 254 * 100);

    return tmp.color.x + ',' + tmp.color.y + ',' + brightness
})(input)

tradfri_brightness_out.js

(function (x) {
    var tmp = x.split(',');
    // The brightness value is received as percentage but needed as integer 1-254
    tmp[2] = tmp[2] * 2.54;

    return '{"color":{"x":"' + tmp[0] + '","y":"' + tmp[1] + '"},"brightness":"' + tmp[2] + '"}'
})(input)

Thanks for your help

3 Likes