MQTT: works in the shell but not in openhab

Hi there,

I’m trying to get a LED on a LED on my ESP8266 with MQTT on my raspberry pi with OH2.2.0. I installed it with apt-get and configured it in the shell. On the ESP there’s a program which turns the LED on when the MQTT gets

mosquitto_pub -t "/test/light1" -m „1“
or to turn off
mosquitto_pub -t "/test/light1" -m "0"
in the shell. This works.

Now, I’d like to implement the control to Openhab. I installed the MQTT Binding in PaperUI. What I’ve tried:

.items

Switch MQTT_Switch_Command  "MQTT Command" {mqtt=">[mosquitto:test/light1:command:ON:1],>[mosquitto:test/light1:command:OFF:0]“}
String MQTT_Switch_State    "MQTT State [%s]" {mqtt="<[mosquitto:test/light1:state:default]"}

the .cfg:

broker.url=tcp://xxx.xxx.x.xxx:1883 (x replaced by my ip)
mosquitto.user=domeninini
mosquitto.pwd=******** 

The String should show the status of the LED, but neighter the LED nor the status item work.

Have you an idea where I made a mistake ? Thanks !

@domeninini

maybe this should be

mosquitto.url=...
1 Like

@Josar Thank you for the answer. I’ve tried your advise but sadly it still does not work. Neighter the status in the sitemap, nor the LED.

Did you reboot?
What is in the log?

Not sure but is there a / missing?

Your topic starts with a / but you’ve not put it in your item configuration.

Switch MQTT_Switch_Command  "MQTT Command" {mqtt=">[mosquitto:/test/light1:command:ON:1],>[mosquitto:test/light1:command:OFF:0]"}
String MQTT_Switch    "MQTT State [%s]" {mqtt="<[mosquitto:/test/light1:state:default]"}

@Confused Thank you, too, this is absolutely true. I’ve tried this now, but no one works:

Switch 1a "Switch 1" {mqtt=">[mosquitto:/test/light1:command:on:1],>[mosquitto:/test/light1:command:on:0]"}
String 1s"Sta1 [%s]" {mqtt="<[mosquitto:/test/light1:state:default]"}
Switch 2a "Switch 2" (all) {mqtt=">[mosquitto:/test/light1:command:on:default],>[mosquitto:/test/light1:command:off:default],<[mosquitto:/test/light1:state:state:default]"}
Switch 3a "Switch 3" (all) {mqtt=">[mosquitto:/test/light1:command:*:default],<[mosquitto:/test/light1:state:state:default]"}
Switch 4a "Switch 1" (all) {mqtt=">[mosquitto:testsw/1:command:*:ON:1],<[mosquitto:testsw/1:state:OFF:0]"}

In the shell:

Bildschirmfoto 2018-02-19 um 19.12.30

Edit: and should’n I see the “answer” when opening in the shell

mosquitto_sub -t "/test/light1 ?

This works with mosquitto_pub -t "/test/light1" -m "1"but not with openhab.

do a quick troubleshooting check:

verify the contents of /var/lib/openhab2/config/org/openhab/mqtt.config versus /etc/openhab2/services/mqtt.cfg

OH2MQTT.url.url="tcp://xxx.xxx.x.xxx:1883" (x replaced by my ip)
broker.url="tcp://xxx.xxx.x.xxx:1883" (x replaced by my ip)
mosquitto.clientId="openHAB2"
mosquitto.pwd="***********" (* replaced by my pw)
mosquitto.url="\=tcp://xxx.xxx.x.xxx:1883" (x replaced by my ip)
mosquitto.user="domeninini"
service.pid="org.openhab.mqtt"
/etc/openhab2/services/mqtt.cfg gives:
mosquitto.url==tcp://xxx.xxx.x.xxx:1883" (same ip)
mosquitto.clientId=openHAB2
mosquitto.user=domeninini
mosquitto.pwd=***********" (same pw)

Does this have to match ?

Yes, they should match. Are you sure, you pasted the contents correctly and you just put those xx’s in?

what’s wrong with your mqtt.cfg is the double == for the mosquitto.url, please delete one.
after that:

  1. stop OH2
  2. delete /var/lib/openhab2/config/org/openhab/mqtt.config
  3. restart OH2

then please compare the both files again - they should now match and oyur old configs like “OH2MQTT” and “broker” should be gone.

those xx’s are created here in the browser and they are not standing in the code :smiley:
Sure, let me check this.

Thank you @binderth ! This was the mistake :smiley: Thank you, too, @Dim, @Josar, @Confused and @nitingautam for your help.

For those who read this and have the same problem, here’s the summary:

.items

Switch 2a "Switch 2" (all) {mqtt=">[mosquitto:/test/light1:command:on:1],>[mosquitto:/test/light1:command:off:0],<[mosquitto:/test/light1:state:state:default]"}

.cfg

mosquitto.url=tcp://IP_FROM_THE_SERVER:1883
mosquitto.clientId=openHAB2
mosquitto.user=RANDOM_USERNAME
mosquitto.pwd=RANDOM_PASSW" 

With the command ON in the sitemap you can do this:
openhabian@pi:~$ mosquitto_pub -t “/test/light1” -m "1"
and with the command ON you can control this:
openhabian@pi:~$ mosquitto_pub -t “/test/light1” -m “0”

Hope this is helpful.

1 Like

@domeninini you should have a console opened with the log running side by side when editing files this helps to track down erros.

@Dim @Kai So basically there is a bug.

When the mqtt.cfg is changed the connection is restarted but i assume not with the changed value.
When i enter a wrong brocker address i get the errror when restarting. When i change the broker url the error comes againb besides that it is correct then.
The /var/lib/openhab2/config/org/openhab/mqtt.config gets updated.

And it is not very helpfull, maybe extending the massage to report what client, port, topic, etc was used would help track down such problems.

20:52:37.456 [INFO ] [rt.mqtt.internal.MqttBrokerConnection] - Starting MQTT broker connection 'mymosquitto'
20:52:37.482 [ERROR] [openhab.io.transport.mqtt.MqttService] - Error starting broker connection
org.eclipse.paho.client.mqttv3.MqttException: MqttException
        at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:38) [205:org.openhab.io.transport.mqtt:1.11.0]
        at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:664) [205:org.openhab.io.transport.mqtt:1.11.0]
        at java.lang.Thread.run(Thread.java:745) [?:?]
Caused by: java.net.UnknownHostException: Nolocalhost
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184) ~[?:?]
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[?:?]
        at java.net.Socket.connect(Socket.java:589) ~[?:?]
        at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:70) ~[?:?]
        at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:650) ~[?:?]
        ... 1 more

@kai as always i don´t know where to place this issue, openhab1-addons, openhab2-addons, smarthome?

As this seems to be an issue with the MQTT 1.x binding, I’d think it should be filed under https://github.com/openhab/openhab1-addons/issues.

Hey there, I am migrating to my new raspi 4 and I have a similar problem.
Mosquitto is working in the shell, but nothing happens, when toggling a switch in BasicUI. The output of “cat /etc/openhab2/services/mqtt.cfg” is:

mosquitto.url=tcp://192.168.1.187:1883
mosquitto.clientId=openHAB2
mosquitto.user=domeninini
mosquitto.pwd=********
mqtt:broker.retain=true
mqtt:broker.async=false

and the content of “/var/lib/openhab2/config/org/openhab/mqtt.config” is


broker.async="false"
broker.retain="true"
mosquitto.clientId="openHAB2"
mosquitto.pwd="*********"
mosquitto.url="tcp://192.168.1.187:1883"
mosquitto.user="domeninini"
service.pid="org.openhab.mqtt"

MQTT Binding is installed, then a new thing is added manually (MQTT Broker) with the following features:


My openhab version is 2.4.0-1.
What else could cause the problem ?

But which one?
MQTT version 1.x uses the mqtt.cfg file.
MQTT version 2.x does not. It uses things instead.
You seem to be using both methods.

It think you are right ! On my old pi, I used the .cfg file and no things file. This works find and I don’t see a reason why to upgrade to v2. (What would be the advantage ? )
So, I’d like to use the old v1. Now, I activated the Include 1.x Bindings Legacy and installed the the v1 Binding and see there - it works ! Thank you ! I did not realised that there is a new version.