MQTT Discovery
The discovery of MQTT devices will enable one to use MQTT devices with only minimal configuration effort on the side of Home Assistant. The configuration is done on the device itself and the topic used by the device. Similar to the HTTP binary sensor and the HTTP sensor. The basic idea is that the device itself adds its configuration into your configuration.yaml
automatically. To prevent multiple identical entries if a device reconnects a unique identifier is necessary. Two parts are required on the device side: The configuration topic which contains the necessary device type and unique identifier and the remaining device configuration without the device type.
Supported by MQTT discovery:
To enable MQTT discovery, add the following to your configuration.yaml
file:
# Example configuration.yaml entry
mqtt:
discovery: true
discovery_prefix: homeassistant
Configuration variables:
- discovery (Optional): If the MQTT discovery should be enabled or not. Defaults to
False
. - discovery_prefix (Optional): The prefix for the discovery topic. Defaults to
homeassistant
.
The discovery topic need to follow a specific format:
<discovery_prefix>/<component>/[<node_id>/]<object_id>/<>
<component>
: One of the supported components, eg.binary_sensor
.<node_id>
: (Optional) id of the node providing the topic.<object_id>
: “The ID of the device. This is only to allow for separate topics for each device and is not used for theentity_id
.”<>
: The topicconfig
orstate
which defines the current action.
The payload will be checked like an entry in your configuration.yaml
file if a new device is added. This means that missing variables will be filled with the platform’s default values. All configuration variables which are required must be present in the initial payload send to /config
.
The <node_id>
level can be used by clients to only subscribe to their own (command) topics by using one wildcard topic like <discovery_prefix>/+/<node_id>/+/set
.
Support by third-party tools
The following firmware for ESP8266, ESP32 and Sonoff unit has built-in support for MQTT discovery:
- Sonoff-Tasmota (starting with 5.11.1e)
- esphomelib
- ESPurna
- Arilux AL-LC0X LED controllers
Examples
A motion detection device which can be represented by a binary sensor for your garden would sent its configuration as JSON payload to the Configuration topic. After the first message to config
, then the MQTT messages sent to the state topic will update the state in Home Assistant.
- Configuration topic:
homeassistant/binary_sensor/garden/config
- State topic:
homeassistant/binary_sensor/garden/state
- Payload:
{"name": "garden", "device_class": "motion"}
To create a new sensor manually. For more details please refer to the MQTT testing section.
$ mosquitto_pub -h 127.0.0.1 -p 1883 -t "homeassistant/binary_sensor/garden/config" -m '{"name": "garden", "device_class": "motion"}'
Update the state.
$ mosquitto_pub -h 127.0.0.1 -p 1883 -t "homeassistant/binary_sensor/garden/state" -m ON
Setting up a switch is similar but requires a command_topic
as mentionend in the MQTT switch documentation.
- Configuration topic:
homeassistant/switch/irrigation/config
- State topic:
homeassistant/switch/irrigation/state
- Payload:
{"name": "garden", "command_topic": "homeassistant/switch/irrigation/set"}
$ mosquitto_pub -h 127.0.0.1 -p 1883 -t "homeassistant/switch/irrigation/config" \
-m '{"name": "garden", "command_topic": "homeassistant/switch/irrigation/set"}'
Set the state.
$ mosquitto_pub -h 127.0.0.1 -p 1883 -t "homeassistant/switch/irrigation/set" -m ON
Setting up a sensor with multiple measurement values requires multiple consecutive configuration topic submissions.
- Configuration topic no1:
homeassistant/sensor/sensorBedroomT/config
- Configuration payload no1:
{"device_class": "sensor", "name": "Temperature", "state_topic": "homeassistant/sensor/sensorBedroom/state", "unit_of_measurement": "°C", "value_template": "{{ value_json.temperature}}" }
- Configuration topic no2:
homeassistant/sensor/sensorBedroomH/config
- Configuration payload no2:
{"device_class": "sensor", "name": "Humidity", "state_topic": "homeassistant/sensor/sensorBedroom/state", "unit_of_measurement": "%", "value_template": "{{ value_json.humidity}}" }
- Common state payload:
{ "temperature": 23.20, "humidity": 43.70 }