# Using MQTT Binding on OpenHAB 2

**URL:** <https://community.openhab.org/t/using-mqtt-binding-on-openhab-2/21988>\
**Category:** Bindings\
**Tags:** openhab2, binding, mqtt-binding\
**Created:** [February 4, 2017, 2:24am UTC](https://community.openhab.org/t/using-mqtt-binding-on-openhab-2/21988 "2017-02-04T02:24:07Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmueller1607](https://community.openhab.org/user_avatar/community.openhab.org/dmueller1607/32/71981_2.png) [@dmueller1607](https://community.openhab.org/u/dmueller1607)\
**Post date:** [February 4, 2017, 2:24am UTC](https://community.openhab.org/t/using-mqtt-binding-on-openhab-2/21988/1 "2017-02-04T02:24:07Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![omadon](https://community.openhab.org/letter_avatar_proxy/v4/letter/o/9d8465/32.png) [@omadon](https://community.openhab.org/u/omadon)\
**Post date:** [February 4, 2017, 5:02am UTC](https://community.openhab.org/t/using-mqtt-binding-on-openhab-2/21988/2 "2017-02-04T05:02:55Z")

</div>

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

```auto
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](http://mqttfx.jfx4ee.org)

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

```php
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

```php
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…

---

<div class="post-metadata">

**Author:** ![dmueller1607](https://community.openhab.org/user_avatar/community.openhab.org/dmueller1607/32/71981_2.png) [@dmueller1607](https://community.openhab.org/u/dmueller1607)\
**Post date:** [February 4, 2017, 5:25am UTC](https://community.openhab.org/t/using-mqtt-binding-on-openhab-2/21988/3 "2017-02-04T05:25:51Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dmueller1607](https://community.openhab.org/user_avatar/community.openhab.org/dmueller1607/32/71981_2.png) [@dmueller1607](https://community.openhab.org/u/dmueller1607)\
**Post date:** [February 4, 2017, 10:28am UTC](https://community.openhab.org/t/using-mqtt-binding-on-openhab-2/21988/4 "2017-02-04T10:28:31Z")

</div>

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”?

---

<div class="post-metadata">

**Author:** ![omadon](https://community.openhab.org/letter_avatar_proxy/v4/letter/o/9d8465/32.png) [@omadon](https://community.openhab.org/u/omadon)\
**Post date:** [February 4, 2017, 12:50pm UTC](https://community.openhab.org/t/using-mqtt-binding-on-openhab-2/21988/5 "2017-02-04T12:50:32Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![watou](https://community.openhab.org/user_avatar/community.openhab.org/watou/32/70595_2.png) [@watou](https://community.openhab.org/u/watou)\
**Post date:** [February 4, 2017, 1:14pm UTC](https://community.openhab.org/t/using-mqtt-binding-on-openhab-2/21988/6 "2017-02-04T13:14:31Z")

</div>

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](https://github.com/openhab/openhab-docs/pull/284) is merged and [docs.openhab.org](http://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](https://github.com/openhab/openhab1-addons/blob/master/bundles/binding/org.openhab.binding.mqtt/README.md), which will be mirrored onto [docs.openhab.org](http://docs.openhab.org).

Update: The MQTT binding doc lives [here](http://docs.openhab.org/addons/bindings/mqtt1/readme.html).

---

<div class="post-metadata">

**Author:** ![omadon](https://community.openhab.org/letter_avatar_proxy/v4/letter/o/9d8465/32.png) [@omadon](https://community.openhab.org/u/omadon)\
**Post date:** [February 4, 2017, 2:47pm UTC](https://community.openhab.org/t/using-mqtt-binding-on-openhab-2/21988/7 "2017-02-04T14:47:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mikyh](https://community.openhab.org/letter_avatar_proxy/v4/letter/m/a3d4f5/32.png) [@mikyh](https://community.openhab.org/u/mikyh)\
**Post date:** [February 4, 2017, 3:14pm UTC](https://community.openhab.org/t/using-mqtt-binding-on-openhab-2/21988/8 "2017-02-04T15:14:55Z")

</div>

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!!

---

<div class="post-metadata">

**Author:** ![Ahmad\_Khalaf](https://community.openhab.org/user_avatar/community.openhab.org/ahmad_khalaf/32/18663_2.png) [@Ahmad\_Khalaf](https://community.openhab.org/u/Ahmad_Khalaf)\
**Post date:** [February 4, 2017, 8:47pm UTC](https://community.openhab.org/t/using-mqtt-binding-on-openhab-2/21988/9 "2017-02-04T20:47:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Daniel\_Lundberg](https://community.openhab.org/user_avatar/community.openhab.org/daniel_lundberg/32/36434_2.png) [@Daniel\_Lundberg](https://community.openhab.org/u/Daniel_Lundberg)\
**Post date:** [February 4, 2017, 10:10pm UTC](https://community.openhab.org/t/using-mqtt-binding-on-openhab-2/21988/10 "2017-02-04T22:10:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Andy\_North](https://community.openhab.org/letter_avatar_proxy/v4/letter/a/a9a28c/32.png) [@Andy\_North](https://community.openhab.org/u/Andy_North)\
**Post date:** [February 8, 2017, 7:31am UTC](https://community.openhab.org/t/using-mqtt-binding-on-openhab-2/21988/11 "2017-02-08T07:31:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Kees\_van\_Gelder](https://community.openhab.org/user_avatar/community.openhab.org/kees_van_gelder/32/17497_2.png) [@Kees\_van\_Gelder](https://community.openhab.org/u/Kees_van_Gelder)\
**Post date:** [June 1, 2017, 8:47am UTC](https://community.openhab.org/t/using-mqtt-binding-on-openhab-2/21988/13 "2017-06-01T08:47:38Z")

</div>

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 😉

---

<div class="post-metadata">

**Author:** ![Chris81T](https://community.openhab.org/user_avatar/community.openhab.org/chris81t/32/24057_2.png) [@Chris81T](https://community.openhab.org/u/Chris81T)\
**Post date:** [June 1, 2017, 3:48pm UTC](https://community.openhab.org/t/using-mqtt-binding-on-openhab-2/21988/14 "2017-06-01T15:48:11Z")

</div>

Hi,

> [@omadon](#):
>
> Install mosquitto, the new version not the one in the original repo

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](https://community.openhab.org/t/sending-command-over-mqtt-to-openhab/29274))

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)

---

<div class="post-metadata">

**Author:** ![Levi](https://community.openhab.org/letter_avatar_proxy/v4/letter/l/ce73a5/32.png) [@Levi](https://community.openhab.org/u/Levi)\
**Post date:** [June 3, 2017, 2:22am UTC](https://community.openhab.org/t/using-mqtt-binding-on-openhab-2/21988/15 "2017-06-03T02:22:22Z")

</div>

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! ☹

---

<div class="post-metadata">

**Author:** ![Dim](https://community.openhab.org/user_avatar/community.openhab.org/dim/32/4568_2.png) [@Dim](https://community.openhab.org/u/Dim)\
**Post date:** [June 3, 2017, 3:20am UTC](https://community.openhab.org/t/using-mqtt-binding-on-openhab-2/21988/16 "2017-06-03T03:20:05Z")

</div>

> [@Levi](#):
>
> mqtt.cfg file  
> mqtt:mosquitto.url=tcp://localhost:1883  
> mqtt:mosquitto.clientId=openhab  
> mqtt:mosquitto.retain=true  
> mqtt:mosquitto.async=false

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

```auto
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

---

<div class="post-metadata">

**Author:** ![Levi](https://community.openhab.org/letter_avatar_proxy/v4/letter/l/ce73a5/32.png) [@Levi](https://community.openhab.org/u/Levi)\
**Post date:** [June 3, 2017, 3:36am UTC](https://community.openhab.org/t/using-mqtt-binding-on-openhab-2/21988/17 "2017-06-03T03:36:53Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Dim](https://community.openhab.org/user_avatar/community.openhab.org/dim/32/4568_2.png) [@Dim](https://community.openhab.org/u/Dim)\
**Post date:** [June 3, 2017, 3:38am UTC](https://community.openhab.org/t/using-mqtt-binding-on-openhab-2/21988/18 "2017-06-03T03:38:32Z")

</div>

> [@Levi](#):
>
> but how do i stop OH2 in openhabian?

it should be the same as in Debian Jessie:

```auto
sudo systemctl stop openhab2

```

---

<div class="post-metadata">

**Author:** ![Levi](https://community.openhab.org/letter_avatar_proxy/v4/letter/l/ce73a5/32.png) [@Levi](https://community.openhab.org/u/Levi)\
**Post date:** [June 3, 2017, 3:40am UTC](https://community.openhab.org/t/using-mqtt-binding-on-openhab-2/21988/19 "2017-06-03T03:40:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Levi](https://community.openhab.org/letter_avatar_proxy/v4/letter/l/ce73a5/32.png) [@Levi](https://community.openhab.org/u/Levi)\
**Post date:** [June 3, 2017, 3:50am UTC](https://community.openhab.org/t/using-mqtt-binding-on-openhab-2/21988/20 "2017-06-03T03:50:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![4integration](https://community.openhab.org/letter_avatar_proxy/v4/letter/4/838e76/32.png) [@4integration](https://community.openhab.org/u/4integration)\
**Post date:** [June 11, 2017, 5:26pm UTC](https://community.openhab.org/t/using-mqtt-binding-on-openhab-2/21988/21 "2017-06-11T17:26:43Z")

</div>

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?

[Next page](https://community.openhab.org/t/using-mqtt-binding-on-openhab-2/21988.md?page=2)
