Dimmer linked to 2 channels (switch/% value) jumping from 0 to 100% only

Hi All, firstly, i’m still running OH 2.5.9 on RPi 4.

I am trying to setup a dimmer via MQTT I have working separate % value and Switch ON/OFF Thing channels that update correctly.

When i try to link then both of these to the same ‘Dimmer’ item, the % value will just jump from 0 to 100% only. (I.e. whenever it is ON, it always shows 100%, no matter what the % value is).

I’ve used ‘Dimmer’ items before and follow guidance on this forum on how to link with separate Switch and % channels and it worked fine. But it just won’t work with these MQTT things… any help greatly appreaciated!

My MQTT things:

    Thing topic summerhouse_fader  "Summer House Light MQTT" @ "Summer House" {
           Type switch : summerhouse_fader1_sw "Switch 1" [ stateTopic="shfader/light/fader_1r/state" , commandTopic="shfader/light/fader_1r/command" , transformationPattern="JSONPATH:$.state", formatBeforePublish="{“state”:“%s”}", on="ON", off="OFF"]
           Type dimmer : summerhouse_fader1 "Fader 1" [ stateTopic="shfader/light/fader_1r/state" , commandTopic="shfader/light/fader_1r/command" , transformationPattern="JSONPATH:$.brightness", formatBeforePublish="{“brightness”:“%s”}", min="0", max="255"] 

The item:

Dimmer sh_fader1sw	"Summer house 1 switch" {channel="mqtt:topic:ada47998:summerhouse_fader:summerhouse_fader1_sw", channel="mqtt:topic:ada47998:summerhouse_fader:summerhouse_fader1"}

When i view the log, i can see the item updating to the correct value, but then straight away being over written by 0 or 100…

2021-05-17 11:07:54.912 [vent.ItemStateChangedEvent] - sh_fader1sw changed from 0 to 4

2021-05-17 11:07:54.921 [vent.ItemStateChangedEvent] - sh_fader1sw changed from 4 to 100

(FYI, the reason why i want to link both channels is so that Google home integration works with on/off and value commands).

Also, the thing receives the updates, but sending commands is not working.

I think my formatBeforePublish="{“brightness”:“%s”}" is wrong.

Device is expecting command {“brightness”:“100”} , but trying to put " symbol into the thing file causes issues… i copied some code from another forum post, which seems to use another type of " symbol… but doesn’t work…

This is what escaping is for, using a backslash to hide ‘internal’ quotemarks.

But you don’t have to link both channels in both ‘directions’. For example,if your switch channel has no stateTopic then it will never issue “off=0% ,on= 100%” style updates to your shared Item. The commandTopic will still work with ON/OFF commands.

Thank you! the backlash worked!

I now have both separate items sending commands correctly.

I removed the ‘state’ topic from switch channel and tried again, but still didn’t work (switch would work once, but then stop). I believe it might be to do with weird behaviour of the dimmer (esphome). I noticed that when it turns ‘off’ it always sends MQTT brightness of 100%, even though, if you send an “ON” command it’ll go back to previous brightness. Seems it always sends an update to brightness channel of 100% whenever it is “off”. Weird.

I removed the state channel from the brightness thing and all working as expected now, but obviously the slider doesn’t update when external controls edit the dimmer, but i can live with that!

It can be fiddly,to match up openHAB’s model of a Dimmer to a real-world device with different characteristics.
In particular Dimmer Item is happy to handle commands ON/OFF … but has no on/off state, only 0-100.

Trouble comes with a device reporting both OFF and a non-zero brightness; that’s just not compatible with OH which can only represent OFF internally with 0%.

It is possible (but fiddly) to construct a specialist transformation, to deal with the whole payload in one step
{“state”:xxx, “brightness”:nnn}
with a little logic - if the received state is “off”, pass 0% to Item.
Otherwise pass nnn%

Likewise, there is trouble sending to two different topics where that is used for separate on/off and % commands.
Luckily here you seem to have only one topic, but perhaps a need to send two elements in one JSON.
Again a fiddly transformation can take care of translating a single OH Dimmer command to JSON.
If you get a non-zero numeric command, convert to
{“state”:ON, “brightness”:nnn}
If a zero numeric,
{“state”:OFF, “brightness”:0}
If ON,
{“state”:ON, “brightness”:100}
{“state”:OFF, “brightness”:0}

There’s no real way to tackle INCREASE/DECREASE commands in this way, because you don’t have access to the starting value in this context; but these are rarely used and few bindings implement them.
You’d have to resort to rules to add that feature, if wanted.

1 Like

Thanks! Super helpful!!! I have it working perfectly now. Can control the light on/off and brightness from a remote rotary encoder switch, google home and OH sitemap… all working together :slight_smile: perfect!

Hi @Matt4 I have just started investigating ESPHome and have discovered this exact same problem… any chance you could post your full solution please?