MQTT switch status not updating

Hi all.

Following a long break from openHAB, I am now finding myself having to setup my house with MQTT2 rather than MQTT1. I am having trouble with getting the state of MQTT items to update in the Basic UI. I have done extensive searching, including reading the openHAB documentation and threads such as help-migrating-mqtt-1-x-binding-to-the-new-mqtt-2-x (new forum user, so I can’t hyperlink)

I am running openHAB 2.4.0-1 on a RPi3, using David Graeff’s MQTT Thing Binding.

Goal

Using MQTT, I want to have openHAB’s Basic UI reflect state of switch item (on/off) by reference to the text being published to a given MQTT topic.

Current situation

I can send out MQTT commands using the switches in the basic UI, however I can not get the state to update. In other words, I can turn my items on or off, but the Basic UI can’t tell me whether a current item is actually on or off in reality.

Background

I control ceiling speaks in different rooms of my house using an arduino relay board that subscribes to the MQTT topic “speakerControl” and listens for these commands: 0, 1, 2, 3, 4, 5, or 6.
Depending on the command the arduino receives, different relays will turn on while other relays turn off (the relays themselves switch certain speakers on or off). The arduino then publishes a text confirmation to the topic “speakerStatus”, such as “Study On”, to reflect which room’s speakers are being used.

In openHAB, I have a Generic MQTT Thing set up with 6 channels, one channel per room with the speakers I want to control. Each channel publishes a different command (1, 2, 3, 4, 5, or 6) to the topic “speakerControl”, depending on the speakers I want to turn on, or the command ‘0’ to the same topic f I want to turn the speakers off.

Each channel is set up to listen to “speakerStatus” as a status topic - but I am not sure how to incorporate this into switch state in the UI.

Each line in my openHAB items file references the channel topic as show in the paper UI (as a new user the forum wont allow me to copy my Things file to show you)

How do I set up openHAB to reflect the current state of the relays? I am unsure of the current coding to use - and whether it is easiest to do this in openHAB or modify the arduino code.

Any help is greatly appreciated.

Do you use an external programme to monitor which messages are sent, like MQTTFX? That way you could check the actual needed subscription to get those messages. Wh ich in turn you need to set to the StateTopic of each channel.

Thank you, Opus. I do use MQTTFX.

I have found a suitable workaround, which I have explained below:

As each channel in MQTT Generic Thing binding can listen to a different MQTT status topic, and by default can use the command “OFF” to update a switches’ state in the Basic UI, I updated my arduino code so that when I turn a given switch on via Basic UI, all other switches turn off.

An extract of the arduino code is below, and should make what I have said a bit clearer. Here, the arduino turns on relays 11 and 12 (main bedroom speakers), and all other relays off, when it sees the command “1” in the MQTT topic “speakerControl”, and also sends “OFF” commands to the unique topics openHAB is monitoring to determine switch states for the speakers in the other rooms of the house:

if(payload[0] == ‘1’){
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
digitalWrite(relay4, HIGH);
digitalWrite(relay5, HIGH);
digitalWrite(relay6, HIGH);
digitalWrite(relay7, HIGH);
digitalWrite(relay8, HIGH);
digitalWrite(relay9, HIGH);
digitalWrite(relay10, HIGH);
digitalWrite(relay11, LOW);
digitalWrite(relay12, LOW);
client.publish(“speakerStatus”, “MainBedroom On”);
client.publish(“speakerStatus/study”, “OFF”);
client.publish(“speakerStatus/baby”, “OFF”);
client.publish(“speakerStatus/bathroom”, “OFF”);
client.publish(“speakerStatus/kitchen”, “OFF”);
client.publish(“speakerStatus/outside”, “OFF”);