Dimmer values (0-255) into MQTT switch channel (on/off)

I have the following channel in an MQTT thing:

Type switch : switch "Power Switch" [ 
	commandTopic="wled/rgb2",
	on="ON",
	off="OFF",
	stateTopic="wled/rgb2/g",
	transformationPattern="JS:dimmer2switch.js"
]

The stateTopic receives a number between 0 and 255, and the javascript transformation converts that number to ON or OFF (where zero = off and anything else = on).

Is there a way to do this without using a custom javascript transform?

For completeness, here is the javascript:

(function(x) {
    if(x == 0){
        return "OFF";
    }
    else{
        return "ON";
    }
})(input)

Why not use a Dimmer Item. The dimmer Channel is able to convert the 0-255 to 0-100 for you. And then you can use your Dimmer Item as if it were a Switch Item in your sitemap and in your rules you can use MyItem.getStateAs(OnOffType) to get its state as if it were a Switch.

Beyond that, a JS transformation is the only way to do it I know of, But since a Dimmer can be treated as a Switch I don’t see much reason to do this in the first place.

Thanks for the suggestion - I wasn’t aware a dimmer item could be passed into a switch in the sitemap!

However, I don’t think it’s quite what I’m after. My existing dimmer channel is this:

Type dimmer : brightness "Brightness" [ 
	commandTopic="wled/RGB2",                
    stateTopic="wled/RGB2/g", 
	min=0, 
	max=255, 
	step=1
]

This is tied to the following item:

Dimmer dRGB2Brightness "RGB2 Brightness" {channel="mqtt:topic:swRGB2:brightness", autoupdate="false"}

If I tie this item to the switch in the sitemap, as follows:

Switch item=dRGB2Brightness label="RGB2 []" icon="light"

it works, BUT when using this switch it sends either a 0 or 255 over MQTT - in other words, the switch isn’t honouring the currently set brightness level on the device, and overwriting. I’m sure this is as intended for the switch - it makes sense - but it’s not quite what I’m after. I just want the switch to send ON and OFF.

What I was really hoping for was that there is some magic I can perform with the switch channel’s on= and off= options, but it doesn’t look like it…

Looks like I’ll be sticking to the JS transform!

Thanks again!

EDIT: Hmm, I wonder if max="ON" and min="OFF" are allowed in a dimmer channel. I presume not as I assume a dimmer can only handle numbers… Looks like I might be in luck: there’s an on and off option: https://www.openhab.org/addons/bindings/mqtt.generic/#channel-type-dimmer

If it helps here is what I use:

Thing:

Thing topic zigbee2mqtt "Bedroom Light" @ "Bedroom" {
    Channels:
        Type switch : power  "Power"               [ stateTopic="zigbee/0xb0ce1814030ac279/state",
                                                    commandTopic="zigbee/0xb0ce1814030ac279/set/state", on="ON", off="OFF" ]
        Type dimmer : dimmer "Dimmer"              [ stateTopic="zigbee/0xb0ce1814030ac279",
                                                    commandTopic="zigbee/0xb0ce1814030ac279/set", transformationPattern="JSONPATH:$.brightness", formatBeforePublish="{\"brightness\":%s}" ]
    }

Transformation:
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)

setZigbeeState.js

(function(x){

    var result = new Object();
    result.state = x;
 
    return JSON.stringify(result);
    
})(input)

@H102 This looks good, but I’m not having any issues with the transforms at all - it all works. I was just trying to get rid of the JS transform altogether!

@rlkoshak I’ve had a play with the on and off parameters for the dimmer channel, but I don’t think it does what I’m after either:

Thing

Type dimmer : power "Power Switch" [ 
	commandTopic="wled/RGB2",
    stateTopic="wled/RGB2/g",
	on="ON",
	off="OFF",
	min=0,
	max=255,
	step=1
]

Item

Dimmer dRGB2Power "RGB Power" {channel="mqtt:topic:swRGB2:power", autoupdate="false"}

Sitemap

Switch item=dRGB2Power label="RGB []" icon="light"

What I thought I’d done is define a dimmer channel which, when commanded by a switch from the sitemap, would send the string “ON” and “OFF” instead of 0 and 255. What actually happens is that it continues to send 0 or 255.

I’ve mis-interpreted the documentation, that’s clear, but have I mis-defined something?

I’ve not real experience with Dimmers and the MQTT binding so I can’t really say. I do know there are some order of operations that might trip one up. For example, perhaps if you define the min/max it overrides the on/off so those options just get ignored. I don’t know what the correct behavior is and whether it’s behaving as expected or if there is a bug here.

You could try filing an issue explaining how you expect it to work. It might be a use case that could be supported.