Home Assistant MQTT auto-discovery library for ESP32. It provides a small set of entity types (sensor, binary sensor, button) with simple APIs and diagnostic helpers.
- Auto-Discovery: Registers entities via Home Assistant MQTT discovery topics.
- Device Grouping: All entities share a single HA device (same identifiers + metadata).
- Diagnostics: Built-in auto-updated sensors for RSSI, uptime, free heap, and reset reason.
- Command Callbacks: Button entities can trigger callbacks from Home Assistant.
- ESP32-only: Uses ESP32 WiFi and reset APIs.
- Sensor:
HA::Sensor::Generic,HA::Sensor::Diag, plus auto-diagnostic sensor variants. - Binary Sensor:
HA::BinarySensor::Generic. - Button:
HA::Button::Generic,HA::Button::Restart. - Switch:
HA::Switch::Generic,HA::Switch::Outlet.
- PubSubClient (^2.8)
- ArduinoJson (^7.0)
- ArduinoLog (^1.0.1)
Add to your platformio.ini:
lib_deps =
https://github.com/DereIBims/HomeAssistantManager.git
knolleary/PubSubClient@^2.8
bblanchon/ArduinoJson@^7.0
thijse/ArduinoLog@^1.0.1Note: All dependencies are automatically handled by the library definition, however for some reason they're not available for the project. Therefor they need to be added to lib_deps.
#include "HomeAssistantManager.h"
// Manager requires WiFi + MQTT server, log level is optional
HA::Manager hassManager("your-ssid", "your-password", "mqtt-host", LOG_LEVEL_INFO);
// Optional MQTT settings
// hassManager.setMqttPort(1883);
// hassManager.setMqttCredentials("mqtt-user", "mqtt-pass");
// Example entities
HA::Sensor::Generic tempSensor(&hassManager, "Temperature", HA::Temperature, HA::MEASUREMENT);
HA::BinarySensor::Generic doorSensor(&hassManager, "Door", HA::Door);
HA::Button::Generic testButton(&hassManager, "Test Button");
void onTestButton() {
Serial.println("Button pressed");
}
void setup() {
Serial.begin(115200);
testButton.setCommandCallback(onTestButton);
if (!hassManager.begin("ESP32_HASS", "MyCompany", "ESP32", "1.0.0")) {
while (true) {
delay(1000);
}
}
}
void loop() {
hassManager.loop();
tempSensor.setValue(23.5, 1);
doorSensor.setValue(true);
delay(5000);
}These update automatically in Manager::loop():
HA::Sensor::Diag::AutoRSSI rssi(&hassManager, "RSSI");
HA::Sensor::Diag::AutoUptime uptime(&hassManager, "Uptime");
HA::Sensor::Diag::AutoFreeHeap heap(&hassManager, "Free Heap");
HA::Sensor::Diag::AutoResetReason resetReason(&hassManager, "Reset Reason");Comprehensive examples are provided for each device type and constructor variation.
PlatformIO: Examples with main.cpp structure.
Sensor_Generic_DefaultUnit- Temperature sensor with predefined unitSensor_Generic_Custom- Custom sensor with arbitrary class and unitSensor_Diag_Constructors- Diagnostic sensor constructorsDiagnostic_AutoSensors- Auto-updated diagnostic sensors (RSSI, uptime, heap, reset reason)BinarySensor_Generic- Door/window binary sensor exampleButton_Generic- Button with callbackButton_Restart- Restart buttonSwitch_Generic- Generic switch with bidirectional controlSwitch_Outlet- Outlet-class switch for power outlets and appliancesDevice_Removal- Demonstrates how to cleanly remove a device from Home AssistantAdvanced_MultiEntity_With_Timers- Multiple entities with timing logic
Arduino IDE: Same examples as standalone .ino files.
All examples include detailed constructor comments and cover all available APIs.
Manager(const char *ssid, const char *pass, const char *mqttServer, int logLevel = LOG_LEVEL_INFO)void setMqttPort(uint16_t port)void setMqttCredentials(const char *user, const char *pass)void setAutoDiagInterval(unsigned long intervalMs)bool begin(const char *deviceName, const char *manufacturer, const char *model, const char *swVersion, bool unregisterDevices = false)- unregisterDevices parameter:
false(default): Normal operation - device registers and stays registeredtrue:⚠️ Removes ONLY entities defined in current code, program halts after removal, must set tofalseto reactivate- Important: Old/renamed entities not in current code are NOT removed (delete manually from Home Assistant or the MQTT Broker with something like MQTT Explorer)
- See
Device_Removalexample for complete workflow
- unregisterDevices parameter:
void loop()
Sensor::Generic(Manager *mgr, const char *name, SensorClass sensorClass, const char *stateClass = HA::MEASUREMENT)Sensor::Generic(Manager *mgr, const char *name, const char *sensorClass, const char *unit, const char *stateClass = HA::MEASUREMENT)void setValue(float value, int decimalPlaces = 1)void setValue(int value)void setValue(long value)void setValue(double value, int decimalPlaces = 1)
BinarySensor::Generic(Manager *mgr, const char *name, const char *sensorClass)void setValue(bool state)
Button::Generic(Manager *mgr, const char *name)Button::Restart(Manager *mgr, const char *name)void setCommandCallback(Callback cb)
Switch::Generic(Manager *mgr, const char *name)Switch::Outlet(Manager *mgr, const char *name)void setCommandCallback(Callback cb)void setValue(bool state)
- ESP32 only (guarded by
ARDUINO_ARCH_ESP32). - Entity names are used in MQTT topics and unique IDs, and are combined with the device MAC.
- Only the entity types listed above are implemented in this library.- Device Removal: When
unregisterDevices = trueinManager::begin():⚠️ Only removes entities currently defined in your code⚠️ Program halts after removal (intentional - no entities to process)⚠️ Must set back tofalseand reflash to use device again- Old/renamed entities must be manually deleted from Home Assistant
- See
Device_Removalexample for complete workflow- AI assistance note: AI was used to generate documentation and examples and to answer occasional questions. It was not used for “vibe coding.” All library code is handwritten.
MIT License - see LICENSE file for details.