MQTT binding retained messages

Remove the retain checkmark in your broker and/or device Thing configuration.

how can i do that through text configuration?

No idea, never used that complicated stuff :innocent:

retain is a property usually set per topic.
The Action does not appear to offer any option for retain or not.

The broker bridge thing does have a setting

  • retainMessages : Retain messages. Defaults to false.

IMO setting retain per connection is wrong and a bug.

Can you clarify “per connection”? As I understand the MQTT side of the mechanism, it is actually flagged per message.

It seems like it is the publisher’s internal business if it opts to publish all its messages with retain flag. I presumed this is what the openHAB broker thing option is for - note this is disabled by default.

But it also seems it is not possible to otherwise select retain or not in the Action, I think that is a shortcoming. We might be publishing a mix of events as well as states and not want them all retained.

There might be a workaround configuring two broker things, one with and one without retain. You could then choose in a rule Action which broker to have publish something. I don’t know if the binding will support that, two things pointing to the same real broker.

Thanks for weighing in on this Jan because the proper implementation of the retain bit has been up for debate. The developer (David G.) has lost interest and will do whatever the community decides.
here is thread for reference

uhh, the UNDEF behaviour has little to do with ‘retain’ except that when you do you use retain, UNDEF is not an issue.

I agree. I suspect that is left over from MQTT 1.x which only allowed one to define it on a per connection basis.

But I believe the binding also supports setting the flag for outgoing topics on the Generic Thing.

I was planning on filing an issue on the Action though as we should be allowed to set the retained flag there as well.

Indeed, but the MQTT 1.x implementation only allowed one to set the flag per connection. I believe the MQTT 2.x binding also allows one to set the flag per connection (i.e. on the Broker Thing).

I believe that was added after the 2.4 release so it might not be available for those using OH 2.4.

The old MQTT 1.x approach was to set up two broker connections, one with the retained and one without. But I agree, the Action should allow us to set the flag when we call it.

As long as the two are using unique client IDs there is no problem.

So, how can i set the retain flag through text config?

I’ve no idea. As sihui said, I don’t use that complicated stuff either. In PaperUI there is a toggle under “Show more”.

I don’t like to mix config approaches for Things and since I use automatic discovery that means I manage all my Things through PaperUI and/or the REST API.

???

like that?

Bridge mqtt:broker:local [ host=“localhost”, secure=false, retainMessages=false ]

Since i did not set that it should default to false, thus not retain them anyway…Yet, it does retain them

I would have thought so, yes.

Perhaps that’s a bug in the Action then. How are you determining that a message is retained? You should use mqtt.fx or similar to see if the flag gets set on your message.

Just to be clear about the implications of the retained messages.

Once a retained message gets published to a topic, that message will remain on that topic as a retained message until it get’s replaced by another retained message. Sending new messages without the retained flag set will not clear the retained message. Thus, to remove a retained message, you need to send a new empty message with the retained bit set to true.

Have you cleared out the existing retained messages? If not then your tests might be getting the old messages.

yes, i cleared the data and double checked that.

i tested the retained state by reconnecting a client and observing it getting the last message on every connect…

Did you observe something different?

It‘s a little bit unexpected and IMO a bug, but the homie adding sets the retain flag for the used connection. Could be your problem.

To fix this we need some additions in core but I‘ll not add these before the paho library has been removed (would create quite some merge conflicts).

So, would adding a new broker help? Or does homie overwrite all brokers?

Maybe simply adding a reteined-parameter in the MQTT action would be sufficient…

Here is my proposal: https://github.com/openhab/openhab2-addons/pull/5998

1 Like

merged.

2 Likes