Dimmer Item mqtt 0-100 > 0-255 without a rule

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.

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

Thanks! (but I can’t get it to work :frowning: )

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

I think you have too many items in the channel definition (get rid of the :default at the end, leave only the JS tranform).

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!

Ah sorry about that (my reply was confusing as I said both “script” amd “transform” in the same sentence) :slight_smile: Glad it worked!