Quick Answer
MQTT topics are like email addresses for messages. Devices publish to topics, other devices subscribe. Install Mosquitto in HA, use mosquitto_pub to send a test message, mosquitto_sub to receive it, then create an HA sensor that reads from any MQTT topic. The whole thing takes 15 minutes.
Table of Contents
What You Need
- Home Assistant (any version) running
- Mosquitto MQTT Broker add-on (free, from HA add-on store)
mosquitto-clientspackage on your computer (for terminal testing)
Topics and Payloads Explained
An MQTT topic is a hierarchical string that identifies where a message goes. Think of it like a folder path: home/bedroom/temperature. Topics use forward slashes to create hierarchy.
The payload is the content of the message — it can be plain text, a number, JSON, or even binary data.
Examples of how smart home devices use topics:
zigbee2mqtt/bedroom-sensor— Zigbee2MQTT publishes all sensor data as JSONshellies/shelly1pm-ABC/relay/0— Shelly publishes switch state ("on" or "off")home/alarm/state— a custom topic you might create for an alarm system statehomeassistant/sensor/bedroom_temp/state— HA MQTT autodiscovery pattern
MQTT also supports wildcards:
home/#— matches all topics underhome/home/+/temperature— matcheshome/bedroom/temperature,home/kitchen/temperature, etc. (+matches exactly one level)
Installing Mosquitto in HA
- Go to Settings → Add-ons → Add-on Store
- Search Mosquitto broker → click Install
- Enable Start on boot and Watchdog
- Click Start — it starts immediately
- Go to Settings → Integrations — HA auto-discovers Mosquitto and shows a configure prompt
- Click Configure → Submit — the MQTT integration is now active
Mosquitto uses your HA user credentials for authentication. Port 1883 (plain), 8883 (TLS).
mosquitto_pub — Send Your First Message
Install the client tools on your Linux/Mac computer:
# Ubuntu/Debian: sudo apt install mosquitto-clients # macOS: brew install mosquitto
Now publish a test message. Replace values with your HA IP and credentials:
mosquitto_pub \ -h 192.168.1.100 \ -p 1883 \ -u "your_ha_username" \ -P "your_ha_password" \ -t "home/test/hello" \ -m "world"
If no error appears, the message was delivered. Send a JSON payload:
mosquitto_pub \
-h 192.168.1.100 \
-p 1883 \
-u "your_ha_username" \
-P "your_ha_password" \
-t "home/bedroom/sensor" \
-m '{"temperature": 22.5, "humidity": 60}'
mosquitto_sub — Receive Messages
mosquitto_sub \ -h 192.168.1.100 \ -p 1883 \ -u "your_ha_username" \ -P "your_ha_password" \ -t "#" \ -v
The -t "#" subscribes to every single topic. The -v flag prints the topic name before each payload. Run this in one terminal window, then publish from another — you'll see messages appear in real time. This is incredibly useful for debugging what your Zigbee2MQTT, Shelly, or other devices are publishing.
To subscribe to a specific path:
mosquitto_sub \ -h 192.168.1.100 \ -p 1883 \ -u "your_ha_username" \ -P "your_ha_password" \ -t "zigbee2mqtt/bedroom-sensor" \ -v
HA MQTT Sensor (YAML)
Create an HA sensor that reads from an MQTT topic. This is how ESPHome and custom devices get their data into HA:
mqtt:
sensor:
- name: "Bedroom Temperature"
unique_id: bedroom_temperature_mqtt_v1
state_topic: "home/bedroom/sensor"
value_template: "{{ value_json.temperature }}"
unit_of_measurement: "°C"
device_class: temperature
state_class: measurement
- name: "Bedroom Humidity"
unique_id: bedroom_humidity_mqtt_v1
state_topic: "home/bedroom/sensor"
value_template: "{{ value_json.humidity }}"
unit_of_measurement: "%"
device_class: humidity
state_class: measurement
The value_template uses Jinja2 templating to extract values from JSON payloads. After restarting HA, these sensors appear in your entity list and update whenever a message is published to that topic.
Automation from MQTT Message (YAML)
Trigger an automation when a specific MQTT message arrives:
automation:
- alias: "Alert on high temperature (MQTT)"
trigger:
- platform: mqtt
topic: "home/bedroom/sensor"
value_template: "{{ value_json.temperature | float }}"
above: 28
action:
- service: notify.mobile_app_your_phone
data:
title: "Hot bedroom!"
message: >
Temperature is {{ trigger.payload_json.temperature }}°C.
- alias: "Custom device button press"
trigger:
- platform: mqtt
topic: "home/custom-button/action"
payload: "pressed"
action:
- service: light.toggle
target:
entity_id: light.bedroom
Troubleshooting
- Entities don’t appear in Home Assistant: Confirm the integration is loaded correctly and double-check that the device is supported by the platform you chose.
- Commands feel delayed: This is usually caused by cloud polling, weak Wi-Fi, or a slow Zigbee mesh rather than Home Assistant itself.
- Things work in the vendor app but not in HA: Re-check credentials, local API permissions, and whether the device is using the correct network path.
- Automations misfire: Open the automation trace in Home Assistant and verify the exact entity IDs and state changes being used.
FAQ
How do I find what MQTT topics my devices are using?
Run mosquitto_sub -h YOUR_HA_IP -u USER -P PASS -t "#" -v and watch for messages. Every Zigbee2MQTT device, every Shelly, every MQTT-capable device will show up here. It's the single best debugging tool for MQTT.
What's a "retained" MQTT message?
When you publish with the -r flag (mosquitto_pub ... -r), the broker stores the last message for that topic and immediately delivers it to any new subscriber. This is useful for device state — when HA restarts, it immediately gets the current state of all retained topics.
Can two devices share the same MQTT topic?
Yes, but it causes conflicts unless that's intentional. Best practice: give each device its own unique topic path. Two publishers to the same topic will overwrite each other's state in HA.
SmartWired uses affiliate links. If you buy through our links, we may earn a commission at no extra cost to you. See our Affiliate Disclosure.