Legacy Sensors - Configuration - Part 2
Home Assistant MQTT Device Discovery
With the MQTT Configuration topics figured out, now it's on to the message payloads. I'll have six configuration payloads to craft for Home Assistant MQTT Device Discovery to work.
There are six sensors in the Tuya Air Quality Monitor, that means six configuration messages to the six MQTT topics created in Part 1.
The unique device ID we created in Part 1 is part of these payloads.
Something else to decide now is the MQTT topic and JSON payload for the sensor data. When those sensors send their data to the broker, what MQTT topic will they use? What will their data payload format be?
In my case, my Tuya Air Quality Monitor Sensors will publish their values on MQTT topic:
home/eb0b-5a81
And the sensor data payload will look like:
{"topic": "home/eb0b-5a81", "dateTime": "2022-09-27T15:45:43", "version": "1.0", "state": "online", "temperature": 25.2, "humidity": 38.8, "PM25": 3, "CH2O": 0.002, "VOC": 0.008, "CO2": 359}
Configuration Payloads
The documentation for a Sensor Configuration payload is outlined here. Recall that our device ID is "eb0b-05a81"
Each of the six Tuya Air Quality Monitor configuration message carries a payload similar to:
{
"topic":"home/eb0b-05a81/state",
"value_template":"{{ value_json.state }}"
}
],
"device":{
"identifiers":[
"eb0b-05a81"
],
"manufacturer":"Tuya",
"model":"Air Quality Sensor",
"name":"eb0b-05a81",
"hw_version" : "?",
"via_device": "python"
},
"device_class":"volatile_organic_compounds",
"enabled_by_default":true,
"expire_after" : 3600,
"force_update" : true,
"name":"AirQuality/A volatile organic compounds",
"state_class":"measurement",
"state_topic":"home/eb0b-05a81",
"unique_id":"eb0b-05a81_volatile_organic_compounds",
"value_template":"{{ value_json.VOC}}"
}
Let's go through the major sections. I'm sure there's a few different ways to do this, but here's what I did.
Availability
Availability - in this section I tell Home Assistant that the state of this sensor, online or off, will come in a message posted to "home/eb0b-05a81/state". And the state value will be in the JSON payload "state".
Here's my online payload: { "state":"online" }
And my offline payload: { "state":"offline" }
Note! The topics embedded in these messages do NOT start with "homeassistant" as the root topic. The configuration messages are letting Home Assistant know that it will need to subscribe to these topics to pick up sensor data.
We use the idea of "state" again, farther down in the payload. It's a potential for confusion. A better topic for the "availability" section might have been "home/eb0b-05a81/availability" with an availability value in the payload.
As it is - what I've done is tell Home Assistant that it can find the availability status by subscribing to the MQTT topic and extracting the state message from the payload.
Note that I'm NOT using an availability/state topic specific to this sensor. It's NOT "home/eb0b-05a81/volatile_organic_compound/state". It could be, but because all six sensors are tied to a single monitor, I chose one topic. All six sensors will go online or offline together.
Device and...
This section holds information about the device. Home Assistant uses this data to tie it into the device registry. You can read about the attributes; I chose a handful to populate with data meaningful to me.
Moving down, "device_class" controls the Icon displayed in the UI. For devices of type 'binary sensor' a list of the classes is here: Home Assistant Binary Sensors Device Class.
For a device type of 'sensor' a list of the classes is here: Home Assistant Sensors Device Class.
"expire_after" : means I want this sensor to show offline if no data comes in after 3600 seconds / 1 hour.
Setting "name" to "AirQuality/A volatile organic compounds" controls how this sensor is displayed in the UI. I call this monitor "AirQuality/A". I'll call a second monitor "AirQuality/B".
The "state_topic":"home/eb0b-05a81" tells Home Assistant that the sensor data will be coming in on this MQTT topic. So Home Assistant needs to subscribe to this topic.
The "unique_id" for this VoC sensor is the device ID with a string appended. And finally the "value_template":"{{ value_json.VOC}}" tells Home Assistant to parse the VoC value out of the JSON payload's attribute VOC.
That rounds out one of the six Configuration payloads. For brevity here are the other five.
Temperature Sensor Configuration Payload
"availability":[
{
"topic":"home/eb0b-05a81/state",
"value_template":"{{ value_json.state }}"
}
],
"device":{
"identifiers":[
"eb0b-05a81"
],
"manufacturer":"Tuya",
"model":"Air Quality Sensor",
"name":"eb0b-05a81",
"hw_version":"?",
"via_device":"python"
},
"device_class":"temperature",
"enabled_by_default":true,
"expire_after":3600,
"force_update":true,
"name":"AirQuality/A_temperature",
"unique_id":" eb0b-05a81_temperature",
"state_class":"measurement",
"state_topic":"home/eb0b-05a81",
"unit_of_measurement":"°C",
"value_template":"{{ value_json.temperature }}"
}
Humidity Sensor Configuration Payload
Carbon Dioxide Sensor Configuration Payload
Formaldehyde Sensor Configuration Payload
Particulate Matter (2.5) Sensor Configuration Payload
{"availability":[
{
"topic":"home/eb0b-05a81/state",
"value_template":"{{ value_json.state }}"
}
],
"device":{
"identifiers":[
"eb0b-05a81"
],
"manufacturer":"Tuya",
"model":"Air Quality Sensor",
"name":"eb0b-05a81",
"hw_version":"?",
"via_device":"python"
},
"device_class":"pm25",
"enabled_by_default":true,
"expire_after":3600,
"force_update":true,
"name":"AirQuality/A particulate 2.5",
"state_class":"measurement",
"state_topic":"home/eb0b-05a81",
"unique_id":"eb0b-05a81_particulate_matter_2_5",
"unit_of_measurement":"ug/m³",
"value_template":"{{ value_json.PM25 }}"
}
Now try it!