Using MQTT Binding on OpenHAB 2

I’m trying to add the MQTT binding (actual version 1.9) to my new OpenHAB 2 installation.

What do I exactly have to do? The manuals are very confusing and seem to be version-dependent.

I have installed “openhab2-addons” and “openhab2-addons-legacy”, and installed Mosquitto as MQTT broker. Also I added the project into the Eclipse Smart Home Designer and edited the “/service/mqtt.cfg” and “/services/mqtt-eventbus.cfg”, set the broker name and “localhost” as server (as Mosquitto is installed on my Raspberry).

Can someone give me more information how to use MQTT 1.9 on OpenHAB2? Are there some incompatibilities with these two versions? If yes, how to use another MQTT version (my Paper UI only contains the MQTT binding and action version 1.9). Do I need also the action?

3 Likes

MQTT 1.9 is legacy binding for OH2 so you will have to configure it using configuration files:

My example on OH2 running on RP3:

  1. Install mosquitto, the new version not the one in the original repo
wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
sudo apt-key add mosquitto-repo.gpg.key
cd /etc/apt/sources.list.d/
sudo wget http://repo.mosquitto.org/debian/mosquitto-wheezy.list
sudo apt-get install mosquitto

systemctl enable mosquitto
systemctl start mosquitto
systemctl status mosquitto
  1. Check your MQTT server is working using the free MQTT.fx http://mqttfx.jfx4ee.org

  2. Configure mqtt binding on OH2:
    /etc/openhab2/services/mqtt.cfg

mqtt:broker.url=tcp://localhost:1883
mqtt:broker.clientId=openhab
mqtt:broker.retain=true
mqtt:broker.async=false
  1. Add two switches:
    /etc/openhab2/items/switch.items
Switch Switch1 {mqtt=">[broker:myhome/bedroom/switch1:command:ON:1],>[broker:myhome/bedroom/switch1:command:OFF:0],<[broker:myhome/bedroom/switch1:state:ON:1],<[broker:myhome/bedroom/switch1:state:OFF:0"}
Switch Switch2 {mqtt=">[broker:myhome/bedroom/switch2:command:ON:1],>[broker:myhome/bedroom/switch2:command:OFF:0],<[broker:myhome/bedroom/switch2:state:ON:1],<[broker:myhome/bedroom/switch2:state:OFF:0]"}

Update the sitemap to the new switches and you are ready to go…

5 Likes

Thank you, I will try it later! So to clearify the installation steps: installing the “openhab2-addons” is not necessary but “openhab legacy addons” is? And what about the “legacy addon support” switch in the system settings in Paper UI?

1 Like

We used your configuration settings and now it the MQTT broker works! Do you know the issue why I still can’t see the bindung in the Paper UI under “Configuration -> Bindings”?

Like I said, currently there is only legacy OH1 version of MQTT binding. Legacy bindings will not appear in Paper UI.

If you would like to check status of the items, you can use karaf console access.

For the configuration you have only text files, but configuration is very simple…

1 Like

A “legacy” binding is a binding that has been obsoleted by a newer binding. This is not the case for the MQTT binding. The confusion regarding 1 vs. 2 bindings should be largely resolved once this PR is merged and docs.openhab.org has been updated, which should happen hopefully very soon.

In the meantime, please have a look at the new source document for the MQTT binding, which will be mirrored onto docs.openhab.org.

Update: The MQTT binding doc lives here.

1 Like

You are right, it is not legacy it is just version 1x but it will not integrate in Paper UI.
I personally prefer text configuration over GUI, especially with simple bindings that do not require some kind of auto discovery.

1 Like

MQTT is a main component of IOT and I’m amazed how complicated it is made to look when it is in fact very simple to implement. Misinformation and the github entry being written by programmers for programmers doesn’t help (sorry, just my opinion).

I spent days with jar files trying to get Openhab2 to work with MQTT when in fact it is as simple as installing MQTT through PaperUi, adding the URL of the broker in the MQTT.cfg file that it creates and adding the information to the switch definition in the items files as detailed above.

Keep it simple!!

8 Likes

Make sure to restart openhab completely after making changes to mqtt.cfg. It is not enough to just save the file for some reason.

Thanks for this gudie. It helped me get MQTT broker working after I also ha spent about two weeks without sucess following diffrent guides.

I have found it easiest to make sure Mosquito is running prior to loading Openhab. If you have the prompt window open you can see it connect client xxxxx on port xxxx.

Michael, maybe I am a bit off topic, but I shy-ed away from MQTT for a long time, because it looked complicated.
Then I got to use it, in a practical example by someone who could explain very well (wich was also my first use of openhab) and I was hooked. It is really extremely simple, once you get passed the programmers talk :wink:

Hi,

Can you specify the minimum version and give the reason, why using the newer one?

I have actually installed following version:

mosquitto version 1.3.4 (build date 2014-08-22 06:10:51+0000)

mosquitto is an MQTT v3.1 broker.

My problem is to send a command from a client over mqtt to openhab to control a rollershutter (thread: Sending command over Mqtt to OpenHAB)

Maybe this is something, where the newer version is required?

EDIT: I have test it with your sample with the mosquitto version 1.3.4:

Switch Switch1 {mqtt=">[mosquitto:myhome/bedroom/switch1:command:ON:1],>[mosquitto:myhome/bedroom/switch1:command:OFF:$
Switch Switch2 {mqtt=">[mosquitto:myhome/bedroom/switch2:command:ON:1],>[mosquitto:myhome/bedroom/switch2:command:OFF:$

and a sample sitemap:

sitemap demoswitch label="My MQTT Demo Switches" {
        Frame {
                Switch item=Switch1
                Switch item=Switch2
        }

        Frame {
                Default item=Rollladen_UG_links

        }
}

The demo switches are working as expected, but I don’t get it working with a rollershutter. I’m not sure, which concrete message I should send to change the percent value of the rollershutter.

Cheers!

Christian

UPDATE: Solved! See my solution in other thread (url above)

look, i love the promise of openHAB2 but im having the worst experience ever so far.
keep it simple right? this is what i was thinking when i decided to download the latest stable openhabian image… after the 4th try it still gave me errors that i could not sort(im no programmer)out.
3 more tries with the netInst version and i was away…
when i eventually got the thing to work(stable heartbeat with the leds),
i got a bit excited,but now im just frustrated.
i cannot get MQTT to work.
installed mosquitto on the pi3, it works, i can connect to it with devices etc.
can NOT get openhab2 to talk to the broker.
mqtt.cfg file
mqtt:mosquitto.url=tcp://localhost:1883
mqtt:mosquitto.clientId=openhab
mqtt:mosquitto.retain=true
mqtt:mosquitto.async=false

//sitemap
sitemap home label=“MyHouse”
{
Frame label=“Light Switch”
{
Switch item=STEVESW
}
Frame label=“Light Switch”
{
Switch item=LARRYSW
}
Frame label=“Light Switch”
{
Switch item=BRIANSW
}
Frame label=“MK-Room”
{
Switch item=Switch1
}
Frame label=“JK-Room”
{
Switch item=Switch2
}
Frame label=“Master Bedroom”
{
Switch item=DEMOSW
}
Frame label=“Living Room”
{
Switch item=DEMOSW
}
Frame label=“Garage”
{
Switch item=DEMOSW
}
Frame label=“Outside”
{
Switch item=DEMOSW
}
}

//switch items page

Switch STEVESW {mqtt=">[broker:home/lights/stevein:command:ON:1],>[broker:home/lights/stevein:command:OFF:0],<[broker:home/lights/stevein:state:ON:1],<[broker:home/lights/stevein:state:OFF:0]"}
Switch Switch1 {mqtt=">[broker:myhome/bedroom/switch1:command:ON:1],>[broker:myhome/bedroom/switch1:command:OFF:0],<[broker:myhome/bedroom/switch1:state:ON:1],<[broker:myhome/bedroom/switch1:state:OFF:0"}
Switch Switch2 {mqtt=">[mosquitto:myhome/bedroom/switch2:command:ON:1],>[mosquitto:myhome/bedroom/switch2:command:OFF:0],<[mosquitto:myhome/bedroom/switch2:state:ON:1],<[mosquitto:myhome/bedroom/switch2:state:OFF:0]"}

any help would be greatly appreciated as ive been at this for a week now and cannot get it to work.
i am probably overlooking something, and yes its gotten a little messy there…but for a start im just trying to get it to publish ANYTHING to the broker.

PLEASE HELP! :frowning:

1 Like

You shouldn’t use mqtt: infront of the config options since you are on OH2 and are using a dedicated mqtt.cfg file

mosquitto.url=tcp://localhost:1883
mosquitto.clientId=openhab
mosquitto.retain=true
mosquitto.async=false

Stop OH2, fix your mqtt.cfg, delete /var/lib/openhab2/config/org/openhab/mqtt.config (to remove stale configs) and start OH2

3 Likes

thanks for your Reply Dim, i seem to have got it working , more luck than good management…lol, but im still having some small issues so im going to to follow your suggestions, but how do i stop OH2 in openhabian?

it should be the same as in Debian Jessie:

sudo systemctl stop openhab2
1 Like

cool, ill try it now(thanks for your help btw)

1 Like

Dim! oh thats much better! thank you so much, now i can get on with the business of integrating everything!
Thanks again!

1 Like

I have installed MQTT binding 1.9.0 and configured it towards RabbitMQ

rabbitmq.url=tcp://192.168.1.30:1883
rabbitmq.clientId=openHAB
rabbitmq.user=openhab
rabbitmq.pwd=thepassword
rabbitmq.qos=1
rabbitmq.retain=true
rabbitmq.async=true

It refuse to connect, with error:

19:24:19.445 [ERROR] [penhab.io.transport.mqtt.MqttService] - Error starting broker connection
Unable to connect to server (32103) - java.net.ConnectException: Connection refused
        at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:75)[217:org.openhab.io.transport.mqtt:1.9.0]
        at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:538)[217:org.openhab.io.transport.mqtt:1.9.0]
        at java.lang.Thread.run(Thread.java:745)[:1.8.0_65]
Caused by: java.net.ConnectException: Connection refused
        at java.net.PlainSocketImpl.socketConnect(Native Method)[:1.8.0_65]
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)[:1.8.0_65]
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)[:1.8.0_65]
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)[:1.8.0_65]
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)[:1.8.0_65]
        at java.net.Socket.connect(Socket.java:589)[:1.8.0_65]
        at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:66)[217:org.openhab.io.transport.mqtt:1.9.0]
        ... 2 more

I have other MQTT clients connected with the same credentials.
Which MQTT protocol version is used in the binding?