OH2 MQTT: "Client OpenHAB2 already connected, closing old connection."

I am using OH2b4 with the MQTT binding. I have discovered two issues with MQTT:

  1. OH2 wants to create two connections to MQTT broker although I have only defined one. In services/mqtt.cfg I defined a broker called “home”. Everything worked fine so far. Later on I decided to rename it in the cfg-file to “homebroker”. Since then OH2 always wants to connect to two brokers “home” and “homebroker”. I don’t know where it still takes the old name “home” from - is it saved in some kind of internal database and how do I get it removed? I have restarted OH2 several times now and it still uses the old and the new name of the broker in parallel.
  2. Maybe this is linked to the first issue: OH2 gets disconnected form my MQTT broker (mosquitto) frequently and mosquitto.log says
1474751407: New client connected from 192.168.x.y as OpenHAB2 (c1, k60).
1474751417: New connection from 192.168.x.y on port 1883.
1474751417: Client OpenHAB2 already connected, closing old connection.
1474751417: Client OpenHAB2 disconnected.

At the same time openhab2.log says:

2016-09-24 23:11:37.905 [INFO ] [t.mqtt.internal.MqttBrokerConnection] - Starting connection helper to periodically try restore connection to broker 'home'
2016-09-24 23:11:47.934 [INFO ] [t.mqtt.internal.MqttBrokerConnection] - Starting MQTT broker connection 'home'
2016-09-24 23:11:47.951 [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)[193:org.openhab.io.transport.mqtt:1.9.0.b4]
        at java.lang.Thread.run(Thread.java:745)[:1.8.0_101]
Caused by: java.io.EOFException
        at java.io.DataInputStream.readByte(DataInputStream.java:267)[:1.8.0_101]
        at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:56)[193:org.openhab.io.transport.mqtt:1.9.0.b4]
        at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:100)[193:org.openhab.io.transport.mqtt:1.9.0.b4]
        ... 1 more
2016-09-24 23:11:47.954 [ERROR] [t.mqtt.internal.MqttBrokerConnection] - MQTT connection to 'homebroker' was lost: Connection lost : ReasonCode 32109 : Cause : null
2016-09-24 23:11:47.955 [INFO ] [t.mqtt.internal.MqttBrokerConnection] - Starting connection helper to periodically try restore connection to broker 'homebroker'
2016-09-24 23:11:57.974 [INFO ] [t.mqtt.internal.MqttBrokerConnection] - Starting MQTT broker connection 'homebroker'
2016-09-24 23:11:57.991 [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)[193:org.openhab.io.transport.mqtt:1.9.0.b4]
        at java.lang.Thread.run(Thread.java:745)[:1.8.0_101]
Caused by: java.io.EOFException
        at java.io.DataInputStream.readByte(DataInputStream.java:267)[:1.8.0_101]
        at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:56)[193:org.openhab.io.transport.mqtt:1.9.0.b4]
        at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:100)[193:org.openhab.io.transport.mqtt:1.9.0.b4]
        ... 1 more
2016-09-24 23:11:57.993 [ERROR] [t.mqtt.internal.MqttBrokerConnection] - MQTT connection to 'home' was lost: Connection lost : ReasonCode 32109 : Cause : null
2016-09-24 23:11:57.993 [INFO ] [t.mqtt.internal.MqttBrokerConnection] - Starting connection helper to periodically try restore connection to broker 'home'

I can connect fine to the broker using node-red and weewx (both on port 1883) as well as the HiveMQ websocket client in the browser on another port. I assume it’s a problem with OH2 / MQTT-binding. Any ideas how to solve this? I would like to use MQTT for loose coupling of some components such as OH2, weewx and node-red.

Sorry for not answering your question (btw, I have the same problem on openHAB1 with old broker messages), but I’m interested in the combination of weewx and openHAB via mqtt:
I’m looking for a way to gather my weather data (published to a mqtt broker from an esp8266) and send them to weewx to further publish them to public weather services.
I know there is a weewx plugin to SEND data to an mqtt broker, but I would like to RECEIVE data from a broker into weewx.
So if you ever come across a solution for that or know a way to do that, please chime in.

How did you configure your broker? There is a config option <broker>.retain=<retain>
It defaults to faulse, but if you set that to true the broker will retain old messages …

I do not have a way to solve the problem but can confirm that if you have two clients attempt to connect to a Broker with the same client ID, the Broker will kick the old one off. So this almost certainly explains your current behavior. If you change the client ID OH uses to connect in mqtt.cfg for the homebroker, assuming it picks up the changes, this should at least mitigate the problem and let OH stay connected.

A reboot should have cleared up the old connection config. It is odd that it is still hanging around.

Hi Rich,
many thanks for the hint. I totally overlooked this workaround. I changed the clientID and at least the problem does not show up any longer.

Hi sihui,
I currently have not set the broker to retain messages. Everything runs on a system with very limited resources (diskstation DS414). So I try to minimize the resources needed by every single component. I am relatively new to smart home implementations and from my current point of view retain isn’t necessary. But if you have a scenario where retain helps in a smart home environment please let me know as it helps me to broaden my view on this.