From 1ea3de712ee0074e44ef36f7f3bf4be8a6dfd62a Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Mon, 16 May 2022 21:40:41 -0500 Subject: [PATCH] values: Report battery level in percentage The LiPo battery apparently ranges from 3.0 V to 3.95 V. Since we know the range, we can report the current level as a percentage, which Home Assistant will display. We'll leave the original voltage reading in the MQTT state payload for diagnostic purposes. --- mqtt_discovery.cpp | 10 +++++----- values.cpp | 1 + 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/mqtt_discovery.cpp b/mqtt_discovery.cpp index dcf5134..a7c4465 100644 --- a/mqtt_discovery.cpp +++ b/mqtt_discovery.cpp @@ -75,12 +75,12 @@ bool publish_all_config(PubSubClient* mqtt, const char* ident) { }; publish_config(mqtt, TOPIC_CFG_TEMPERATURE, &temperature); struct sensor_config battery = { - .name = "Garden Sensor Battery Level", - .unique_id = "sensor.garden_sensor_battery_level", - .value_template = "{{ value_json.battery_level }}", + .name = "Garden Sensor Battery", + .unique_id = "sensor.garden_sensor_battery", + .value_template = "{{ value_json.battery }}", .identifier = ident, - .device_class = "voltage", - .unit = "V", + .device_class = "battery", + .unit = "%", .state_class = NULL, .entity_category = "diagnostic", }; diff --git a/values.cpp b/values.cpp index 97ed9ff..67dc8cc 100644 --- a/values.cpp +++ b/values.cpp @@ -20,6 +20,7 @@ bool Values::send(PubSubClient* mqtt, const char* topic) { doc["moisture"] = moisture; doc["temperature"] = round2(temperature); doc["battery_level"] = round2(battery); + doc["battery"] = (int)((battery - 3) / 0.95 * 100); doc["rssi"] = rssi; doc["boot_count"] = boot_count;