How to use switch for light control

I have test switch with config:
items:

Switch MQTT_Test "Testing..." { mqtt="<[broker:testing/mqtt/topic:state:default], >[broker:testing/mqtt/back-topic:command:*:default]" }

sitemaps:

	Text label="MQTT_Test Sensor" icon="light" {
            Default item=MQTT_Test label="MQTT_Test"
        }

Hardware switch send “testing/mqtt/topic” message.
Hardware relay wait for “testing/mqtt/back-topic” message

From UI I able to control relay, but when I press hardware button - in UI I see ON, but no “testing/mqtt/back-topic” message.

What is right config to on/off light by button and UI?

Have you installed the mqtt binding?
If yes, can you post your mqtt.cfg located in your conf/services folder, please?
How do you monitor your mqtt traffic?

mqtt config very easy:

broker.url=tcp://192.168.0.4:1883

No any issues with mqtt. I able control relay from UI. Hardware send “testing/mqtt/topic” and I see ON in UI. When Ichange state in UI - relay on or off. But I can’t control relay via button. Button change state only in UI.
I monitor traffic with mqtt-spy, but it is no mqtt issue. The same behavior for switch on modbus.

So what you are saying is that when you change the switch on the OH UI, you don’t see the message testing/mqtt/back-topic:ON or OFF on mqtt spy, but the UI and the switch state changes when you publish testing/mqtt/topic:ON or OFF on mqtt-spy for testing

When i change switch state via OH UI i see message “testing/mqtt/back-topic” [ON|OFF]
But when my device publish “testing/mqtt/topic” - UI state changed, but OH doesn’t send “testing/mqtt/back-topic” with new state to device. So i can’t control light via button. Only via OH UI.

AAAHHHH. Ok.
So change the inbound binding to command instead of state:

Switch MQTT_Test "Testing..." { mqtt="<[broker:testing/mqtt/topic:command:default], >[broker:testing/mqtt/back-topic:command:*:default]" }
1 Like

BUT
I would recommend two items because you have 2 devices really, 1 switch and one light
so

Switch MQTT_Test_Switch "Testing Switch..." { mqtt="<[broker:testing/mqtt/topic:state:default], >[broker:testing/mqtt/back-topic:command:*:default]" }
Switch MQTT_Test_Light "Testing Light..." { mqtt=">[broker:testing/mqtt/back-topic:command:*:default]" }

And a rule:

rule "Test mqtt switch"
when
    MQTT_Test_Switch changed
then
    if (MQTT_Test_Switch.state == ON) {
        MQTT_Test_Light.sendCommand(ON)
    } else {
        MQTT_Test_Light.sendCommand(OFF)
    }
end

This way you separate the behaviours and if you wanted to command something else with that switch then you can change the rule

1 Like

I guess change binding to command instead state will work. Will test it today.
Config with rule works but i want to understand how to implement it without rules.
Thank, will provide feedback after testing.

Using the rules will give far more flexibility. Just advice