Controlling Tradfri Colortemp via zigbee2mqtt

Hey there,
I want to control Tradfri Bulbs via zigbee2mqtt.
I found some stuff here and over at zigbee2mqtt, but I think its more an openhab issue, so I’m posting here.
I am already able to control light level with a Slider in the UI, but I cant seem to manage to control the color temperature. Well, I could make it all cold white with the slider, but now it seems to be stuck there. Could you guys have look at my transforms, I think I did something wrong there.

tradfri_flur1.items

Switch tradfri1_TOGGLE “tradfri1 [%s]” {mqtt=">[broker:zigbee2mqtt/tradfri-flur-1/set:command::JS(setZigbeeState.js)],<[broker:zigbee2mqtt/tradfri-flur-1:state:JSONPATH($.state)]", expire=“120m,command=OFF”}
Dimmer tradfri1_DIMM “tradfri1 Dimm” {mqtt=">[broker:zigbee2mqtt/tradfri-flur-1/set:command:
:JS(setZigbeeBrightness.js)],<[broker:zigbee2mqtt/tradfri-flur-1:state:JS(getZigbeeBrightness.js)]"}
Dimmer tradfri1_TEMP “tradfri1 Temp” {mqtt=">[broker:zigbee2mqtt/tradfri-flur-1/set:command:*:JS(setZigbeeColor.js)],<[broker:zigbee2mqtt/tradfri-flur-1:state:JS(getZigbeeColor.js)]"}

setZigbeeColor.js

(function(x){
var color_temp = x*255/100;
var result = new Object();
result.color_temp = color_temp;
return JSON.stringify(result);
})(input)

getZigbeeColor.js

(function(x){
var result;
var json = JSON.parse(x);
result = json.color_temp * 100 / 255;
return result;
})(input)

setZigbeeBrightness.js

(function(x){
var brightness = x*255/100;
var result = new Object();
result.brightness = brightness;
return JSON.stringify(result);
})(input)

getZigbeeBrightness.js

(function(x){
var result;
var json = JSON.parse(x);
result = json.brightness * 100 / 255;
return result;
})(input)

test.sitemap

sitemap default label=“My first sitemap” {

Frame label=“Slide bright” {
Slider item=tradfri1_DIMM
}
Frame label=“Slide temp” {
Slider item=tradfri1_TEMP
}

}

Hey @ebi, in my direct color_temp tests over mqtt with the Ikea Tradfri white spectrum bulb, I noted that the range of color_temp is from 250 to ~450, so I set my scale to x*450/100 and that works for a range from 55% to 100% on a color_temp slider. Can’t ever seem to get it to over 6000K, always hovers at a yellowish white, sadly :confused:

setZigbeeColor.js:

(function(x){

    var color_temp = x * 450 / 100;

    var result = new Object();
    result.color_temp = color_temp;

    return JSON.stringify(result);

})(input)

getZigbeeColor.js:

(function(x){

    var result;

    var json = JSON.parse(x);  
    result = json.color_temp * 100 / 450;

    return result;

})(input)

You could throw in a 250 difference to bring it to a 0 - 200 range, but I haven’t tested that yet. Should look something like:

setZigbeeColor.js:

(function(x){

    var color_temp = (x * 450 / 100) + 250;

    var result = new Object();
    result.color_temp = color_temp;

    return JSON.stringify(result);

})(input)

getZigbeeColor.js:

(function(x){

    var result;

    var json = JSON.parse(x);  
    result = (json.color_temp - 250) * 100 / 200;

    return result;

})(input)