logo elektroda
logo elektroda
X
logo elektroda

How does Home Assistant Discovery work? JSON format, sensors, device classes, units

p.kaczmarek2  Cool? (+3)
📢 Listen (AI):
Home Assistant logo on a blue background. .
Here I will show the inner workings of MQTT-based HASS Discovery, a way for Home Assistant to automatically detect and configure devices. HASS Discovery only requires our device to be connected to the MQTT broker in HA. After that, our device can already be detected. In order for the discovery to take place, our device needs to publish a properly formatted text in JSON format to a specific subject from Discovery. This JSON contains information describing the entity, i.e. its type, name, icon, entity and the associated MQTT topics, among others, the topic for reading the value (listening) and the topic for setting the new value (from the HA).

These types of mechanisms are best presented in practice, so I suggest starting with practice. Let's try firing up some Discovery and take a look at what is being sent. Fortunately, there is a ready-made tool in Home Assistant to listen in on MQTT communications. It comes with Mosquitto broker integrations:
MQTT Integration Interface in Home Assistant .
Then we click Configure:
MQTT configuration screen with options for publishing and listening to topics. .
This brings us to the tool that allows us to publish packages and listen for publications.
Now it's time to listen to something.
Just a note - of course, I'm assuming that the user knows the basics of HA, MQTT, or JSON there.... The inner workings of Discovery are already a bit advanced, but also a worthy topic.
It's also worth looking at the HA documentation if you have questions.
So, Home Assistant Discovery by default is based on the subject homeassistant , we append a multi-level wildcard behind it, i.e. #, so that it listens to all matching subjects:

homeassistant/#
.
And then we click Start Listening:
Screenshot of Home Assistant's MQTT topic listening tool window. .
Now it's time to do some Discovery - e.g. from within OpenBeken:


.
For the example, I used a simulated OBK device with two channels (relays).
Let's see what such a thing "caught" with MQTT:
MQTT communication monitoring tool panel with a visible message. .
Here we have a separate publish for each separate entity.
Let's start with the IP address. Topic (the so-called "mqtt topic"):

homeassistant/sensor/WinTest_00000000_ip/config
.
Content (so-called "mqtt payload"):
Code: JSON
Log in, to see the code
.
Here we have, in sequence:
- "dev" - section describing the device, its ID, name, software version (sw), manufacturer (mf), model (mdl), cu (panel URL)
- "name" - entity name, here "IP"
- "~" - abbreviation for the MQTT topic path, "WT00000000". This allows you to write ~/ip instead of the full "WT00000000/ip"
- "avty_t" - the topic for the availability status, "~/connected", i.e. "WT00000000/connected". On this topic, the device reports its presence.
- "uniq_id" - unique identifier of the entity in the HA, "WinTest_00000000_ip"
- "qos" - Quality of Service level for MQTT, here "1" (guaranteed to be delivered at least once)
- "stat_t" - the topic on which the entity value is published, "~/ip", i.e. "WT00000000/ip". Home Assistant here only listens for changes on this topic.
- "entity_category" - entity category, "diagnostic", i.e. HA treats this as diagnostic information
- "icon" - an icon in HA, "mdi:ip-network", which stands for the network icon from Material Design Icons (MDI)
It is worth noting that abbreviations are used here, making the JSON shorter and easier for the microcontroller to generate and publish.
Similarly, we have separate entities for different device parameters, e.g. for the SSID:

homeassistant/sensor/WinTest_00000000_ssid/config
.
Content:
Code: JSON
Log in, to see the code
.
Here only there is a different "state_topic" ("stat_t") and a different icon.
Similarly for uptime:

homeassistant/sensor/WinTest_00000000_uptime/config

Content:
Code: JSON
Log in, to see the code
.
Here the unit of measurement is entered - "unit_of_meas", in this example it is seconds. In addition, the entity class, "duration", i.e. the period of time, and the value type "total_increasing" are specified, meaning that the value will increase continuously.

Similarly, the temperature of the device (from Beken's internal thermometer) is also specified:

 homeassistant/sensor/WinTest_00000000_temp/config
.
Content:
Code: JSON
Log in, to see the code
.
Here, however, the unit is changed - °C - and the type of measurement is changed, here it is just that, a measurement, or 'measurement'.
In a similar way, this whole section is created:
Diagnostic panel displaying information about the device, including version, IP address, RSSI, SSID, temperature, and uptime. .
Forgive the deficiencies in the screenshot, I was testing on the Simulator.

It's now time to see something that supports both writing and reading. Relay. Its state can Home Assistant both set and read.

 homeassistant/switch/WinTest_00000000_relay_2/config
.
Content:
Code: JSON
Log in, to see the code
.
We have some new fields here:
- "pl_on", or "payload_on", a value that sets the relay to on
- "pl_off", as above, analogous to off
- "stat_t" - "state_topic", the relay's state topic, this is where HA listens for changes
- "cmd_t" - "command_topic", command topic, this is where the Home Assistant sends a new state when the user wants to change the state of the relay

Now a curiosity. This is what relays look like, but what if we turn on light mode in the OBK? There is a flag for that:

Screenshot of MQTT flag configuration for HA Discovery .
Let's check. I have performed HASS Discovery again:

homeassistant/light/WinTest_00000000_light_2/config
.
Code: JSON
Log in, to see the code
.
So Home Assistant has lights and relay mode separately.... although you could probably just as well change the icon - but here it's also about the entity type itself, those "lights" after the slash in the publish subject.

Now, it would be possible to go more deeply into the topic of lights, but I decided I'd leave that for now. This will be covered separately, as there are more topics out there.

In the meantime, I would like to show a few more options for sensors, sensors. In the OBK we have channels for this.
Let's set up:

setChannelType 5 Motion
setChannelType 6 Humidity
setChannelType 7 Temperature_div10
setChannelType 8 Voltage_div100
.
Let's perform another Discovery.

homeassistant/sensor/WinTest_00000000_humidity_6/config
.
Code: JSON
Log in, to see the code
.
Humidity - the unit of measurement is percentages. Simple.
Let's look further.
Movement.

homeassistant/binary_sensor/WinTest_00000000_binary_sensor_5/config
.
Code: JSON
Log in, to see the code
.
Here only the class is set - it is the class that provides the corresponding icon.
View of motion history for device WT00000000 5 in Home Assistant

Now a more interesting thing - a temperature type, but divisible by 10. This comes from the fact that TuyaMCU operates on integers and this type of value is used there. Topic:

homeassistant/sensor/WinTest_00000000_temperature_7/config
.
Code: JSON
Log in, to see the code
.
This is where a new field appears - 'val_tpl'. This is short for "value_template", which means value template. It defines how the value ("value") is formatted before being displayed. You can conveniently test these templates in Developer Tools -> Templates, but about that another time:
Screenshot of Home Assistant developer tools showing the template editor. .

What's left is the voltage - also from TuyaMCU, divided by 100:

 homeassistant/sensor/WinTest_00000000_voltage_8/config 
.
Code: JSON
Log in, to see the code
.
There is rather nothing new here - the value template is similar to that for temperature, just a different multiplier.

That's enough for today.
Now you know more or less how it works that the Home Assistant is able to detect devices on its own. This way, they do not have to be manually added to configuration.yaml. Home Assistant Discovery relies on the device itself publishing information about its entities separately in JSON format under the subject subordinate to "homeassistant/". These JSONs describe their parameters as well as subjects, values, types, icons and entities, and even templates for processing the values on receipt. Home Assistant parses this JSON and then creates the entities and devices itself in MQTT devices. For example:
List of devices connected via MQTT in Home Assistant. Home Assistant device control panel with device info, controls, sensors, and logbook. Screenshot from Home Assistant platform showing device status. .
And you can already create automations from these:
Configuration of a motion detection trigger in Home Assistant. .
In the next section, I will already try to look at a more specific use case, namely the handling of coloured LED lights, operating in both RGB colour and white temperature (CCT) modes.
Do you use Home Assistant Discovery, or perhaps you happen to add devices to configuration.yaml yourself? .
PS: For a complete list of available keys, options and icons, I refer you to the HA documentation, especially as these may change over time... .

About Author
p.kaczmarek2
p.kaczmarek2 wrote 12437 posts with rating 10295 , helped 585 times. Been with us since 2014 year.

Comments

krzbor 25 Feb 2025 19:54

@pkaczmarek2 - on the Controls tab you have the switches. 1 and 2 look normal, while 3 and 4 are flashing. I've managed to solve the mystery of why this is happening - I'm curious to see if you've managed... [Read more]

p.kaczmarek2 25 Feb 2025 21:29

This problem has come up on the forum in the context of OBK. Users have reported that there are flashes (as it sounds like) when the HA restarts, and only when the OBK publishes its state do they turn... [Read more]

krzbor 25 Feb 2025 21:44

I have discovered why this happens. In the beginning there are lightning bolts. In order for them to turn into a normal toggle - and they turn automatically and we have no control over it - there must... [Read more]

p.kaczmarek2 25 Feb 2025 21:51

What do you mean by "exist" in the context of "state_topic"? From what we have tested with users, we have flashed in OBK until OBK publishes its state under "state_topic". To put it another way: 1.... [Read more]

krzbor 25 Feb 2025 22:09

"Exist" in the sense that some "xxx/yyyy" cannot be given. In other words, it is exactly as you describe - the HA must receive the state via the "state_topic" message and does not settle for the mere declaration... [Read more]

walkowiakmariusz8989 03 Mar 2025 18:22

. [Read more]

p.kaczmarek2 03 Mar 2025 18:25

@walkowiakmariusz8989 what do you mean by quoting this passage? The mysterious "WinTest_00000000" comes from the fact that this was tested in the OpenBeken Simulator, which runs on Windows. And the... [Read more]

lexx_ 08 Mar 2025 22:03

I have been experimenting with Discovery themes in Home Assistant for some time now and have encountered a problem that I have not yet managed to solve. The issue is on what basis (and why) some names... [Read more]

krzbor 09 Mar 2025 21:20

. And how do you have Zigbee devices connected to the HA? [Read more]

lexx_ 10 Mar 2025 11:05

Via Zigbe2MQTT. There, a separate topic is created for each device, and in Zigbe2MQTT itself Home Assistant is enabled, so that discovery topics are additionally created. [Read more]

krzbor 12 Mar 2025 23:33

. Start MQTT-Explorer and connect to the MQTT broker with it. Then restart the Z2M. After the restart, Z2M sends "discovery". See if it sends in Polish or English. [Read more]

lexx_ 13 Mar 2025 21:08

I thought about that too, but no, in MQTT explorer everything is in English. In addition, HA would have to "expose" information about what language the user is using, so that Zigbe2MQTT could use this... [Read more]

krzbor 13 Mar 2025 21:22

. It matters what is sent in "discovery". Have you checked the "discovery" messages or the data messages? The MQTT discovery messages are best captured by restarting the Z2M (it is available in the browser... [Read more]

%}