JS Transform doesn't seem to apply to Color Temp with Alexa

  • Platform information:
    Latest OpenHabian on Pi4 (fresh install)
  • Issue of the topic: I have a FEIT RGBW bulb that I have flashed with Tasmota 9.2 and setup MQTT as needed. Everything seems to work except that tasmota has a range of 153:500 for Warm to Cool white… so I write a transform that takes the 2200:7000 Cool:Warm (Alexa inverts Kelvin for some strange reason) and maps it down the correct values:

/etc/openhab/transform/lighttemps.js:
(function(i) {
var lightMax = 500;
var lightMin = 153;
var tempMax = 7000;
var tempMin = 2200;

var result = (i - tempMin)/(tempMax - tempMin) * (lightMax - lightMin) + lightMin;
return Math.round((lightMax+lightMin)-result);
})(input)

In my Channel defintion for the light I have set the Profile to “JS” and the JavaScript filename to lighttemps.js. I have exposed the Lamp Group as Lighting and also tagged the “Color Temp” point as
ColorTemperatureController.colorTemeratureInKelvin for Alexa.

When I say, “Alex set Foyer Lamp to Cool White,” I get the following:
2020-12-28 20:13:20.347 [INFO ] [openhab.event.ItemCommandEvent ] - Item ‘FoyerLamp_ColorTemp’ received command 4000
2020-12-28 20:13:20.351 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item ‘FoyerLamp_ColorTemp’ predicted to become 7000

And in the console for the bulb I get:
21:13:20 MQT: stat/L_Foyer/RESULT = {“CT”:500}

  • Anything value greater than 500 = 500

The value should be 336…

So how do I get Alexa’s (backward) Kelvin values to actually transform before dropping them into the queue?

Bear in mind both input and output to transforms will be in string form. You generally need to parse string input to do maths on it.

Per the Tasmota documentation, the color temperature is expressed in mireds based on the range you mentioned but from cool white to warm white. So it’s not inverted on the Alexa side :smiley:

Anyway, I did some research and the formula to convert mireds to kelvin is kelvin = 1000000 / mireds and the same vice-versa. So that gives you a kelvin range of 2000 to ~6500 based on my calculation.

In terms of Alexa configuration, you should specify that range in the metadata parameter. That way all commands coming from the skill would be limited to that range.

Number ColorTemperatureKelvin "Color Temperature [%d K]" (gBulb) {alexa="ColorTemperatureController.colorTemeratureInKelvin" [range="2000:6500"]}