Tuesday, October 4, 2022

(#43) Home Assistant Legacy Sensors - Part 3

It's working. Mostly.


With success from hooking the Tuya Air Quality Monitor (non-Zigbee) WiFi up to Home Assistant using the MQTT Device Discovery Protocol, it was not a lot of work to get the Eaton Home Heartbeat (HHB) sensors added.

As mentioned before, the HHB sensors are Zigbee based but I'm unable to quickly see how to unpair them from the Eaton base station and see if they'll re-pair to my Sonoff Zigbee dongle.

So, since the code that reads the HHB sensor data by polling the base station has been up and running for eight years, I made the decision to just add in the MQTT packets to support MQTT Device Discovery. And see if the sensors will appear in Home Assistant.

For the impatient, yes it works. The HHB sensors are now showing up in Home Assistant. The HHB Open/Close sensors, the Tilt Sensor, the Power Sensor, the Wet/Dry Sensor and the Motion sensors all are reporting their status in Home Assistant.


The only downside that I've discovered so far is the lag between a change in a sensor's status and when Home Assistant reflects the state change. That's because the old HHB code polls the gateway for sensor status changes.  So there's a couple of seconds of delay between a state change and the UI reflecting the state change.

No biggie. I can live with that.

Implementation - Summarized

The details are irrelevant; they're specific to my original code from 2013. And you can't buy these sensors anymore.

Nonetheless, for the curious, here's a brief summary:

- original code 'discovered' all of the HHB sensors at startup, by polling the gateway

- new Home Assistant (HA) code, added a call to "HA_CreateConfigurationMessage( HHB_Device )" as each sensor was discovered

Configuration messages were type "binary sensor". Here's the one for the HHB Open/Close Sensor:

 const   char *configTemplate_OpenCloseSensor = 
        "{\"availability\":[{\"topic\":\"%s\",\"value_template\":\"{{ value_json.state }}\"}],"       
        "\"device\":{\"identifiers\": [ \"%s\"],"                                                     
        "\"manufacturer\":\"Eaton\",\"model\":\"HHB OpenClose\",\"name\":\"%s\",\"via_device\": \"HHB\" },"  
        "\"device_class\":\"door\",\"enabled_by_default\":true,\"expire_after\" : 3600,\"force_update\" : true," 
        "\"name\":\"%s [%s] OpenClose\","               
        "\"state_topic\":\"HHB/%s\","                   
        "\"unique_id\":\"%s_OpenClose\","           
        "\"payload_on\":\"OPEN\", \"payload_off\":\"CLOSED\","   
        "\"value_template\":\"{{ value_json.state }}\"}";

The "%s" string substitutions were the MAC address for each sensor.

And then published to an MQTT Topic like:

homeassistant/binary_sensor/%s/config

Again, where the subsitution was the device MAC address.

Most of the sensors (all but the Power Sensor) also had a battery configuration message sent.
// The Eaton HHB battery levels are binary - either OK or not
    const   char *configTemplate_Battery = 
        "{\"availability\":[{\"topic\":\"%s\",\"value_template\":\"{{ value_json.state }}\"}],"       
        "\"device\":{\"identifiers\": [ \"%s\"],"                                                     
        "\"manufacturer\":\"Eaton\",\"model\":\"HHB OpenClose\",\"name\":\"%s\",\"via_device\": \"HHB\" },"  
        "\"device_class\":\"battery\",\"enabled_by_default\":true,\"expire_after\" : 3600,\"force_update\" : true," 
        "\"name\":\"%s [%s] Battery\","               
        "\"state_topic\":\"HHB/%s\","                   
        "\"unique_id\":\"%s_Battery\","           
        "\"payload_on\":\"LOW BATTERY\", \"payload_off\":\"BATTERY OK\","   
        "\"value_template\":\"{{ value_json.battery }}\"}";


To a topic of:

homeassistant/binary_sensor/%s/battery/config

That took care of telling Home Assistant to expect new sensors. Then it was a small addition to publish the unchanged data payload to a new MQTT topic.

Data Payloads still look like:

{"topic" : "HHB/STATUS",  "dateTime" : "2022-10-04T13:37:31-0600" , "deviceType" : 23 , "type" : "MOTION SENSOR" , "name" : "Dining Room Motion" , "state" : "MOTION" , "duration" : 60 , "setAlarmAction" : "ALARM ON MOTION" , "unsetAlarmAction" : "NO ALARM ON NO MOTION" , "setCallAction" : "DO NOT CALL ON MOTION" , "unsetCallAction" : "CALL ON NO MOTION" , "online" : "ONLINE" , "battery" : "BATTERY OK" , "triggered" : "TRIGGERED" , "MACAddress" : "0000093BF5" }

And published to a topic like:

HHB/0000093BF5 


There's a new payload to indicate sensor state. I think I could have told HA to parse state from the existing payload, but since this approach worked for the Tuya sensor, I just added it here.

{"state":"online"}

HHB/0000093BF5/STATE 


And that's about it. Like I said - the original code is from 2013 and updated a personal web page. So that's unique to me. And you can't buy these HHB sensors any more.  There's no sense going into more detail.

It's working.

I salvaged the existing sensors and have them integrated with Home Assistant. 




That's good enough for me, for today.





No comments :

Post a Comment