MQTT Cheat Sheet

MQTT Cheat Sheet

As the title suggests, a bunch of MQTT commands that will come to your rescue when needed.

MQTT Setup

By default, Home Assistant comes with a built-in MQTT server. If you want to install MQTT (either on a different Raspberry Pi or on the same server), follow the steps below.


sudo apt-key add mosquitto-repo.gpg.key

cd /etc/apt/sources.list.d/

sudo wget

Installation of Mosquitto requires root privileges.

sudo -i	

The above command is not mandatory, it is if you wish to use root or you will need to prefix each below command with sudo. For e.g. sudo apt-get update

First, update all the source list

apt-get update
apt-get upgrade

After updating the source list, you are ready to install Mosquitto

apt-get install mosquito

Mosquitto is controlled in two ways. First, the default configuration is in /etc/mosquitto/mosquitto.conf. It is recommended not edit this file. Instead, make a copy of it with a .conf extension in /etc/mosquitto/conf.d folder. For e.g., if you create a file called mosquitto.conf, the full path to the local configuration file would be /etc/mosquitto/conf.d/mosquitto.conf.

Add the following in the mosquitto.conf file:

user mosquitto
max_queued_messages 200
message_size_limit 0
allow_zero_length_clientid true
allow_duplicate_messages false

listener 1883
autosave_interval 900
autosave_on_changes false
persistence true
persistence_file mosquitto.db
allow_anonymous true
password_file /etc/mosquitto/passwd

After updating the file, it is required to restart mosquitto. Run the following command to restart the mosquitto.

sudo systemctl restart mosquitto

You are required to provide a password for mosquitto. To create a password for mosquitto, run the following command

sudo mosquitto_passwd -c /etc/mosquitto/passwd <username>

You are required to restart mosquitto to refresh the settings.

sudo systemctl restart mosquitto

To make a system service to run Mosquitto automatically on boot, create a file ‘mosquitto.service’ in /etc/system/system folder.

sudo vi /etc/systemd/system/mosquitto.service

And, enter the following in the file and save.


Description=Mosquitto MQTT Broker daemon

ExecStart=/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf -d
ExecReload=/bin/kill -HUP $MAINPID


Now enable the service, start it and look into the status:

sudo systemctl enable mosquitto.service
sudo systemctl start mosquitto.service
sudo systemctl status mosquitto.service -l

A successful start will show a green Mosquitto service. Note that the config and service definition are interlinked via the path of the PID file.

Bridging MQTT with CloudMQTT

To bride Local MQTT Server with CloudMqtt, use the following. The following will pull all the messages from cloudmqtt to local mqtt server. Not the other way around.

Edit the /etc/mosquitto/mosquitto.conf file and add the following content

Just make sure you replace XXX_*_XXX with your details

# 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/

persistence true
persistence_location /var/lib/mosquitto/

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

include_dir /etc/mosquitto/conf.d

connection cloudmqtt
start_type automatic
#bridge_cafile /etc/ssl/certs/ca-certificates.crt
try_private true
bridge_attempt_unsubscribe true
cleansession true

topic # in 0

Publish a message to MQTT

mosquitto_pub -h -t home/bedroom/switch1 -m "ON"

Read more on publishing messages

Read messages on a topic

mosquitto_sub -v -t 'test/topic'


mosquitto_sub -h -t topic

To delete all topics in MQTT:

mosquitto_sub -t '#' -v -u USERNAME -P PASSWORD| while read line _; do mosquitto_pub -t $line -r -n -u USERNAME -P PASSWORD; done

To remove a specific topic

mosquitto_pub -t 'TOPIC' -r -n -u USERNAME -P PASSWORD


Remove an MQTT Topic Permanently from MQTT


Read more on subscribing to messages

comments powered by Disqus