Home Assistant discovery
Make simulated devices appear in Home Assistant by themselves, with MQTT Discovery.
Home Assistant's MQTT integration finds devices through MQTT Discovery: a device publishes a config message that says what it measures and where, and Home Assistant creates the device and its entities. With home_assistant = true on a template, every instance of the template does that when it connects. You get sensors in Home Assistant without writing any YAML, to build dashboards and automations against before the real hardware exists.
The project
# Two devices that appear in Home Assistant by themselves: an air sensor and a door contact.
# Both templates turn on MQTT Discovery, so each instance becomes a Home Assistant device with one
# entity per field.
[broker]
host = "localhost"
port = 1883
[[templates]]
id = "air-sensor"
interval = "30s"
home_assistant = true
availability = { topic = "senzor/{{ device_id }}/status", online = "online", offline = "offline" }
[[templates.messages]]
topic = "senzor/{{ device_id }}/state"
payload = '{"co2": {{ co2 | round | int }}, "temperature": {{ temperature | round(1) }}, "humidity": {{ humidity | round | int }}}'
[templates.fields.co2]
generator = "random_walk"
start = 650
min = 400
max = 2500
step = 20
unit = "ppm"
name = "CO2"
[templates.fields.temperature]
generator = "sine"
center = 22
amplitude = 1.5
period = "8h"
unit = "°C"
name = "Temperature"
[templates.fields.humidity]
generator = "random_walk"
start = 45
min = 25
max = 70
step = 1
unit = "%"
name = "Humidity"
[[templates]]
id = "door-contact"
interval = "10s"
home_assistant = true
availability = { topic = "senzor/{{ device_id }}/status", online = "online", offline = "offline" }
[[templates.messages]]
topic = "senzor/{{ device_id }}/contact"
payload = '{"open": {{ open }}}'
[templates.fields.open]
generator = "sequence"
values = [false, false, false, true, true, false]
name = "Door"
[[devices]]
id = "living_room"
template = "air-sensor"
[[devices]]
id = "front_door"
template = "door-contact"
- Both templates set
home_assistant = true. Each instance becomes one Home Assistant device, named after the instance ID:living_roomandfront_door. - Each field becomes one entity of that device. The air sensor has three: CO2, Temperature and Humidity. The field's
nameis the entity's name and itsunitthe unit Home Assistant shows; a field without anameis named by its key. openis a boolean field, so it becomes a binary sensor, which Home Assistant shows as on or off. Fields with numbers are sensors that Home Assistant keeps statistics for; fields with text are sensors without them.- Both templates have availability, so Home Assistant shows the entities as unavailable while an instance is offline, including when it drops off and the broker publishes its Last Will.
In the GUI, the example is under Examples as Home Assistant Discovery.
Run it
Your Home Assistant needs the MQTT integration connected to the same broker as Senzor. Discovery is on by default there, with the prefix homeassistant, which is the prefix Senzor publishes under. Set the project's broker to that broker, then run the project:
senzor-cli run examples/home-assistant-discovery.tomlWithin a few seconds, Settings → Devices & services → MQTT lists two new devices, living_room and front_door, with their entities. Their values follow the messages Senzor publishes every 30 and 10 seconds.
What Senzor publishes
When an instance connects, it publishes one config message per field, QoS 1 and retained, before its online status. It publishes them again after every reconnect, and when an edit changes them while it runs (Editing while running). The topic is homeassistant/<component>/<instance ID>/<field>/config, where <component> is binary_sensor for a boolean field and sensor for the others. Characters other than letters, digits, _ and - in the instance ID and the field name become _, since Home Assistant does not allow them there.
The CO2 field of living_room is announced on homeassistant/sensor/living_room/co2/config with:
{
"availability_topic": "senzor/living_room/status",
"device": {
"identifiers": ["senzor_living_room"],
"manufacturer": "Senzor",
"model": "air-sensor",
"name": "living_room"
},
"name": "CO2",
"origin": {
"name": "Senzor",
"support_url": "https://senzor.kankaj.cz",
"sw_version": "1.0.0"
},
"payload_available": "online",
"payload_not_available": "offline",
"state_class": "measurement",
"state_topic": "senzor/living_room/state",
"unique_id": "senzor_living_room_co2",
"unit_of_measurement": "ppm",
"value_template": "{{ value_json.co2 }}"
}modelis the template's ID.unique_idissenzor_, the instance ID and the field name, so Home Assistant keeps the entity, and any changes you make to it there, across restarts.state_topicandvalue_templatesay where the value is. Senzor works them out from the template, as the next section describes.- Without availability on the template, the three
availabilityandpayload_keys are left out.
How Senzor finds a field
Senzor renders each message twice, with only the field's value changed, and looks at what changes:
- One value inside a JSON payload: Home Assistant reads it there, for example
{{ value_json.co2 }},{{ value_json.door.open }}or{{ value_json.readings[0] }}. A key that is not a plain name is quoted:{{ value_json["CO2 level"] }}. - The whole payload, when it is the value alone such as
650orheat: Home Assistant reads{{ value }}. - For a binary sensor, the value template turns what the payload holds for
trueintoONand anything else intoOFF, for example{{ 'ON' if value_json.open == true else 'OFF' }}.
The first message that holds the field in one of these ways is the one Home Assistant reads. A field that is in no message, only in a topic, in two places of one payload, or in a payload that is neither JSON nor the value alone, such as co2=650;temp=21, is not announced. The template editor says which: under announce over MQTT Discovery it shows Every field is announced., or names the fields after Not announced, not in one place of a payload:.
Remove the devices from Home Assistant
The config messages are retained, so the devices stay in Home Assistant after Senzor stops; they show as unavailable when the template has availability. To remove them, stop the devices and clear the retained messages: Clear retained messages… in the broker settings, or senzor-cli clear-retained. With home_assistant = true, the topics it clears include the config topics, and Home Assistant removes an entity when its config is cleared. Clear them before you turn home_assistant off: once it is off, Senzor no longer lists those topics.
You can also turn home_assistant off while the devices run. Each running instance then clears its config messages itself, and Home Assistant removes its entities. The same happens to one entity when you remove its field from the template while the instance runs.
Home Assistant MQTT Statestream is the opposite direction: it simulates what Home Assistant publishes about its own entities.