As this thread is currently the best source for zigbee2mqtt-examples here is my solution to connect Lidl bulbs (but should work with other manufacturers too).
One problem is that the range of the brightness is hardware specific and not a percentage value like in OH. And there is a bug in z2m with hsb-payloads.
Solution:
For the brightness item there is a (not very well documented) feature (brightness_percent) to let z2m do the conversion.
Then for the color we need a transformation and we need the attribute_and_json option within the z2m-config enabled.
Here the example:
First the item-definition in file-format:
Thing mqtt:topic:Lidl1 "Lidl1" (mqtt:broker:MosquittoMqttBroker) { Channels:
Type switch : state "State" [ stateTopic = "zigbee2mqtt/Lidl1/state", commandTopic = "zigbee2mqtt/Lidl1/set", on="ON", off="OFF" ]
Type color : color "Color" [ stateTopic = "zigbee2mqtt/Lidl1", transformationPattern="JS:color_in.js", commandTopic = "zigbee2mqtt/Lidl1/set" , transformationPatternOut="JS:color_out.js"]
Type dimmer : brightness "brightness" [ commandTopic = "zigbee2mqtt/Lidl1/set/brightness_percent" ]
Type dimmer : color_temp "color_temp" [ stateTopic = "zigbee2mqtt/Lidl1/color_temp", commandTopic = "zigbee2mqtt/Lidl1/set/color_temp", min=153, max=500, step=1 ]
Type datetime : last_seen "last_seen" [ stateTopic = "zigbee2mqtt/Lidl1/last_seen" ]
}
Then the file transformation color_out.js which has to be placed in the transform-folder:
(function(color){
var parts = color.split(/[,:]/);
var h = Number(parts[0]);
var s = Number(parts[1]);
var v = Number(parts[2]);
var bright = v * 2.54;
var result;
if (s === 0) {
// catch bright white
result = '{"brightness":' + bright.toFixed(0) + ',"color_temp":153}';
} else {
// normal colors
result = '{"brightness":' + bright.toFixed(0) +
',"color":{"h":' + h + ',"s":' + s + '}}';
}
return result;
})(input)
The point here is to not send hsb (not working in z2m) but to send the brightness and for the color only “h” (Hue) and “s” (Saturation) in one json-payload.
And then the color_in transformation:
(function (x) {
var tmp = JSON.parse(x);
var bri = Math.round(tmp.brightness / 2.54);
var hue = Math.round(tmp.color.hue);
var sat = Math.round(tmp.color.saturation);
return hue + ',' + sat + ',' + bri
})(input)
Took me a while to figure this out, so I hope this may help others facing a similar problem.