Switch Item - MQTT

I setup an Arduino with buttons on it that when depressed send a message to the MQTT broker 0 for “on” and 1 for “off”. I also am listening on the same channel on the arduino to turn the relay on or off.

The openhab UI is correctly turning on and off the relay, however when I hit a button on the arduino, it doesn’t update the openhab UI.

MQTT Subscribe:

FROM BUTTON ON ARDUINO

Client mosqsub/806-openhab received PUBLISH (d0, q0, r0, m0, 'livingroom/relay1', ... (1 bytes))
0
Client mosqsub/806-openhab received PUBLISH (d0, q0, r0, m0, 'livingroom/relay1', ... (1 bytes))
1


FROM OPENHAB UI

Client mosqsub/806-openhab received PUBLISH (d0, q0, r0, m0, 'livingroom/relay1', ... (1 bytes))
0
Client mosqsub/806-openhab received PUBLISH (d0, q0, r0, m0, 'livingroom/relay1', ... (1 bytes))
1

This is my item in the items file. I’m sure there’s something here I’m doing wrong:

Switch LivingRoom_Relay1 "Living Room Light [MAP(switch.map):%s]" (Bedroom,Lights) {mqtt=">[mymosquitto:livingroom/relay1:command:ON:0],>[mymosquitto:livingroom/relay1:command:OFF:1],<[mymosquitto:livingroom/relay1:state:default]"}

for completeness here is the contents of the map file.

OFF=0 ON=1

Nothing is showing up in the openhab.log as if something is misconfigured.

Anyone have any ideas??

Thanks in advance

It looks as if your map file has"off=0" but mqtt binding shows command “off=1”. Also, if you use a switch to toggle the relay does the switch or the relay send a status update to openhab?

Ya I switched that around when I posted it, it doesn’t seem to matter. The relay/switch sends updates to the MQTT broker that openHAB is of course subscribed to.

Just glancing, I think you need to swap the direction of your > to < since the mqtt variable is an input??

Hello, I have a similar issue. MQTT and OH works fine, but I want to display the inverted switch status. The reason is that I have a relay in the normal closed mode. I only want to have the item displayed as ON state. I feel my problem us in the :state:default]

Switch qGast12V "Gast 12V" <poweroutlet> (gESP8266) {mqtt="
                >[broker:/pucon/haus/IO12V/control:command:ON:1],
				>[broker:/pucon/haus/IO12V/control:command:OFF:0],
                <[broker:/pucon/haus/IO12V/state:state:default]" }

my MQTT code :

  // -- IO 12V ----------------------------------

    if( strstr( sub.topic().c_str(), T_IO12V )>0 ) {

        if (sub.payload_string() == "0") {
            pubMessage( T_IO12V S_STATE, "OFF" );
            digitalWrite(IO_IO12V,LOW);
            }

        else if (sub.payload_string() == "1") {
            pubMessage( T_IO12V S_STATE, "ON" );
            digitalWrite(IO_IO12V,HIGH);
            }
        }

and the current state feedback:

 pubMessage( T_IO12V S_STATE, digitalRead(IO_IO12V) ? "ON" : "OFF" );   

thanks in advance