- Learn example words:
Let’s start by looking at any example.
Assuming there are no devices in your Home Assistant, I want to add some devices.
See the sample code:
sensor:
- platform: mqtt
state_topic: "home/bedroom/temperature"
name: "MQTT Sensor 1"
Two basic components when declaring a new component (domain) is the domain (called the easy-to-understand class) and the platform. - Explain:
- The class is to declare for the Home Assistant to know which device you are about to add, which device group it belongs to, eg switch (switch) or light (lamp) or sensor (sensor) …
- Once you have declared the class, Hass knows what kind of device it is then we declare it to the platform (like a carrier). We look at the sample code as follows to add a device of the sensor class ( ) and the platform is mqtt (  . The accompanying information is the name ( , the value is a string so you put it in quotation marks) and is the address. In order to get data, you only need to understand the concept, and MQTT will learn in the following articles The enclosed information varies according to the type of platform, each type has different information so they We have to get used to each type, but there are few general rules.
sensor:
platform:mqtt)
name:
state_topic:
Next problem:
If we need to add a second sensor similar to the first sensor, how do we declare it? Suppose the second sensor has a sensor class (of course) and the platform is also MQTT. Declare the following as you see right:
sensor:
- platform: mqtt
state_topic: "home/bedroom/temperature"
name: "MQTT Sensor 1"
sensor:
- platform: mqtt
state_topic: "home/kitchen/temperature"
name: "MQTT Sensor 2"
Looks reasonable, but that’s not true. The same sensor class only needs to be declared once. Declare the following as correct:
sensor:
- platform: mqtt
state_topic: "home/bedroom/temperature"
name: "MQTT Sensor 1"
- platform: mqtt
state_topic: "home/kitchen/temperature"
name: "MQTT Sensor 2"
Such class
sensor:
Once declared is okay, guys. But wait, there is another way of declaring, less used but also true:
sensor 1:
- platform: mqtt
state_topic: "home/bedroom/temperature"
name: "MQTT Sensor 1"
sensor 2:
- platform: mqtt
state_topic: "home/kitchen/temperature"
name: "MQTT Sensor 2"