Dimmer Thing Configuration. What is the commandTopic?

I am trying to configure my first dimmer and connect it to a switch and slider. The following works fine:

  • Turning the light on / off (via state)
  • Getting the dimmer level (brightness_percent)

Somehow, I cannot figure out how to set the dimmer level (brightness_percent).
If I use MQTT Explorer, I set the following JSON string on Topic “zigbee2mqtt/my_dimmer/set”

{ "brightness_percent": 50, "state": "ON" }

I have the following Things configuration

Thing mqtt:topic:my_dimmer "Dimmer 1" (mqtt:broker:MosquittoMqttBroker) {
	Channels:
		Type switch : state       "State"        [ stateTopic = "zigbee2mqtt/my_dimmer/state", commandTopic = "zigbee2mqtt/my_dimmer/set", on="ON", off="OFF" ]
		Type number : brightness  "Brightness"   [ stateTopic = "zigbee2mqtt/my_dimmer/brightness_percent", commandTopic = "zigbee2mqtt/my_dimmer/set" ]
		Type number : linkquality "Link Quality" [ stateTopic = "zigbee2mqtt/my_dimmer/linkquality" ]
}

I tried all kind of variations in the commandTopic for brightness_percent. What am I missing?

Have a look here.
It also depends on your light bulb brand etc., Zigbee2mqtt website usually lists the command topics though too.

Thanks for the feedback. I am using zigbee2mqtt with a Livolo Slide Dimmer. As far as I know, I need to do the manual configuration via Things. The description of how to control the dimmer is clear via JSON.

But I can’t seem to figure out the command topic for brightness_percent
I was thinking I might need a JSON transformation, but I am also not sure about that.

Based on what i can see on the zigbee2mqtt page the command topic is:

brightness: To control the brightness publish a message to topic zigbee2mqtt/FRIENDLY_NAME/set with payload {“brightness”: VALUE} where VALUE is a number between 0 and 254. To read the brightness send a message to zigbee2mqtt/FRIENDLY_NAME/get with payload {“brightness”: “”}.

And yes, if you do not use the experimental attributes feature of zigbee2mqtt it looks like you need to do an outgoing json transformation.

The documentation is incomplete, you can also directly set “brightness_percent” (easier to link to a slider). Tested with MQTT explorer.

{ "brightness_percent": 50, "state": "ON" }

I also tried this:

Type number : brightness  "Brightness"   [ stateTopic = "zigbee2mqtt/my_dimmer/brightness_percent", commandTopic = "zigbee2mqtt/my_dimmer/set", transformationPattern="JSONPATH:$.brightness_percent" ]

But in MQTT explorer, I still see:

set = VALUE

Instead of:

set = { "brightness_percent": VALUE }

Struggling with finding the correct syntax here :wink:

Why? transformationPattern is about incoming payloads, and JSONPATH is about extracting data from JSON, not building it.
You need to understand this, not throw random stuff at it.

MQTT messages have two parts - a topic, like a mailing address, and a payload, like the contents of a letter.

There’s your topic, your topic is fine, leave it alone.

That’s the payload you want.
The openHAB Dimmer Item only allows commands like ON and values like 55, so you have to make your payloads by transforming or otherwise.

First thing to note is that your wanted payload has two parameters. openHAB commands have only one. So it’s difficult to transform into two different things.
Not impossible, but as you are struggling with simple so far let’s simplify the problem.
Would this work?
zigbee2mqtt/my_dimmer/set { "brightness_percent": 50 }

If it does, you’ve simplified your task. You can make a payload out of fixed text
{ "brightness_percent": XX }
and substitute your openHAB numeric command into the XX position.

Here’s how you do that
formatBeforePublish="{ \"brightness\": %s }"

1 Like

Thanks for the pointer. That is exactly what solves the problem. There is plenty of documentation, so if you do not know what you’re looking for, it is difficult.

Type number : brightness  "Brightness"   [ stateTopic = "zigbee2mqtt/my_dimmer/brightness_percent", commandTopic = "zigbee2mqtt/my_dimmer/set", formatBeforePublish="{ \"brightness_percent\": %s }" ]

I hope you’re getting a feel for how these various parts work together.

If you really wanted a payload like -

it can be done with scripted transformation.
You might write a little javascript that looked at the openHAB Dimmer Item command - which might be ON or OFF or 0 or 75 etc. - and decided what to do.

If ON, pass { "brightness_percent": 100, "state": "ON" }
if OFF, pass { "brightness_percent": 0, "state": "OFF" }
if numeric 0, pass { "brightness_percent": 0, "state": "OFF" }
if numeric NN > 0 , pass { "brightness_percent": NN, "state": "ON" }

Such a script could then be called by the channel’s transformationPatternOut= parameter, and you wouldn’t need the format stuff.

This was just me playing around in MQTT explorer to see what commands I needed to properly control the dimmer. There is no need to send combined payloads (except when playing around with transitions).

I was indeed thinking of creating a script/rule to pass the JSON strings, but with the formatBeforePublish I do not need too. Thanks for the explanation.