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.
master
Dustin 2022-05-16 21:40:41 -05:00
parent d0e546571e
commit 1ea3de712e
2 changed files with 6 additions and 5 deletions

View File

@ -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",
};

View File

@ -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;