Monday, September 26, 2022

(#41) Home Assistant - Legacy Sensors

Adding in Legacy Sensors - Part 1

Those Eaton Home Heartbeat sensors, mentioned in post #2 from May 2014, are still working! 


So, it'd be nice to get them feeding data into my new Home Assistant system.  After reading and researching, my current (as of September 2022) thought is to use "MQTT Device Discovery."


MQTT Device Discovery, in Home Assistant, is the ability to add sensors using carefully formatted MQTT messages sent to the broker.  I've already got an MQTT integration into Home Assistant, and I'm using zigbee2mqtt to feed sensor data to Home Assistant. zigbee2mqtt uses MQTT Device Discovery -- so I'll try it too.


Send the right messages to the broker, and the sensors will appear in Home Assistant. No additional fussing required! Cool!


Reading and Sniffing

A little reading, and a little "eavesdropping" of the messages that zigbee2mqtt sends and I'm able to come up
with a prototype.


For a device, I'm going to start with a non-zigbee sensor, my Tuya WiFi Air Quality Monitor. I've already got Python code that logs into the Tuya IoT cloud, pulls the sensor data and sends it out via MQTT packets to my broker.  

The thought is, to leave that code as is, and just modify the MQTT payload so it matches Home Assistant's MQTT Device Discovery format.


Treat each sensor individually

The format is documented, but I'll share the approach I've taken. First an architectural issue of note. This Air Quality monitor has six sensors (temperature, humidity, carbon dioxide, formaldehyde, particulate matter 2.5, and volatile organic compounds).

Current Best Practice is to treat this one unit as six separate sensors. Don't try to lump them together, yet.

Two MQTT Messages 

At least two. Getting a sensor to show up involves (a) sending a "configuration" message to the "config" topic and (b) sending sensor data to a slightly different topic.  You need at least two message types, I'll be using three.

Configuration - Craft the Root MQTT Topic

MQTT Device Discovery starts with publishing configuration messages to topics, topics that Home Assistant is already subscribed to.  The topic format I'm using starts with:

homeassistant/sensor/<device ID>

"homeassistant" is the default root topic. 

"sensor" is a component that has to match one of the known Home Assistant MQTT component types. See here

And <device ID> is the unique way of identifying this sensor (or group of sensors). In my case, I'm using the unique device ID assigned to my Air Quality sensor from the Tuya IoT cloud.


The device ID assigned to my Monitor is eb0b ... 05a81 (abbreviated as "eb0b-05a81" from now on).  That's what I'll use. You can pick anything you link - MAC address, random characters, as long as its unique to the device.

One Config Message Per Sensor

Each sensor will get a subtopic appended in the format of <sensor description>/configFor the Air Quality Monitor, with it's six sensors, I end up with six configuration topics for MQTT:

homeassistant/sensor/eb0b-05a81/temperature/config

homeassistant/sensor/eb0b-05a81/humidity/config

homeassistant/sensor/eb0b-05a81/carbon_dioxide/config

homeassistant/sensor/eb0b-05a81/formaldehyde/config

homeassistant/sensor/eb0b-05a81/particulate_matter_2_5/config

homeassistant/sensor/eb0b-05a81/volatile_organic_compounds/config


The text for the sensor description appears to not matter - pick what you want. But the final subtopic must be "config". And the start of the topic must be "homeassistant/<component type>/<device ID>"

Now that we have the MQTT Configuration Topics hammered out, onto the Configuration message payloads.


No comments :

Post a Comment