Scroll down for more guides

Part 2 · Communication and Technology 6 min read

Sensors publish to an MQTT broker, which passes the messages to a phone, a light and a chart

The Lightweight Messaging System Behind Many Automation Projects

When you start building more advanced home automation – especially with devices from different manufacturers or with your own electronics – you will soon come across MQTT.

MQTT is a simple and efficient way for devices and software to exchange messages. It is used in industry, in vehicles and in the Internet of Things, and it has become one of the most popular "common languages" in home automation.

This guide explains how MQTT works, the most important terms, and how it is used in practice.

1. What Is MQTT?

MQTT is a messaging protocol designed for devices with limited processing power and for networks that may be slow or unreliable. The name originally stood for "MQ Telemetry Transport". It is an open standard published by OASIS, and version 3.1.1 is also an international ISO/IEC standard.

MQTT does not describe what a light or a sensor is. It only transports messages. What the messages contain – a temperature, the word "ON", or a block of structured data – is agreed between the devices and the software that use them.

This makes MQTT very flexible: a temperature sensor built with an ESP32, a Zigbee network, a home automation server and a mobile dashboard can all exchange information through the same MQTT system.

2. Publish and Subscribe

MQTT uses a model called publish/subscribe. Devices do not send messages directly to each other. Instead, all messages pass through a central server called the broker.

  • A device that has information publishes a message to a topic, for example: "the living room temperature is 21.4".
  • Devices or programs that need that information subscribe to the topic.
  • The broker receives every published message and passes it on to everyone who has subscribed to that topic.

The sender does not need to know who receives the message, or how many receivers there are. A new display or a logging program can be added at any time simply by subscribing – nothing needs to be changed in the sensor.

3. The Broker

The broker is the heart of every MQTT system. It is a program that runs on a computer in the network, often the same computer as the home automation server. Mosquitto is a widely used open-source broker, and Home Assistant can install it with a few clicks.

All devices connect to the broker, normally on network port 1883, or 8883 when the connection is encrypted.

Because every message passes through it, the broker must be reliable. If the broker stops, MQTT devices can no longer exchange messages until it is running again.

4. Topics

Every message is published to a topic – a text address made of levels separated by slashes, similar to a folder path:

  • home/livingroom/temperature
  • home/livingroom/humidity
  • home/garage/door/state
  • home/garden/pump/set

Topics do not need to be created in advance; they exist as soon as someone publishes to them. They are case-sensitive, so Home/Garage and home/garage are two different topics.

When subscribing, two wildcards can be used:

  • + replaces exactly one level: home/+/temperature receives the temperature from every room.
  • # replaces all remaining levels: home/garage/# receives everything from the garage.

A clear and consistent topic structure – for example location, then device, then value – makes a system much easier to understand and extend.

5. Messages and Commands

The content of a message is called the payload. It can be a simple value such as 21.4 or ON, or structured data in JSON format such as {"temperature": 21.4, "humidity": 48}.

A common convention is to use separate topics for the state and for commands:

  • The relay publishes its state to home/garden/pump/state.
  • The controller sends commands to home/garden/pump/set.
  • The relay switches, then publishes its new state, so the controller knows the command was carried out.

Many devices and programs follow their own topic conventions. Check the documentation of each device to find out which topics and payloads it uses.

6. Quality of Service

MQTT offers three levels of delivery guarantee, called Quality of Service (QoS):

LevelNameMeaningTypical Use
QoS 0At most onceThe message is sent once, without confirmationFrequent sensor values, where a missed value is soon replaced
QoS 1At least onceThe message is repeated until it is confirmed; it may arrive twiceCommands and important states
QoS 2Exactly onceA four-step exchange ensures one delivery onlyCases where duplicates must be avoided

Higher levels mean more network traffic. In home automation, QoS 0 and QoS 1 cover almost all needs.

7. Retained Messages and Last Will

Two features are particularly useful in home automation:

  • Retained messages – the broker stores the last message on a topic and sends it immediately to any new subscriber. A dashboard that starts up can then show the current state at once, instead of waiting for the next update.
  • Last Will and Testament – when a device connects, it can leave a message with the broker, such as "offline". If the device disappears without disconnecting properly, the broker publishes this message, so the rest of the system knows the device is unavailable.

Use retained messages for states, not for commands. A retained "switch on" command could be repeated unexpectedly every time a device reconnects.

8. MQTT in Practice

Some typical uses of MQTT in home automation:

  • Do-it-yourself devices – ESP32 and ESP8266 boards with firmware such as Tasmota or ESPHome, or your own program, can publish sensor values and receive commands.
  • Zigbee networks – software such as Zigbee2MQTT connects a Zigbee coordinator to an MQTT broker, so that Zigbee devices can be used by any system that understands MQTT.
  • Home automation servers – Home Assistant, openHAB, Node-RED and similar software can read and send MQTT messages. Home Assistant can also discover devices automatically when they publish their description in a specific format.
  • Monitoring – energy meters, heat pumps and solar inverters can often be connected through MQTT for logging and graphs.

9. Security

An MQTT broker can control equipment in your home, so it must be protected:

  • Require a user name and password for every device and program.
  • Give each device its own account where possible, and limit which topics it may use.
  • Use encrypted connections (TLS) when messages travel outside the home network.
  • Do not expose the broker directly to the internet. Use a VPN or a secure remote access solution instead.
  • Keep the broker software up to date.

Guide 33 covers security and privacy in more detail.

Final Thoughts

MQTT is a simple, lightweight and reliable way for devices and software to exchange messages. A broker passes messages between publishers and subscribers, organised by topics.

Features such as quality of service, retained messages and last will make it well suited to home automation, especially when devices from different sources need to work together.

With a clear topic structure and good security, MQTT can become the common backbone that connects sensors, controllers and your own electronic projects.

MISOLIMA® – Technology Made Practical.

Practical knowledge for understanding, designing and building home automation.