Set switch mqtt read-only mode

  • Platform information:
  • Hardware: Raspberry PI3+
  • OS: openHABian
  • Java Runtime Environment: javac 1.8.0_222
  • openHAB version: 2.5.3-1

I publish with arduino in mqtt, a sensor that has only two states: on and off. ( “controller_garage / sensor / state”)
how do I configure the parameters on the openhab .things configuration?

//MQTT Binding
Bridge mqtt:systemBroker:embedded-mqtt-broker “MQTT Broker” @ “Garage” [host=“localhost”, secure=false, retain=false ] {
Thing topic mqtt_power “MQTT Power” @ “Garage” {
Type number : current “RMS Current” [stateTopic=“controller_garage/sensor/current”]
Type number : voltage “RMS Voltage” [stateTopic=“controller_garage/sensor/voltage”]
Type number : power “RMS Power” [stateTopic=“controller_garage/sensor/power”]
Type switch : state “State” [commandTopic=“controller_garage/sensor/state”]

.things configuration

mqtt topic

thank you

On the MQTT channel, you use the stateTopic, not the commandTopic.

You’d normally represent a read-only binary device in Items as a Contact type.

do not work



the value of STATE is equal to NULL. Where am I wrong?

What are the payloads on that topic? You probably want to turn them into ON and OFF, suitable for a Switch type, using the on= off= channel options maybe?

The topic payloads are “on” and “off.”
I would like to display a switch that switches based on the payload as in the image


Then you’d need to use a Switch type Item, and use the on= off= channel options to match your MQTT payload.

do not work

I want to explain that this is not a command.
if it receives an “off” payload the switch must switch to OFF
if it receives an “on” payload the switch must switch to ON
thank you

Link a string type channel and Item to the same topic. see what you get.

do not work. The value of “controller_garage/sensor/state” on OpenHab is NULL


Okay, you seem to have proved there is no MQTT message on that topic yet.

if you look at the first image (mqqt topic), you can see that the message is off
The topic publishes only two payloads possible “on and off”
I can’t get this payload to read to openhab

I did look at your image, but your string channel appears not to be seeing your topic, you tell us that. If your remote device doesn’t set “retain” on its messages, then your string topic won’t see any messages until one is sent. There’s nothing we can do from here, you have to investigate.

Just to confirm that you are doing it right, make another string channel on one of your other topics that you think is working.
Make sure you give it some new unique name. You seem to have chosen the same name for your new string test channel as the switch channel that doesn’t work, which might be your problem. Have you other channels that you haven’t shown us called “state”?

thanks for your help, I solved it in part.
the error was in the .items file
this is the correct items

now I have a problem.
I see a text no a switch

how can I do?
thank you

Yes. It’s a string channel. Now you have found out how to configure, you probably want to change it back to a switch type channel and Item if you want it to look like a switch.

how can I do to look like a switch?

See your own post #3

configure a switch type MQTT channel
Link that to a Switch type Item

do not work

the payload is “on”


Now we’re back to this part -

sorry I do not understand
I want a switch, on OpenHab, that switches OFF when the topic sends the payload “off” and that switches ON when the topic sends the payload “on”.
can you write me a sample code?
thank you

Like the last line of your screenshot in your post #7

I can’t copy-paste the line for you, because it’s only a screenshot.

You would need to link that switch channel to a Switch type Item as well.