Openhab, MQTT April 2017

This forum has lots of out of date information and I have a problem:

MQTT is working. OpenHAB is running; OpenHAB does not talk to MQTT.

So, in an effort to provide adequate context, please bear with me.

Hardware:

  • RPI2, fixed IP 192.168.1.201
  • Sonoff flashed with Ahrendts-Tasmota firmware, fixed IP 192.168.1.50

Software:

  • OpenHAB2 V2.0…0-1 installed via Openhabian
  • Mosquitto installed using /opt/openhabian/openhabian-setup.sh (Version 1.4.11)
  • MQTTLens (on PC) (and/or Mosquitto_pub / sub in terminal window)

Configuration:

  • MQTT Binding and Actions installed using Paper UI of OpenHAB instance.
  • MQTT config file: smb://192.168.1.201/openHAB-conf/services/mqtt.cfg looks like this:
<broker>.url=tcp://localhost:1883
<broker>.clientId=openhab2
  • file at smb://192.168.1.201/openHAB-conf/items/demo.items includes this line:
Switch FrontLoungeMedia 	"Front Lounge Media" (LR,gLight) { mqtt=">[localhost:cmnd/FrontLoungeMedia/power:command:*:default], <[localhost:stat/FrontLoungeMedia/POWER:state:default]" }

Testing:

I can send the following command:

mosquitto_pub -h localhost -p 1883 -t ‘cmnd/FrontLoungeMedia/power’ -m ‘OFF’

and get the following response on topic ‘stat/FrontLoungeMedia/POWER’, the message OFF.

Also, the device does what it is told.

  • I can use MQTTLens to subscribe to all topics (topic #), but don’t see any communication between openHAB and MQTT no matter what I do.

  • The file /var/log/openhab2/events.log on the RPi does not include any mention of MQTT messages.

The file /var/log/openhab2/openhab.log includes the following error:

2017-04-21 13:18:21.344 [ERROR] [t.mqtt.internal.MqttBrokerConnection] - MQTT c$
Connection lost (32109) - java.io.EOFException
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsRecei$
at java.lang.Thread.run(Thread.java:745)[:1.8.0_121]
Caused by: java.io.EOFException
at java.io.DataInputStream.readByte(DataInputStream.java:267)[:1.8.0_12$
at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqt$
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsRecei$

Plea for help

So I am lost and confused, not sure what needs to be done from here, pretty confident that I have misinterpreted the instructions somewhere. Suggestions appreciated.

mqtt:mqtt-loc.url=tcp://192.168.1.2:1883
mqtt:mqtt-loc.clientId=openhab2

{mqtt=">[mqtt-loc:cmnd/FrontLoungeMed…

Hiya Sergey

I appreciate the effort. If you read https://community.openhab.org/users/KjetilA 's comment, it appears that there is an error in the information you provided - perhaps from older iterations of OpenHAB?

I did the following:

  1. Replaced<broker> with mqtt:mqtt-loc.url in the mqtt.conf file.
  2. Edit the items file and within that change the MQTT command to reflect those changes.

I chose not to change the IP address.

So we got one win in /var/log/events.log:

1493359108: mosquitto version 1.4.11 (build date Mon, 20 Feb 2017 22:47:27 +000$
1493359108: Config loaded from /etc/mosquitto/mosquitto.conf.
1493359108: Opening ipv4 listen socket on port 1883.
1493359108: Opening ipv4 listen socket on port 1883.
1493359110: New connection from 192.168.1.200 on port 1883.
1493359110: New client connected from 192.168.1.200 as lens_GcWKQNQMAeqRxTomkPR$
1493359150: New connection from 127.0.0.1 on port 1883.
1493359150: New client connected from 127.0.0.1 as openhab2 (c1, k60).
1493359150: New connection from 192.168.1.201 on port 1883.
1493359150: Client openhab2 already connected, closing old connection.
1493359150: Client openhab2 disconnected.
1493359150: New client connected from 192.168.1.201 as openhab2 (c1, k60).
1493359160: New connection from 127.0.0.1 on port 1883.
1493359160: Client openhab2 already connected, closing old connection.
1493359160: Client openhab2 disconnected.
1493359160: New client connected from 127.0.0.1 as openhab2 (c1, k60).
1493359170: New connection from 192.168.1.201 on port 1883.

But still nothing showing up on MQTTLens and no action from the configured switch.

Plea for help remains.

Since service configuration is moved to service-specific files in OpenHAB2, you no longer prefix each confguration/line with the sevice name.

Try the following in you mqtt.cfg file:

mqtt-loc.url=tcp://192.168.1.2:1883
mqtt-loc.clientId=openhab2
1 Like

Delete /var/lib/openhab2/config/org/openhab/mqtt.config and restart OH2 with the correct /etc/openhab2/services/mqtt.cfg in place (possible stale configs have been saved in the mqtt.config and this is causing troubles)

Here is a working example of a mqtt.cfg file. Use this to customize it to your system. Don’t use mqtt: in front of the broker name.

# URL to the MQTT broker
HomeR.url=tcp://localhost:1883
# Client id to use when connecting to the broker.
HomeR.clientId=openhab2

Make sure that the clientId is unique (don’t use the same Id for your Sonoff)
If you use authentication on your Mosquitto, set also the user and pwd settings within the /etc/openhab2/services/mqtt.cfg

Set within the karaf console:

log:set DEBUG org.openhab.binding.mqtt
log:set DEBUG org.openhab.io.transport.mqtt

(to turn logging down afterwards: log:set WARN ...)

and monitor the OH2 log file for related messages:

tail -f /var/log/openhab2/openhab.log

Additional troubleshooting step: Uninstall the MQTT Action Addon

Kjetil: That’s what I thought.

As per the opening comment, there remains a lot of old, bad info on these boards. I have made the change you suggested.

Dim

There was indeed an mqtt.config file in /var/lib/openhab2/config/org/openhab.
In addition, there was addons.config, dashboard.config, habpanel.config and ui folder.
I have deleted the mqtt.config file. Should I delete the others as well?

The Sonoff has a separate clientID (DVES_%06X) which I think is the default in the Tasmota firmware.

So having followed Dim and Kjetil’s advice, I can now see a huge number of Java errors where it appears OpenHAB is trying to contact org.eclipse.jetty and org.glassfish.jersey for dozens of different reasons, but there is still no evidence of communication with MQTT.

No! :slight_smile:
These (*.config) files are generated by OH2 processes and they are like the internal version of the configs in the files that you place in the /etc/openhab2/services/ directory. Normally, you shouldn’t change anything there but sometimes, if you make some wrong configs in *.cfg they remain in *.config.

Did you enable debug? No sign of MQTT stuff there?

Hi Dim.

Many thanks for the ongoing support. I finally worked out a way to test this (slow, I know):

  1. I turned Mosquito off using sudo systemctl stop mosquitto

I got a lot of complaints in the openhab log file which I was monitoring in a separate terminal window using your suggested command (tail -f /var/log/openhab2/openhab.log):

2017-04-30 11:34:20.264 [ERROR] [t.mqtt.internal.MqttBrokerConnection] - MQTT connection to broker was lost Connection lost (32109) – java.io.EOFException at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:138)[199:org.openhab.io.transport.mqtt:1.9.0] at java.lang.Thread.run(Thread.java:748)[:1.8.0_131] at java.lang.Thread.run(Thread.java:748)[:1.8.0_131] Caused by: java.io.EOFException at java.io.DataInputStream.readByte(DataInputStream.java:267)[:1.8.0_131] at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:56)[199:org.openhab.io.transport.mqtt:1.9.0] at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:100)[199:org.openhab.io.transport.mqtt:1.9.0]
  1. Then I turned it back on.

I saw:

2017-04-30 11:43:30.325 [DEBUG] [t.mqtt.internal.MqttBrokerConnection] - Starting message consumer for broker 'mqtt-loc' on topic 'stat/FrontLoungeMedia/POWER'

(Which is a status message from a Sonoff device on my network)

Hence I conclude that the binding is working.

Many thanks for your help gentlemen. I hope my longwinded responses help some other poor soul who has managed to overthink everything : )

1 Like

Nice thread! Thank you!

Still I am unsure how to set up MQTT. I would appreciate if the learnings could be documented the Tutorial section of the openHAB 2 Documentation.

Can this easily be done?

Greetings, Timo

Hi Timo

I am fresh from battling this issue on a Raspberry Pi using OpenHabian. I can’t help on other platforms as I am a complete noob, but perhaps I can offer some sort of help if that is what you need.

Cheers

1 Like