MQTT Retained Messages Tool-Python Script Examples

In this project we create simple script that will list topics that have a retained message and optionally deletes the retained message.

The script can be use from the command prompt by supplying options.

How it Works

The utility is designed to be run from the command line.

The first part of the script gets the command line input using getopt

The script requires only two parameters which are the broker IP or domain name and topic.

The script checks for a valid topic before continuing.

Now we initialize some flags using the Initialise_client_object function and the callbacks using the Initialise_clients function.

The script then tries to connect to the broker and uses a try except block to catch connection errors as explained in detail in understanding client connections.

Now we start a loop to process callbacks.

It then waits is loop for the connection to be acknowledged.

def on_connect(client, userdata, flags, rc):
if rc==0:
print("connected sending ",inputs["loops"]," messages ",\
inputs["loop_delay"]," second Intervals")
if rc==5:
print("broker requires authentication")

Notice the use of the flags that are set and reset in the on_connect callback.

while not client.connected_flag and not client.bad_connection_flag:

If the connection acknowledgement is bad the script exits if it is good it then it subscribes to a topic.

It then waits in a loop for the subscription to be acknowledged.

while not client.suback_flag: #wait for subscribe to be acknowledged

Now we subscribe to the topic and wait for the subscribe to be acknowledged

while not client.suback_flag: #wait for subscribe to be acknowledged
if sleep_count>40: #give up
print("Subscribe failure quitting")

We then wait a set period as all of the work is now being handled by the on_message callback.

The message.retained variable contains the retained flag which is 1 for a retained message.

If it is retained it stores the topic.

After the delay expires the script will print out the list of topics with retained messages and then if the clear flag is set it will clear the retained flag.

if inputs["clear_retained"]:
if len(retained_topics)>0:

This function clears the retained flag by publishing a empty message (msg=””) with the retained flag set.

def clear_retained(retained): #accepts single topic or list
if isinstance(retained[0],str):
for t in retained:
print ("Clearing retaind on ",msg,"topic -",t[0]," qos=",QOS0," retain=",RETAIN)
Print("problems with topic")
return -1


Run with the -h option to list options.

c:>python  -h

Note: the -c and -v options no longer require a parameter i.e use -c and not -c True



Create Retained Messages

For testing you may need to create some retained messages this script will do that.(

The script can be run from command prompt but it is easier to edit, and run in the IDE or from the command prompt with no options.


Download scripts here

Related Tutorials

Please rate? And use Comments to let me know more
[Total: 1    Average: 1/5]

Leave a Reply

Your email address will not be published. Required fields are marked *