Weird behavior from MQTT Broker

Hey All,

Please i need a help with a very weird issue i am facing with the MQTT broker in Openhabian 2.3. Everything was working fine until 2 days ago when i noticed that the relays modules (ESP8266) that i am controlling from MQTT is receiving a commands immediately once it’s connected to the Broker.

In another way if i turned Off the relay module (ESP8266) and turn it On again i can see on the Serial interface log of this module that it receives an On commands once the module is connected to the Broker.

For troubleshooting and for testing purposes I activated an MQTT Broker on my Phone and change the Broker of the ESP8266 to My Phone Broker, everything works fine. So strictly the problem is from the OH MQTT Broker. Noting that in Openhab frontail logs i don’t see any commands sent to the relays module.

I am not sure of from the MQTT retain it was true then i turn it to false but nothing changed, or something else.

And one more hint is if i send an Off commands to the relays from OH it’s well received from relay module and they are Off, but once i restart the relay module it turned On again upon the commands received from OH Broker.

Please help!!


Or is there a Last Will?
Can you post the protokoll please.

Hello Harry,

Thanks for your reply, but i didn’t get your questions.

The protocol is MQTT on port 1883.

Are you using mosquitto?
Can you publish your mqtt.cfg file please?
Thanks

Yes Mosquito,

i tried to change the qos to 0 (which is already default) without success.the main problem is when a client subscribed to the Broker, the broker send immediately a command by itself. I read the MQTT essentials am not sure if something related to clean session or the broker is not receiving the ACK from the client that he received the command. but i am facing this issue with all the MQTT subscribers not only one.

and here is below the mqtt.config

# URL to the MQTT broker, e.g. tcp://localhost:1883 or ssl://localhost:8883
mqttoh.url=tcp://localhost:1883

# Optional. Client id (max 23 chars) to use when connecting to the broker.
# If not provided a random default is generated.
#<broker>.clientId=<clientId>

# Optional. True or false. If set to true, allows the use of clientId values
# up to 65535 characters long. Defaults to false.
# NOTE: clientId values longer than 23 characters may not be supported by all
# MQTT servers. Check the server documentation.
#<broker>.allowLongerClientIds=false

# Optional. User id to authenticate with the broker.
#<broker>.user=<user>

# Optional. Password to authenticate with the broker.
#<broker>.pwd=<password>

# Optional. Set the quality of service level for sending messages to this broker.
# Possible values are 0 (Deliver at most once),1 (Deliver at least once) or 2
# (Deliver exactly once). Defaults to 0.
mqttoh.qos=0

# Optional. True or false. Defines if the broker should retain the messages sent to
# it. Defaults to false.
mqttoh.retain=false

# Optional. True or false. Defines if messages are published asynchronously or
# synchronously. Defaults to true.
#<broker>.async=<async>

# Optional. Defines the last will and testament that is sent when this client goes offline
# Format: topic:message:qos:retained <br/>
#<broker>.lwt=<last will definition>

Thank you guys in adavnce for your help.

Can you publish your mosquitto.conf, please?

/etc/mosquitto/mosquitto.conf I think

Ok, and i never knew about this file before. so nover touched it.

and here is it:

# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example

pid_file /var/run/mosquitto.pid

persistence true
persistence_location /var/lib/mosquitto/

log_dest file /var/log/mosquitto/mosquitto.log

include_dir /etc/mosquitto/conf.d

persistence false

restart mosquitto

Still same,

I am trying to uninstall Mosquito then reinstall it. actually this is not the first time that this problem occurred with me. 6 months ago it happened and I re flashed the SD card with Openhabian 2.2, the everything worked normally. and i said that maybe because my tries and fails and i was new to OH something wrong happens. but now i didn’t change anything and the problem occur again. Hopefully with the re installation of Mosquito only the problem will be solved.

Will post an update once done.

Problem solved by removing the Mosquito service and reinstalling it again.

And I think the main issue with the Retain feature once enabled it can do such problems, specially when it comes to control a relay or whatever which device from the OH via MQTT and from a local button connected directly to the inout of an ESP8266 for example.

So when OH send an ON message for example to the relay the Broker keeps retaining this message ON, if i turn Off the relay locally from a button connected directly to ESP8266. When the ESP8266 restart and resubscribe to the Broker, the Broker send the last command sent already which is On while i turned the relay Off from the button but the Broker didn’t know about that last command.

In my case better to disable the Retain feature from the beginning cause in my previous configuration i enabled it 2 days ago thinking that i am adding a good thing to my configuration. Good to learn anyway !!

Hope my problem help other people.

Thanks

If you send a message to the same topic that is an empty message and does not include the retain flag that will clear it the retained message.

Search Google for “how to remove retained message Mosquitto”.

1 Like

I beg to differ: the NULL message should also be retained or it won’t clear it permanently.

mosquitto_pub -t topic -n -r

Next time: you do not have to remove the broker and re-install it. Search for the topic you want to clear and then simply clear that one:

mosquitto_pub -t topic/name -n -r

It can actually be good practice to have messages such as the one you had problem with retained. Assume you swtich something ON by publishing a retained 1 or on or whatever. When the device powers up after, say, a power failure, it can subscribe to the topic to determine the last known good state and, say, switch itself on again.

It depends what you’re trying to accomplish.

Correct, the empty message should also have the retained bit set. I misspoke.

By default, every MQTT topic can have a retained message. The standard MQTT mechanism to clean up retained messages is sending a retained message with an empty payload to a topic. This will remove the retained message.
https://www.hivemq.com/blog/delete-mqtt-retained-messages-subtopics-recursively

Delete a retained message
There is also a very simple way for deleting a retained message on a topic: Just send a retained message with a zero byte payload on that topic where the previous retained message should be deleted. The broker deletes the retained message and all new subscribers won’t get a retained message for that topic anymore. Often deleting is not necessary, because each new retained message will overwrite the last one.
https://www.hivemq.com/blog/mqtt-essentials-part-8-retained-messages

Many thanks for the replies,

However i need to ask if the MQTT-Retain feature is set to false in mqtt.cfg file. is it possible to clear the retain message? or i need to enable the retain feature then clear retained messages?

Thank you,

no :slight_smile:

Noted, thanks.