How to format the number on a slider (sitemap)?

I have a slider that shows the numbers from 0 to 254. Instead, I want the slider to display 0-100%
I tried to use javascript transform, but that has no effect on the number format on this slider. In know the transform works, because if I use a “Default” item instead of a Slider item it works.

Sitemap

Switch item=FF_MasterBedroom_Main_Light
Slider item=FF_MasterBedroom_Main_Brightness visibility=[FF_MasterBedroom_Main_Light == ON] minValue=0 maxValue=254

Items

Number FF_MasterBedroom_Main_Brightness "Main Level  [%d%%]" <light> { channel="<snip>" [profile="transform:JS", toItemScript="brightness2percent.js"]}

I know a workaround with creating another helper item and a rule, but is there a simple solution for this?

Maybe this:

Try to link a dimmer item to your channel instead of a number item

Apparently if you use the javascript transform with a slightly different syntax, it works for a slider! :slight_smile:

Number FF_MasterBedroom_Main_Brightness "Main Level [JS(brightness2percent.js):%s]" <light> {channel="<snip>"}

Thanks for the other suggestions as well.

  • I still have a hard time figuring out the correct syntax for a Dimmer item. Besides, I like two controls (on/off and a slider).
  • I prefer to have all my things, items, etc in code. I have roughly 100 devices and -for me- that is easier to manage.

Just to complete that point: one can also link Switch sitemap Widgets to Dimmer items.

Thanks, good to know! I did not think about that.

Sorry, but I can really not seem to figure out how to define a Dimmer item. I have been looking through Items | openHAB and the forum, but it is hard to find a code example.

So I have tried the following things definition.

Thing mqtt:topic:robbDimmer_ff_masterbedroom_center2WL "Switch FF Master Bedroom Main" (mqtt:broker:MosquittoMqttBroker) {
	Channels:
		Type switch : state       "State"        [ stateTopic = "zigbee2mqtt/robbDimmer_ff_masterbedroom_center2WL/state", commandTopic = "zigbee2mqtt/robbDimmer_ff_masterbedroom_center2WL/set", formatBeforePublish="{ \"state\": \"%s\" }", on="ON", off="OFF" ]
		Type number : brightness  "Brightness"   [ stateTopic = "zigbee2mqtt/robbDimmer_ff_masterbedroom_center2WL/brightness", commandTopic = "zigbee2mqtt/robbDimmer_ff_masterbedroom_center2WL/set", formatBeforePublish="{ \"brightness\": %s }", min=0, max=254 ]
		Type number : linkquality "Link Quality" [ stateTopic = "zigbee2mqtt/robbDimmer_ff_masterbedroom_center2WL/linkquality" ]
}

And the following Dimmer item

Dimmer FF_MasterBedroom_Main_Dimmer  "Main" <light> { channel="mqtt:topic:robbDimmer_ff_masterbedroom_center2WL:brightness"}

So, I get the following error. And how do I link the Dimmer item to both the the switch and the brightness channel?

2023-09-05 12:16:00.674 [ERROR] [nal.common.AbstractInvocationHandler] - An error occurred while calling method 'QueryablePersistenceService.query()' on 'org.openhab.persistence.rrd4j.internal.RRD4jPersistenceService@1fe595c0': Value must be between 0 and 100

And plenty of warnings like:
2023-09-05 12:24:47.148 [WARN ] [transport.mqtt.internal.Subscription] - A subscriber of type 'class org.openhab.binding.mqtt.generic.ChannelState' failed to process message '38' to topic 'zigbee2mqtt/robbDimmer_ff_masterbedroom_center2WL/brightness'.

At least, I got it partly solved → you should use Type dimmer in the Things definition. :see_no_evil:

  • The slider works now really nice (and it is easier than the transformation)
  • Linking the dimmer item to a switch item in the sitemap does not work correctly. It shows the state and the percentage. But as soon as you make a change with the slider, the state shows OFF (which it is not) → so I keep the original switch item.
Thing mqtt:topic:robbDimmer_ff_masterbedroom_center2WL "Switch FF Master Bedroom Main" (mqtt:broker:MosquittoMqttBroker) {
	Channels:
		Type switch : state       "State"        [ stateTopic = "zigbee2mqtt/robbDimmer_ff_masterbedroom_center2WL/state", commandTopic = "zigbee2mqtt/robbDimmer_ff_masterbedroom_center2WL/set", formatBeforePublish="{ \"state\": \"%s\" }", on="ON", off="OFF" ]
		Type dimmer : brightness  "Brightness"   [ stateTopic = "zigbee2mqtt/robbDimmer_ff_masterbedroom_center2WL/brightness", commandTopic = "zigbee2mqtt/robbDimmer_ff_masterbedroom_center2WL/set", formatBeforePublish="{ \"brightness\": %s }", min=0, max=254 ]
		Type number : linkquality "Link Quality" [ stateTopic = "zigbee2mqtt/robbDimmer_ff_masterbedroom_center2WL/linkquality" ]
}