CDriver
(CDriver)
February 25, 2018, 9:16pm
1
Hi folks,
I have a dimmer item which sends it’s value via mqtt.
The dimmer uses 0-100 (percent). is there a way I can use a map on it to make 0-100 to 0-255?
I’d like to not do this via a rule if possible.
Arduino map is the perfect example.
Thanks.
bartus
(B K)
February 25, 2018, 9:27pm
2
You can use a script (JS) transform, i.e.
(function(i) {
if(input >= 0)
{
return input / 100 * 255;
}
else
{
return null;
}
})(input)
and in the item definition, call the JS transform i.e.
{mqtt="<[broker:home/dimmer:state:JS(convert.js)]"}
where convert.js is your script file name
CDriver
(CDriver)
February 25, 2018, 10:08pm
3
Thanks! (but I can’t get it to work )
So I have
Dimmer backroom_led_lantern_dimmer (g_BackRoomDimmer) { mqtt=">[mqtt:home/lights/backroom/LED_BACKROOM_LANTERN/brightness/set:command:JS(convert.js):default]" }
and the convert.js file is
(function(i) {
if(input >= 0)
{
return 666;//input / 100 * 255;
}
else
{
return null;
}
})(input)
If i remove the JS(convert.js) and replace it with * I can see the messages being published.
With the convert in though I don’t see them being published. I don’t see any errors in the event log either.
Did I misunderstand?
Thanks
bartus
(B K)
February 25, 2018, 10:13pm
4
I think you have too many items in the channel definition (get rid of the :default at the end, leave only the JS tranform).
CDriver
(CDriver)
February 25, 2018, 10:49pm
5
ok, fixed it (as you replied :))
So, convert.js needs to go into Transforms. I thought it had to be in scripts.
I had to install the JS transformation. It’s not installed by default.
My final change is
Dimmer backroom_led_lantern_dimmer { mqtt=">[mqtt:home/lights/backroom/LED_BACKROOM_LANTERN/brightness/set:command:*:JS(convert.js)]" }
Works a treat, Thanks!
bartus
(B K)
February 26, 2018, 11:49am
6
Ah sorry about that (my reply was confusing as I said both “script” amd “transform” in the same sentence) Glad it worked!