Please help with MQTT binding thought process

I am still trying to integrate MQTT into OpenHAB so that OH subscribes to a topic, and turns on/off a light when the topic changes from on to off.

Long story short, I want a button on a remote device to write to:

/myhouse/office/switch1/

And when OH sees “on”, it triggers a switch. I have configured the switch as follows (line breaks added for readability, but in the config is it all one line):

 Switch officeLight "Office Light" {mqtt="
<[mosquitto:/myhouse/office/switch1/command:command:ON:1],
<[mosquitto:/myhouse/office/switch1/command:command:OFF:0],
>[mosquitto:/myhouse/office/switch1/state:state:ON",
>[mosquitto:/myhouse/office/switch1/state:state:OFF", autoupdate="false"}

I have reviewed the MQTT binding document, but I am a bit lost. I want the switch to respond to “command” and publish “state” - where have I gone wrong here?

Any assistance is appreciated!

Bob

have you configured your /etc/openhab2/services/mqtt.cfg file?

You have an extra quote in your item config (end of 4th line):

 Switch officeLight "Office Light" {mqtt="
<[mosquitto:/myhouse/office/switch1/command:command:ON:1],
<[mosquitto:/myhouse/office/switch1/command:command:OFF:0],
>[mosquitto:/myhouse/office/switch1/state:state:ON,
>[mosquitto:/myhouse/office/switch1/state:state:OFF", autoupdate="false"}

Usually, it works in the following way: A command is issued by openHAB to an item (for example by using a switch on a sitemap) and then the mqtt config will publish the command to the outbound topic. (with a transformation from ON to 1 and from OFF to 0). Additionally, the status of the remote device is read by openHAB using the inbound configuration (assuming that the remote device publishes status info on the topic).

So, for outbound (>), you would use the command and for inbound (<), you would use the state.
Of course, you can have the state in an outbound config and the command in an inbound.

Example:

 Switch officeLight "Office Light" {mqtt="
>[mosquitto:/myhouse/office/switch1/command:command:ON:1],
>[mosquitto:/myhouse/office/switch1/command:command:OFF:0],
<[mosquitto:/myhouse/office/switch1/state:state:default"}

Remember the way that things are connected:
OH2 ↔ Broker ↔ remote device
So: OH2 publishes something (a command) to a topic and in turn the remote device subscribes to it.
The other way works similarly: the remote device publishes something (its state) to a topic and OH2 subscribes to it.

I don’t think that you should use autoupdate="false". If you do, then the state of the item will not be updated by the commands issued to it.

http://docs.openhab.org/addons/bindings/mqtt1/readme.html

2 Likes

BAM - THANKS!

See what happens when there is not enough coffee around? Lol.

I changed the configuration based on your recommendations to this:

Switch officeLight      "Office Light"  {mqtt="
>[mosquitto:/myhouse/office/switch1/state:state:OFF:OFF],
>[mosquitto:/myhouse/office/switch1/state:state:ON:ON],
<[mosquitto:/myhouse/office/switch1/command:command:default]", 
insteonplm="1b.8e.5b:F00.00.02#switch"}

This publishes state to one topic and subscribes to another - works like a charm without the loops that I was running into during troubleshooting :slight_smile:
Thanks!

1 Like