TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/esphome/esphome.io/llms.txt
Use this file to discover all available pages before exploring further.
mqtt: component connects your ESPHome device to an MQTT broker, enabling it to publish sensor readings and subscribe to command topics. All ESPHome components that support MQTT automatically publish state updates and subscribe to command topics using a consistent <topic_prefix>/<component_type>/<name>/state and /command scheme. ESPHome also supports Home Assistant MQTT auto-discovery out of the box, so your sensors and switches appear automatically in Home Assistant without any manual entity configuration.
Minimal Example
Configuration Variables
The hostname or IP address of your MQTT broker.
The broker port. Defaults to
1883. Use 8883 for TLS connections.Authentication username. Leave empty for anonymous brokers.
Authentication password. Leave empty for anonymous brokers.
The MQTT client identifier. Defaults to
<node_name>-<mac_address> to ensure uniqueness across devices.Prefix for all auto-generated MQTT topics (state, command, availability). Defaults to the node name. Set to
null to disable automatic topic publishing/subscribing entirely.Enable Home Assistant MQTT entity auto-discovery. Defaults to
true.Enable ESPHome device discovery (used by the dashboard, CLI, and Home Assistant for API-based device detection). Publishes device metadata to
esphome/discover/<name>. Defaults to true.Whether to send discovery messages with the MQTT retain flag. Ensures entities reappear after a Home Assistant restart. Defaults to
true.The MQTT topic prefix used for Home Assistant discovery messages. Defaults to
homeassistant.How to generate unique IDs for discovered entities.
legacy format: ESP<type><object_id>. mac format: <mac>-<type>-<fnv1_hash(name)>. Defaults to legacy.How to generate
object_id for entities. device_name uses <device_name>_<friendly_name> to avoid duplicate entity names across devices. Defaults to none.Whether to use abbreviations in MQTT discovery messages. Shorter keys reduce payload size. Defaults to
true.Whether to request a clean session from the broker on connect. When
false, the broker preserves subscription state across reconnects. Defaults to false.MQTT keepalive interval. Shorter values increase traffic but improve connection reliability detection. Defaults to
15s.How long to wait without an MQTT connection before rebooting. Set to
0s to disable. Defaults to 15min.Topic to publish ESPHome log messages to. Set to
null to disable MQTT log forwarding. Supports an additional level sub-key to filter log severity.Message published when the broker connection is established. Used for availability reporting. Defaults to
<topic_prefix>/status with payload online.Message the broker sends if the device disconnects ungracefully (last will and testament). Defaults to
<topic_prefix>/status with payload offline.Message published during a clean/graceful shutdown.
ESP32 only. PEM-encoded CA certificate for TLS broker connections. Paste the full certificate inline.
ESP32 only. PEM-encoded client certificate for mutual TLS authentication.
ESP32 only. PEM-encoded private key matching
client_certificate.ESP32 only. Skip Common Name validation in the server certificate. Note: MbedTLS does not support wildcard certificates (
*.example.com). Defaults to false.ESP32 only. Publish messages from a dedicated MQTT task instead of the main loop. Prevents blocking on poor network conditions. Defaults to
false.Whether MQTT connects at boot. Set to
false for deferred-connect workflows. Defaults to true.Publish
None instead of NaN for unknown/unavailable sensor values. Matches Home Assistant’s “unknown” state semantics. Defaults to false.Block other components from initializing until the MQTT connection is established. Defaults to
false.Triggered when a connection to the broker is established. A
session_present boolean variable is available in lambdas.Triggered when the broker connection is dropped. A
reason variable of type MQTTClientDisconnectReason is available in lambdas.Subscribe to and react to messages on a specific topic. Requires a
topic sub-key. The payload is available as x in lambdas.Like
on_message but automatically decodes a JSON payload using ArduinoJson. The decoded JsonObject is available as x.Advanced Example
Publishing Messages
Use MQTT for Discovery Only (with Native API)
This setup uses MQTT for device discovery (dashboard + HA detection) while Home Assistant connects via the faster native API:Cleaning Stale Discovery Messages
MQTT and Wi-Fi are compatible with each other, but MQTT and the native API can run simultaneously. If you use both, be aware of the
reboot_timeout settings on each — both will independently trigger reboots if their respective connections are lost.