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?
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.
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…
@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!
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.