[SOLVED] Convert HSBType to CIE XY needed for Ikea Tradfri control through Deconz REST

Super dope man. I had solved it before by hardcoding the colors I wanted and using a mapping but decided to give what you did a try. I ended up turning it into a JS Transform so I wouldn’t need a rule per bulb in the future, or to keep adding new bulbs to the same rule. This is pretty much your exact code turned into a JS Transform and some small changes from my own coding style.

(function(i) {
var sRGB = input.split(',');
//sR, sG and sB (Standard RGB) input range = 0 ÷ 255
//X, Y and Z output refer to a D65/2° standard illuminant.
var sR = sRGB[0];
var sG = sRGB[1];
var sB = sRGB[2];

var var_R = ( sR / 255 );
var var_G = ( sG / 255 );
var var_B = ( sB / 255 );

var_R = Math.pow(var_R, 2.19921875);
var_G = Math.pow(var_G, 2.19921875);
var_B = Math.pow(var_B, 2.19921875);

var_R = var_R * 100;
var_G = var_G * 100;
var_B = var_B * 100;

var X = var_R * 0.7161046 + var_G * 0.1009296 + var_B * 0.1471858;
var Y = var_R * 0.2581874 + var_G * 0.7249378 + var_B * 0.0168748;
var Z = var_R * 0.0000000 + var_G * 0.0517813 + var_B * 0.7734287;

var x = X/(X+Y+Z);
var y = Y/(X+Y+Z);

var payload = {};
payload.color = {};
payload.color.x = x;
payload.color.y = y;

return JSON.stringify(payload);

})(input)

I just saved it under bulbColorFix.js in my transform folder.

This is what a thing looks like for me with the transform:

Type colorRGB : color "Color"  [ commandTopic="zigbee2mqtt/bedroom_lights/set",
stateTopic="zigbee2mqtt/bedroom_lights", transformationPatternOut="JS:bulbColorFix.js"]

For a complete solution I’d also need a reverse function to turn back XYZ into sRGB but I don’t think it’s worth it. I’m not sure what the impact is for not having OH get the feedback from the color being set, but if someone can come up with a good reason I’ll take a crack at it.