From e2368413458281d8a85e90dedc6969fac0184894 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Tue, 11 Oct 2022 16:11:11 +0200 Subject: [PATCH] publish altitude --- include/ha.h | 11 +++++++++++ include/mqtt.h | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/include/ha.h b/include/ha.h index 69ab24f..0e6f790 100644 --- a/include/ha.h +++ b/include/ha.h @@ -35,6 +35,17 @@ namespace Ha { } }; + struct AltitudeConfig : SensorConfig { + AltitudeConfig(const char* name, const char* uniqueId, const char* stateTopic) { + this->name = name; + this->uniqueId = uniqueId; + this->stateTopic = stateTopic; + this->deviceClass = "distance"; + this->unitMeasure = "m"; + this->valueTemplate = "{{ value_json.altitude }}"; + } + }; + void buildDeviceConfig(JsonDocument& jsonDoc) { JsonObject device = jsonDoc.createNestedObject("device"); device["name"] = "ESP Clock"; diff --git a/include/mqtt.h b/include/mqtt.h index d11540f..8384a61 100644 --- a/include/mqtt.h +++ b/include/mqtt.h @@ -43,9 +43,16 @@ namespace Mqtt { client.publish("homeassistant/sensor/esp_clock/pressure/config", 0, true, message); } + void publishAltitudeConfig() { + char message[JSON_SIZE]; + Ha::buildSensorConfig(message, Ha::AltitudeConfig{"Livingroom Altitude", "livingroom_altitude", bmpTopic}); + client.publish("homeassistant/sensor/esp_clock/altitude/config", 0, true, message); + } + void publishConfig() { publishTempConfig(); publishPressureConfig(); + publishAltitudeConfig(); } void publishCommand() { @@ -65,6 +72,7 @@ namespace Mqtt { StaticJsonDocument<255> jsonDoc; jsonDoc["temperature"] = Bmp::data.temp; jsonDoc["pressure"] = Bmp::data.pressure; + jsonDoc["altitude"] = Bmp::data.altitude; char message[255]; serializeJson(jsonDoc, message); client.publish(bmpTopic, 0, true, message);