MQTT gives devices a way to pass messages around without being directly connected to one another. An ESP32 can send a temperature reading to an MQTT broker. Home Assistant, a data logger or another device can then receive that reading by subscribing to the same topic.
This becomes useful once you have several devices or applications sharing data. For a straightforward ESPHome Wi-Fi sensor, though, the native Home Assistant API is often simpler. MQTT is not something you need to add just because you’re using an ESP32.
How MQTT works: publish, broker, subscribe
MQTT stands for Message Queuing Telemetry Transport. Despite the name, the easiest way to understand it is with three roles:
- Publisher: a device or service sending information, such as a sensor reporting temperature.
- Broker: the server receiving messages and delivering them to subscribers. Mosquitto is a popular broker.
- Subscriber: a device or program that requests messages on a topic, such as Home Assistant displaying the latest temperature.
The publisher does not have to know who will use the data. That separation is the reason MQTT works well in mixed DIY systems.
Example: sending a temperature reading

Suppose an ESP32 publishes the number 24.6 to the topic below:
home/office/temperatureThe message payload might simply be:
24.6Your MQTT broker receives that publication and forwards it to clients that subscribed to home/office/temperature. Home Assistant can create an entity from the topic, while a separate data logger could also subscribe to the same messages. The ESP32 does not need to send two independent reports.
| Component | Example | Job |
|---|---|---|
| Publisher | ESP32 temperature sensor | Sends 24.6 |
| Broker | Mosquitto | Relays messages to subscribers |
| Topic | home/office/temperature | Names the message channel |
| Subscriber | Home Assistant | Reads the value and displays it |
| Another subscriber | History logger | Stores it for analysis |
How MQTT topics are organised
A topic is a text-based path used to organise messages. MQTT uses forward slashes to create readable hierarchies, but it does not impose one universal naming scheme.
home/office/temperature
home/office/humidity
home/garage/temperature
home/garden/tank/levelKeeping topics consistent makes debugging and integrating new devices easier. I prefer names that describe the place, equipment and measurement, rather than opaque identifiers with no meaning.
MQTT topic wildcards
Subscribers can use wildcards. The single-level wildcard + matches one topic level. The multi-level wildcard # matches all remaining levels and must appear at the end of a subscription.
home/+/temperature # Matches office and garage temperatures
home/garden/# # Matches everything under home/gardenThese wildcard patterns are for subscriptions, not for naming published topics. Be cautious with broad wildcard subscriptions when many devices report frequently.
Where does the broker run?
A broker is the central message router. For a Home Assistant installation, Eclipse Mosquitto is an established option. It can run as the official Mosquitto Broker app in Home Assistant OS, on another local Linux host, or on a managed server. The simplest home setup is usually a private broker on the local network.
An MQTT broker is not the same as Home Assistant. Home Assistant connects to the broker as a client using its MQTT integration. Other devices and services also connect to the broker, publish data, or subscribe to messages.
What do QoS 0, 1 and 2 mean?
MQTT supports three message delivery levels. Higher QoS does not automatically mean the entire system is more reliable, and it adds overhead.
| QoS | Meaning | Practical interpretation |
|---|---|---|
| 0 | At most once | Fast and lightweight, but a message may be missed. |
| 1 | At least once | Retries delivery; a subscriber may see duplicate messages. |
| 2 | Exactly once (protocol delivery) | Stronger message-delivery semantics with more handshake overhead. |
For a sensor publishing every few minutes, QoS 0 is often enough. For an important discrete state change, QoS 1 may make sense if the application handles possible duplicates. QoS does not replace application-level checks for stale or incorrect data.
Retained messages and offline devices
Retained messages
When a message is published with the retain flag, the broker stores the most recent retained message for that topic and sends it to future subscribers. This is useful for a value such as the latest tank level or room temperature because Home Assistant can immediately display the last known reading after restarting.
However, a retained reading is not automatically a fresh reading. If a device disappeared days ago, its last temperature value could still be present. For monitoring systems, combine retained states with availability messages or timestamps.
Last Will and Testament (LWT)
An MQTT client can register a last-will message with the broker. If the connection closes unexpectedly, the broker can publish that message on the client’s behalf. A common pattern is to publish online on connection and configure the will as offline on an availability topic.
home/office/sensor1/availability
online
offlineThis can help Home Assistant distinguish an old retained reading from a currently connected device. The exact timing depends on keepalive and network conditions.
Set up MQTT in Home Assistant

Home Assistant supports MQTT through its official integration. The basic setup is to run a broker, connect Home Assistant to it, and configure the publishing device. Home Assistant’s MQTT documentation covers current menus and supported discovery methods.
1. Run a broker
- If you use Home Assistant OS, open the app store/add-on management area and install the official Mosquitto Broker app, or follow the MQTT integration flow to install it.
- Start the broker and check its logs for startup or authentication errors.
- Use broker credentials appropriate for each device or service. Do not expose anonymous MQTT access to the internet.
2. Connect Home Assistant
- In Home Assistant, open Settings → Devices & services and add MQTT if it has not already been discovered.
- Choose the broker connection and follow the on-screen authentication steps.
- Confirm the integration reports a successful connection before configuring individual sensors.
3. Send a test message
Use a local MQTT client, such as the Mosquitto command-line tools, to verify end-to-end delivery. On a computer with the tools installed, subscribe to one topic:
mosquitto_sub -h BROKER_IP -p 1883 -u USERNAME -P 'PASSWORD' -t 'home/test/message' -vIn another terminal, publish a sample value:
mosquitto_pub -h BROKER_IP -p 1883 -u USERNAME -P 'PASSWORD' -t 'home/test/message' -m 'hello'Replace the placeholders with your own local broker details. Commands shown with -P place the password in process arguments or shell history on some systems, so use a client configuration file or interactive credentials where practical. Port 1883 is commonly unencrypted; do not forward it to the internet.
4. Add an MQTT sensor
For an educational example, this YAML describes an MQTT temperature sensor reading the exact topic introduced above. Put the configuration in the correct Home Assistant YAML location for your setup, then validate and reload or restart as required by your version:
mqtt:
sensor:
- name: "Office Temperature"
unique_id: office_temperature_mqtt
state_topic: "home/office/temperature"
unit_of_measurement: "°C"
device_class: temperature
state_class: measurementThis manual definition is useful for learning. Many modern MQTT devices use MQTT discovery to create the Home Assistant entities without manually adding YAML. Only use manual configuration when it fits the device and integration you’re using.
How MQTT discovery adds devices automatically
MQTT discovery uses special configuration topics to describe entities, identifiers, state topics and supported functions. Home Assistant listens for those messages and creates the relevant devices and entities. The exact discovery payload format matters, and retained old discovery messages can make unwanted entities reappear.
If your project publishes only a number but never sends discovery configuration, Home Assistant will not necessarily know what that number represents until you configure an entity to use it. Use the current MQTT discovery reference if you decide to implement discovery in your own firmware.
MQTT or ESPHome’s native API?
| Situation | My starting choice | Reason |
|---|---|---|
| Normal Wi-Fi ESP32 sensor with Home Assistant | ESPHome native API | Straightforward integration, device entities and OTA workflow. |
| Multiple independent applications need sensor data | MQTT | One publisher can serve several subscribers. |
| Gateway collecting readings from another radio network | Often MQTT | A gateway can publish structured readings to a local broker. |
| A self-contained Home Assistant installation with no custom MQTT dependencies | Native integration where available | Fewer services to configure and maintain. |
| Cross-platform control or complex decoupled integrations | MQTT may help | Publish/subscribe avoids hard-coding every connection. |
My LoRa gateway project is a more natural MQTT example: the gateway receives radio packets, then publishes their values for Home Assistant. For many of the simpler sensors covered in my ESPHome guide, I would stick with the native API.
How MQTT relates to LoRa, Zigbee and Wi-Fi

MQTT is an application-layer messaging protocol, not a substitute for the physical wireless connection. A LoRa radio sends packets over its radio link. A gateway may translate those packets into MQTT messages on an Ethernet or Wi-Fi network. A Zigbee-to-MQTT system similarly connects a Zigbee network to MQTT through coordinator software.
These technologies solve different parts of the communication problem. My Wi-Fi vs Zigbee vs Thread vs LoRa guide covers the radio-network choices separately.
Secure your MQTT broker
- Do not expose an unauthenticated MQTT broker directly to the internet.
- Use unique or appropriately scoped credentials, and disable anonymous connections unless the entire environment is intentionally isolated and controlled.
- Use TLS when traffic crosses untrusted networks, and validate certificates where supported.
- Separate device publishing permissions from broad administrative access when your broker supports access-control lists.
- Avoid publishing passwords, access tokens or sensitive personal data in unprotected topics.
- Monitor unexpected subscriptions, reconnect loops and retained messages that never expire.
Connected, but no readings?
- Check the exact topic spelling, including case and slash placement.
- Confirm the publisher connects to the same broker that Home Assistant uses.
- Subscribe to the topic with a test client to see whether messages really arrive.
- Verify that the payload is valid for the Home Assistant entity, such as a numeric value for a numeric sensor.
- Check broker authentication and ACL permissions.
- If discovery is in use, inspect the config topic and whether the configuration has been retained correctly.
- Check whether retained old states are being mistaken for current readings.
- If messages arrive slowly or disappear, inspect Wi-Fi quality, keepalive behaviour and QoS choices.
Frequently asked questions
Does MQTT require the internet?
No. A broker and its clients can run entirely on your local network. Internet connectivity is only needed if your chosen systems depend on external services.
Does Home Assistant need MQTT?
No. Many Home Assistant integrations talk to devices or services without MQTT. Use it when it suits the device’s protocol or your architecture.
Can I use MQTT with an ESP32?
Yes. ESP32 firmware can connect as an MQTT client over a suitable TCP/IP connection, publish sensor data and receive commands. You can write your own firmware or use a compatible software platform.
Are messages stored forever?
Not by default. Ordinary messages are delivered to matching connected subscribers. Retained messages preserve the latest retained payload on a topic; broker persistence and session settings influence other delivery behaviour.
When I’d actually use MQTT
If you’re just sending one ESPHome sensor to Home Assistant, use the native API unless you have another reason not to. MQTT earns its place when several systems need the same data, or when a gateway is translating messages from another network. Once you understand topics, retained messages and availability, it becomes much easier to troubleshoot.
If you’re beginning with hardware, my Home Assistant getting-started guide, DIY LoRa gateway and ESPHome introduction are useful next steps.




