I managed to create an OH Thing for a RGBCW LED paired with zigbee2mqtt, which sends and receives MQTT commands.
zigbee2mqtt/ArgyleCourt/House/EaveLights/RGBCW_001 {"brightness":80,"color":{"x":0.6719,"y":0.3143},"color_mode":"xy","color_temp":223,"do_not_disturb":true,"linkquality":153,"state":"OFF"}
zigbee2mqtt/ArgyleCourt/House/EaveLights/RGBCW_001/state OFF
zigbee2mqtt/ArgyleCourt/House/EaveLights/RGBCW_001/color_mode xy
zigbee2mqtt/ArgyleCourt/House/EaveLights/RGBCW_001/color_temp 223
zigbee2mqtt/ArgyleCourt/House/EaveLights/RGBCW_001/brightness 80
zigbee2mqtt/ArgyleCourt/House/EaveLights/RGBCW_001/color-x 0.6719
zigbee2mqtt/ArgyleCourt/House/EaveLights/RGBCW_001/color-y 0.3143
zigbee2mqtt/ArgyleCourt/House/EaveLights/RGBCW_001/linkquality 153
zigbee2mqtt/ArgyleCourt/House/EaveLights/RGBCW_001/do_not_disturb true
What I came to like about this ‘chain’ (zigbee2maqtt > OH), is the extra configuration for transitions, steps, and other values the LED can ‘consume’.
Well, to the problem…
I am using a dimmer; its range is 0…100. Yet the LED expects values from 153…500. How can I achieve this mapping?
I suspect it needs an equivalent JS transformation like this C code:
// x = value to transform; rest self-explanatory)
int map(int x, int in_min, int in_max, int out_min, int out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
Actually, I don’t really know how to pass multiple values into a JS transformation.
Or is there a built-in or more elegant way to achieve the desired outcome?
OK, I created a dimmer channel for the light temperature and the JS versions of the above C code:
- id: color_temperature
channelTypeUID: mqtt:dimmer
label: Colour temperature
description: null
configuration:
commandTopic: zigbee2mqtt/ArgyleCourt/House/EaveLights/RGBCW_001/color_temp/set
transformationPatternOut: JS(light_temp_out.js):%s
stateTopic: zigbee2mqtt/ArgyleCourt/House/EaveLights/RGBCW_001/color_temp
transformationPattern: JS(light_temp_in.js):%s
light_temp_out.js
(function(i){
var in_min = 0;
var in_max = 100;
var out_min = 153;
var out_max = 500;
return (i - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
})(input)
light_temp_in.js
(function(i){
var in_min = 153;
var in_max = 500;
var out_min = 0;
var out_max = 100;
return (i - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
})(input)
However, the transformation does not happen.
The JS Scripting add-on is installed (has been for yonks).
[edit] Later added parseInt(i) in the Js transformation, as outlined here, which made no difference, neither to the error message, nor the result.
Where did I bugger up?
Well, OH can’t find the transformations. Not sure why!
2024-03-27 16:54:45.974 [WARN ] [t.generic.ChannelStateTransformation] - Transformation service JS(LIGHT_TEMP_OUT.JS) for pattern %s not found!
2024-03-27 16:54:45.983 [WARN ] [t.generic.ChannelStateTransformation] - Transformation service JS(LIGHT_TEMP_IN.JS) for pattern %s not found!
But these files are there:
# [2024-03-27 17:00] openhabian@openhabian /etc/openhab/transform $
ls -la light*
-rw-rw-r-- 1 openhab openhab 194 Mar 27 15:40 light_temp_in.js
-rw-rw-r-- 1 openhab openhab 194 Mar 27 15:40 light_temp_out.js
Also, why does the error message show the filename in all uppercase; the filename is all lowercase?