# OH2 mqtt commands

**URL:** <https://community.openhab.org/t/oh2-mqtt-commands/33734>\
**Category:** Beginners\
**Tags:** mosquitto, mqtt, rpi\
**Created:** [August 29, 2017, 9:58pm UTC](https://community.openhab.org/t/oh2-mqtt-commands/33734 "2017-08-29T21:58:27Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![xSERGIOx](https://community.openhab.org/letter_avatar_proxy/v4/letter/x/858c86/32.png) [@xSERGIOx](https://community.openhab.org/u/xSERGIOx)\
**Post date:** [August 29, 2017, 9:58pm UTC](https://community.openhab.org/t/oh2-mqtt-commands/33734/1 "2017-08-29T21:58:28Z")

</div>

Hi

I’m new to this smart home thing. So I’m following every tutorial I found but I’m having some problems to make OH2 getting and sending instructions  
I have RPi + Raspbian + mosquitto. My goal is to send and receive from/to an arduino to OH2.

Resuming: My mosquitto broker seams to work fine (tested in the rapbian terminal and with a remote pc using mqtt.fx), so the problem must lie somewhere with OH2 config:  
1 - I added a file mqtt.cfg to /etc/openhab2/services with the content (saw in a tutorial somewhere):  
mqtt:broker.url=tcp://localhost:1883  
mqtt:broker.clientId=openhab  
mqtt:broker.retain=true  
mqtt:broker.async=false

1. Added 2 items:  
Switch ArduinoRelay2 “Nome da tomada 2” {mqtt=“\>[broker:mikasa/garagem/relay2:command:ON:1],\>[broker:mikasa/garagem/relay2:command:OFF:0],\<[broker:mikasa/garagem/relay2:state:ON:1],\<[broker:mikasa/garagem/relay:state:OFF:0]”}  
Number ArduinoThermoresistor “Temperatura [%.1f C]” {mqtt=“\<[broker:mikasa/garagem/temperatura:state:default]”}

2. and added those to the sitemap so they can be seen in the OH2 browser

3. from the raspbian terminal typed:  
mosquitto\_pub -h localhost -t mikasa/garagem/temperatura -m “22”

My goal is to get and set some relay status and temperature from a thermoresistor.  
Can someone spot whats wrong here?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [August 29, 2017, 10:12pm UTC](https://community.openhab.org/t/oh2-mqtt-commands/33734/2 "2017-08-29T22:12:09Z")

</div>

Please read the [docs](http://docs.openhab.org/addons/bindings/mqtt1/readme.html).

Did you actually install the MQTT Binding? If you had successfully then a default services/mqtt.cfg should have been created for you.

Remove the “mqtt:” part of your configs, that is the old OH 1.x style of configuration back when all the configs were in the same file.

Have you set up username/password on Mosquitto? If so you need to fill out the mqtt:user and mqtt:password fields.

---

<div class="post-metadata">

**Author:** ![xSERGIOx](https://community.openhab.org/letter_avatar_proxy/v4/letter/x/858c86/32.png) [@xSERGIOx](https://community.openhab.org/u/xSERGIOx)\
**Post date:** [August 29, 2017, 11:16pm UTC](https://community.openhab.org/t/oh2-mqtt-commands/33734/3 "2017-08-29T23:16:27Z")

</div>

Hi Rich

Thanks for your reply.

> > Did you actually install the MQTT Binding? If you had successfully then a default services/mqtt.cfg should \>\>have been created for you.  
> > I did now… It created a new file called mqtt-eventbus.cfg.

> > Remove the “mqtt:” part of your configs, that is the old OH 1.x style of configuration back when all the \>\>configs were in the same file.  
> > I delete the file mqtt.cfg. OH created a new file descrived above.  
> > That file mqtt-eventbus.cfg dont have the same syntax. It also refers to a openhab.cfg file that i cant find.

> > Have you set up username/password on Mosquitto? If so you need to fill out the mqtt:user and \>\>mqtt:password fields.  
> > Yes I did.

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [August 30, 2017, 1:30am UTC](https://community.openhab.org/t/oh2-mqtt-commands/33734/4 "2017-08-30T01:30:04Z")

</div>

There should be an mqtt.cfg file too. You don’t want to mess with the mqtt-eventbus.cfg.

There is no longer an openhab.cfg file. That is a 1.x version thing and doesn’t exist for OH 2.x

Not knowing the order you did things I can’t say what happened.But I recommend uninstalling the mqtt binding. Deleting the mqtt\*.cfg files. Then reinsall the mqtt binding.

Then fill out the config as documented in the readme, including the username and password.

---

<div class="post-metadata">

**Author:** ![xSERGIOx](https://community.openhab.org/letter_avatar_proxy/v4/letter/x/858c86/32.png) [@xSERGIOx](https://community.openhab.org/u/xSERGIOx)\
**Post date:** [August 30, 2017, 5:31pm UTC](https://community.openhab.org/t/oh2-mqtt-commands/33734/5 "2017-08-30T17:31:52Z")

</div>

I did reinstall the mqtt binding. There is now the file mqtt.cfg  
I uncommented and chaged changed the lines:  
.url=tcp://localhost:1883  
.user=myuser  
.pwd=mypass  
.qos=1

my item is configurated like this  
Number ArduinoThermoresistor “Temperatura [%.1f C]” {mqtt=“\<[broker:mikasa/garagem/temperatura:state:default]”}

In my Windows PC I open mqtt.fx and subscrive the topic “mikasa/#”  
then at the RPi terminal typed:  
mosquitto\_pub -h localhost -t mikasa/garagem/temperatura -m “20”

mqtt.fx gets the message, OH dont change a thing

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [August 30, 2017, 6:03pm UTC](https://community.openhab.org/t/oh2-mqtt-commands/33734/6 "2017-08-30T18:03:35Z")

</div>

Once again, fill out the config **as documented in the binding’s readme**

[http://docs.openhab.org/addons/bindings/mqtt1/readme.html](http://docs.openhab.org/addons/bindings/mqtt1/readme.html)

> `<broker>.url` …  
> `<broker>` is an alias name for the MQTT broker. This is the name you can use in the item binding configurations afterwards.  
> …

---

<div class="post-metadata">

**Author:** ![xSERGIOx](https://community.openhab.org/letter_avatar_proxy/v4/letter/x/858c86/32.png) [@xSERGIOx](https://community.openhab.org/u/xSERGIOx)\
**Post date:** [August 30, 2017, 9:22pm UTC](https://community.openhab.org/t/oh2-mqtt-commands/33734/7 "2017-08-30T21:22:34Z")

</div>

i’m following the syntax… replaced broker by mosquitto, in mqtt.cfg and in my item same result.  
is there a tutorial for OH2 with mqtt? only find for OH1

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [August 30, 2017, 9:53pm UTC](https://community.openhab.org/t/oh2-mqtt-commands/33734/8 "2017-08-30T21:53:47Z")

</div>

[http://docs.openhab.org/addons/bindings/mqtt1/readme.html](http://docs.openhab.org/addons/bindings/mqtt1/readme.html)

This is the tutorial with examples.

If you were following the syntax your mqtt.cfg file would look like:

```auto
mosquitto.url=tcp://localhost:1883
mosquitto.user=myuser
mosquitto.pwd=mypass
mosquitto.qos=1

```

rather than what I assume (since you didn’t use code fences anything between `< >` gets hidden) you have which is:

```auto
<mosquitto>.url=tcp://localhost:1883
<mosquitto>.user=myuser
<mosquitto>.pwd=mypass
<mosquitto>.qos=1

```

and your Item would be:

```
Number ArduinoThermoresistor “Temperatura [%.1f C]” {mqtt="<[mosquitto:mikasa/garagem/temperatura:state:default]"}

```

instead of:

```
Number ArduinoThermoresistor “Temperatura [%.1f C]” {mqtt="<[broker:mikasa/garagem/temperatura:state:default]"}

```

Look at the examples on the readme. Notice how it consistently uses the same name for the “broker” part in both the configs and the Items.

---

<div class="post-metadata">

**Author:** ![xSERGIOx](https://community.openhab.org/letter_avatar_proxy/v4/letter/x/858c86/32.png) [@xSERGIOx](https://community.openhab.org/u/xSERGIOx)\
**Post date:** [August 30, 2017, 10:41pm UTC](https://community.openhab.org/t/oh2-mqtt-commands/33734/9 "2017-08-30T22:41:09Z")

</div>

the files are like that and is not working…  
Im also realized that my page is not showing the date and some of the icons are not the same as the page  
[http://docs.openhab.org/configuration/sitemaps.html](http://docs.openhab.org/configuration/sitemaps.html)

I’ll try to install everything from the begining and see if it solves the problem.

---

<div class="post-metadata">

**Author:** ![xSERGIOx](https://community.openhab.org/letter_avatar_proxy/v4/letter/x/858c86/32.png) [@xSERGIOx](https://community.openhab.org/u/xSERGIOx)\
**Post date:** [September 3, 2017, 3:11pm UTC](https://community.openhab.org/t/oh2-mqtt-commands/33734/10 "2017-09-03T15:11:25Z")

</div>

After some days struggling to get a solution… The thing is… By this date, there is no solution.  
The mosquitto version that cames with the raspbian image don’t work with openhab2, and mosquitto can’t be updated for the latest raspbian stretch as shown at [https://github.com/eclipse/mosquitto/issues/529](https://github.com/eclipse/mosquitto/issues/529)  
All I can do now is wait for some gentil soul works on a solution 😖

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [September 3, 2017, 4:04pm UTC](https://community.openhab.org/t/oh2-mqtt-commands/33734/11 "2017-09-03T16:04:11Z")

</div>

Is there any reason you must be on stretch? It works just fine on Jessie.

That’s better than just giving up. Some people are still running on weezy just fine.

If you are cohosting OH and mosquito on the same pi, I’d recommend using openHABian.

---

<div class="post-metadata">

**Author:** ![xSERGIOx](https://community.openhab.org/letter_avatar_proxy/v4/letter/x/858c86/32.png) [@xSERGIOx](https://community.openhab.org/u/xSERGIOx)\
**Post date:** [September 3, 2017, 5:00pm UTC](https://community.openhab.org/t/oh2-mqtt-commands/33734/12 "2017-09-03T17:00:41Z")

</div>

I’m using the stretch because it’s the image that is available to download at:  
[https://www.raspberrypi.org/downloads/raspbian/](https://www.raspberrypi.org/downloads/raspbian/)  
cant find the jessie image.

openhabian dont even starts. gives a processor error in the config (tried twice with 2 different SD cards).  
It refers to a processor error…

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [September 3, 2017, 6:36pm UTC](https://community.openhab.org/t/oh2-mqtt-commands/33734/13 "2017-09-03T18:36:06Z")

</div>

@ThomDietrich, any ideas?

I just installed Jessie on a brand new Pi Zero W and didn’t see any sorts of errors.

You can download older versions of Raspbian here:

> **[Index of /raspbian/images](http://downloads.raspberrypi.org/raspbian/images/)**

---

<div class="post-metadata">

**Author:** ![ThomDietrich](https://community.openhab.org/user_avatar/community.openhab.org/thomdietrich/32/71239_2.png) [@ThomDietrich](https://community.openhab.org/u/ThomDietrich)\
**Post date:** [September 3, 2017, 10:30pm UTC](https://community.openhab.org/t/oh2-mqtt-commands/33734/14 "2017-09-03T22:30:02Z")

</div>

Hello @xSERGIOx,  
the mosquitto error is one of the issues why we didn’t yet move the openHABian image to stretch (which we will do very soon). However changes introduced in stretch are pretty much unimportant for openHAB (bluetooth audio improvements might be interesting). Please try again with the latest openHABian image.

Regarding the problems, I believe @rlkoshak has mentioned all that’s needed.

1. Set up openHAB
2. Set up an MQTT broker
3. Check the broker is working, using a desktop client
4. Install and connect the MQTT binding
5. Bind a simple item to an MQTT topic, once again check the functionality (desktop client)
6. Continue

Could you please summarize once again where you are having problems?

---

<div class="post-metadata">

**Author:** ![xSERGIOx](https://community.openhab.org/letter_avatar_proxy/v4/letter/x/858c86/32.png) [@xSERGIOx](https://community.openhab.org/u/xSERGIOx)\
**Post date:** [September 5, 2017, 12:03am UTC](https://community.openhab.org/t/oh2-mqtt-commands/33734/15 "2017-09-05T00:03:07Z")

</div>

Hi  
Started again from scratch and followed Rich advise and installed Raspbian Jessie…  
I’m stuck at the same point as before…  
The mqtt binding is installed, mosquitto is working and openhab don’t get it…  
I’m trying to do something simple such as get and set a relay status and get a sensor value such temperature  
default.items  
Switch ArduinoGarageRelay1 “Grupo Tomadas Parede” {mqtt=“\>[mosquitto:mikasa/garagem/relay1:command:ON:1],\>[mosquitto:mikasa/garagem/relay1:command:OFF:0]”}  
Number ArduinoGarageThermoresistor “Temperatura [%.1f C]” {mqtt=“\<[mosquitto:mikasa/garagem/temperatura:state:default]”}  
mqtt.cfg  
mosquitto.url=tcp://localhost:1883  
mosquitto.user=myuser  
mosquitto.pwd=mypass  
mosquitto.qos=1  
mosquitto.retain=true  
mosquitto.async=false

I’m plublishing mqtt messages using the terminal at the RPi and from windows mqtt.fx and each one receibes one another, but openhab dont.  
I also notice that Basic UI at the Rpi is not updating values (need to manually refresh), unlike the Paper UI, and at Android (that uses the same Basic UI) does auto refresh… Is this how its supposed to be?

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [September 5, 2017, 8:23pm UTC](https://community.openhab.org/t/oh2-mqtt-commands/33734/16 "2017-09-05T20:23:02Z")

</div>

Please use code fences to format your Item and cfg text. It is soo much easier to read when you do:

````
```
code goes here
```

````

Are there errors in the log? If you are not publishing something that can be parsed into a Number (e.g. there is white space before or after the digits) OH won’t be able to convert the text into a Number. These will generate error logs.

You should at least see a “Starting MQTT broker connection ‘mosquitto’” in your logs at OH startup if everything is OK. That will narrow the problem down to one of mqtt.cfg or your Items.

Do you have the MQTT Action installed? If so uninstall and reinstall it. This is a known issue and reinstalling the Action fixes it.

I can’t say anything about autorefresh on the Pi, but perhaps the browser on the Pi doens’t support as fast of an autorefresh. All other things being equal, the entries in BasicUI should refresh within a few seconds of the change, usually a lot faster.

---

<div class="post-metadata">

**Author:** ![xSERGIOx](https://community.openhab.org/letter_avatar_proxy/v4/letter/x/858c86/32.png) [@xSERGIOx](https://community.openhab.org/u/xSERGIOx)\
**Post date:** [September 7, 2017, 5:57pm UTC](https://community.openhab.org/t/oh2-mqtt-commands/33734/17 "2017-09-07T17:57:43Z")

</div>

Maybe the log file have some clue about the problem.

> [INFO] [penhab.io.transport.mqtt.MqttService] - MQTT Service initialization completed.  
> 2017-09-07 18:44:43.003 [INFO] [t.mqtt.internal.MqttBrokerConnection] - Starting MQTT broker connection ‘mosquitto’  
> 2017-09-07 18:44:47.505 [WARN] [ore.internal.events.OSGiEventManager] - Dispatching event to subscriber ‘org.eclipse.smarthome.core.thing.link.ThingLinkManager@6f3567’ takes more than 5000ms.  
> 2017-09-07 18:50:17.477 [WARN] [ore.internal.events.OSGiEventManager] - Dispatching event to subscriber ‘org.eclipse.smarthome.io.monitor.internal.EventLogger@f2b96e’ takes more than 5000ms.  
> 2017-09-07 18:50:17.659 [WARN] [ore.internal.events.OSGiEventManager] - Dispatching event to subscriber ‘org.eclipse.smarthome.io.monitor.internal.EventLogger@f2b96e’ takes more than 5000ms.

so the service starts but it cant get events from it, I think.

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [September 7, 2017, 6:07pm UTC](https://community.openhab.org/t/oh2-mqtt-commands/33734/18 "2017-09-07T18:07:50Z")

</div>

At this point, I recommend reading and following the following:

> [@MQTT Binding (v1.11) Getting Started 101](https://community.openhab.org/t/mqtt-binding-v1-11-getting-started-101/33958):
>
> After countless threads regarding initial configuration issues, here is a short guide on how to get started with MQTT, the MQTT binding and openHAB. This thread is a wiki article and can be improved by everyone. Please do! [MQTT](http://mqtt.org) is a machine-to-machine (M2M)/“Internet of Things” connectivity protocol. It was designed as an extremely lightweight publish/subscribe messaging transport. A few resources first: Read all you ever need to know about MQTT in the [MQTT Essentials](http://www.hivemq.com/blog/mqtt-essentials/) The openHAB MQT…
