OH 2.4 with MQTT - retained messages

As of OH 2.4 with mqtt: what needs to be configured in order to force OH to make use of retained messages? Tried it with


Bridge mqtt:broker:MQTT [ host="192.168.2.49", secure=false, username="XXX", password="XXX", clientID="openHAB2", retainMessages=true ]

but investigation using mqtt.fx turned out, there’s no retaining set.

Any suggestions?
Tried setting qos, but this didn’t work as well.

I don’t know for sure but I would correct the retained flag to have to be defined on the Publisher Thing, but the broker. That is the proper MQTT way. It is the one sending the message that determines whether the message gets restained.

Beyond that I’m no help. I don’t see it mentioned in the MQTT docs.

OK, I just went through the process of creating an MQTT broker Thing. There is a toggle for setting retained under advanced.

I’ve no idea how to do it in .things files.

Had a look at it in paperUI and discovered that retained is set. So as I didn’t get any error msgs and paperUI indicates it was set, the question is: why are the messages not affected by this setting? :slight_smile:

So the next question is how, in detail, are you confirming they are not being retained?

Yes, absolutely :slight_smile:

  1. I subsribed to several topics using mqtt.fx, which optically indicates if msgs are retained.
  2. Subsribed to a topic using mosquitto_sub. It ruled out there’s now retained msg left.
  3. I’m running Arduinos that most of the time sleep. When they wake up, they subscribe via MQTT in order to know what to do. This doesn’t work anymore.

@David_Graeff, any ideas why the retain flag would be set even though the flag is set in PaperUI?

In contrast to mqtt1, which offered a coarsed retain feature only (per configured broker), in mqtt2 you set the retain flag per channel. Please have a look at the documentation or paper ui for the configuration name.

If you set “blabla=true” it will also appear in paper UI. As Thing property. But that doesn’t mean anything. It is not used by a binding, it is just there :wink:

1 Like

The document has a retain configuration for both the channels and the broker, and I only see QOS for the broker. Is the documentation wrong for these?

https://www.openhab.org/addons/bindings/mqtt/ shows both qos and retrainMessages.
https://www.openhab.org/addons/bindings/mqtt.generic/ shows retained.

QOS is per broker connection, not per channel. That’s a design decision.

MQTT has it per publish and per subscribe, so you aren’t following the MQTT spec.

Can you explain your reasoning? Because a blanket QOS, like retain, may not make sense in all situations. If I’m reporting a temperature reading (for some reason I’m having OH publish this instead of consuming it) I don’t care if it gets delivered more than once or at all because I’ll be sending another update pretty soon so QOS makes sense. If I’m wanting to open my garage door, I want the message to be delivered exactly once with a confirmation so QOS 2 makes sense.

Setting the QOS for everything that OH publishes seems as limiting and coarse as the mqtt1 binding’s approach.

It appears the documentation is correct.

Meaning both the channel and broker have retain options? That seems odd.

The library we use (paho mqtt) allows a retain flag and qos per connection as well as per message.

Laziness :slight_smile:

This suggests QOS would be properly done for 2.x version:
https://groups.google.com/forum/#!topic/openhab/oOZNJfIEvkw

Then why can’t we do QOS per message?

David provided his answer above.

If you feel strongly about this I’m sure a PR would be welcome to add QOS on a per outgoing Channel basis.

I don’t know if it’s the right place to post this…I didn’t want to duplicate mqtt issues…I cannot make retained work either.

I tried to set up retained for the ShellyHumidity & Temperature sensor readings as it stays offline most of the time…All the configuration were made via PaperUI.

I tried to set the channels to retained and also to set a retained option on the device itself, but I don’t think it’s working.

As per broker documentation there should be a file called mqttembedded.bin somewhere, but I could not find it anywhere; I tried to change the broker configuration to a specified path /var/lib/openhab2/qttembedded.bin then I manually turned on the sensor to trigger it but the file does not appear, and if I try to connect with MQTT Explorer to the broker I don’t see any topic.

There are no evident error logs.

My openhab version is 2.4 and all the mqtt stuff (embedded broker, binding…etc) is the default shipped with this openhab version.

You may have a misunderstanding about how retained works. The retained flag gets set by the sender, not the receiver. So if your Shelly sensors are not reliable, you need to set the retained flag on the Shellys, not in openHAB.

The retained flag is only meaningful for publishing Channels.

I find that if my openhab service is restarted the state of my switches and contacts is not shown in ClassicUI until I operate them again.
Is this what the retain setting corrects or is there something else I have to do to “retain” those settings?

openHAB provides a restore-on-startup capability for general use. This is used in connection with a persistence service, often Mapdb.

Independently of that, it is possible for MQTT providers to request your broker to retain messages, which gives a similar effect of providing last known message when a subscriber like openHAB first connects.