How to republish a state to MQTT every few seconds

Hello again,

I am using a Raspberry Pi to control a gate & garage door and monitor their open/closed state. This state is then published to MQTT when something changes so I can see on my phone the state of the doors. So far, this all works but if the MQTT client on my phone is offline any changes are missed - I open the app later it says still whatever the last received state was. I am not sure if the Mosquitto MQTT broker has a function that can repeatedly republish the current state of a topic, but another solution is to poll the Pi GPIO and have Node Red publish every 10s or so.

Can anyone help me find the elegant solution to this problem? I can't be the first to run into this, but my Google-fu has let me down so far. Thanks in advance.

The correct answer here is you don't.

You publish the state once with the retain bit set to true, this message will then always be delivered to a client when it reconnects and subscribes to the relevant topic

3 Likes

You like to see the current (last published) message and not the history?
Make sure you send your message with the retain flag true AND QoS>0 (unless you tweak the config) qos=0 are really „fire and forget“ so if you don’t listen you will miss that message

As far as I know it works like this

If a client connects with a new client id (or a timed out client id) or configured as clean connected it will get the last message the broker received. If the client is known and it will receive the queued messages.

Use mqtt Explorer and check if the retained flag is set after reconnect. Note: Messages with the retained flag set but received „live“ don‘t carry the retained flag to a client even if they will be stored as a retained message!

So no need to pull and push data frequently- mqtt is especially designed for unreliable connections.

This is only white belt level stuff :martial_arts_uniform: Start with the documentation of what you are using, look for clarity of features and then try key words from there if you still need more info (EG. MQTT, Quality of Service, Retained Messages). Carry on :cricket: :slight_smile:

Read through this MQTT Essentials tutorial. It will tell you virtually all you need to know about MQTT. MQTT Essentials - All Core Concepts explained

Thank you everyone - with hindsight I should have asked more about the QoS parameters that I have always just skipped over. Having this functionality built into the broker is a lot more elegant than polling. I have read the docs and chosen QoS 1 for these topics.

Have a great day.

BTW - what MQTT client are you using on your phone? And is phone IoS or Android?

You also need to set the topic as Retained.

@cymplecy - I am using a Samsung S20 with the MQTT Dash client app - happy for any recommendations of a better app. Both client and Node Red are using QoS 1 and the Retained flag is set to True. From my initial testing the results seem inconclusive but my day job is getting in the way of a serious look at the moment.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.