battery logic update

This commit is contained in:
Joshua King
2026-02-17 07:07:34 -05:00
parent 49d87e7777
commit d6ddd44787
2 changed files with 59 additions and 2 deletions

View File

@@ -19,7 +19,7 @@ esp32:
type: esp-idf
logger:
level: INFO
level: WARN
api:
encryption:

View File

@@ -7,7 +7,6 @@ i2c:
sda: 8
scl: 9
frequency: 100kHz
scan: true
# CH422G I/O expander (Waveshare uses it for LCD reset/backlight/touch reset)
ch422g:
@@ -51,6 +50,14 @@ globals:
type: uint32_t
restore_value: no
initial_value: '0'
- id: last_battery_level_pct
type: float
restore_value: no
initial_value: '-1.0'
- id: battery_charging_inferred
type: bool
restore_value: no
initial_value: 'false'
interval:
- interval: 1s
@@ -152,3 +159,53 @@ sensor:
device_class: battery
state_class: measurement
accuracy_decimals: 0
on_value:
then:
- lambda: |-
const float pct = x;
const bool valid = pct >= 0.0f && pct <= 100.0f;
// Infer charging from SOC trend when no dedicated charge-detect pin exists.
if (valid) {
if (id(last_battery_level_pct) >= 0.0f) {
if (pct > id(last_battery_level_pct) + 0.2f) {
id(battery_charging_inferred) = true;
} else if (pct < id(last_battery_level_pct) - 0.2f) {
id(battery_charging_inferred) = false;
}
}
id(last_battery_level_pct) = pct;
}
const char *icon = "\U000F0091"; // mdi:battery-unknown
if (!valid) {
icon = "\U000F0091"; // mdi:battery-unknown
} else if (pct >= 99.0f) {
icon = "\U000F0079"; // mdi:battery (full)
} else if (id(battery_charging_inferred)) {
if (pct < 40.0f) {
icon = "\U000F12A4"; // mdi:battery-charging-low
} else if (pct < 80.0f) {
icon = "\U000F12A5"; // mdi:battery-charging-medium
} else {
icon = "\U000F12A6"; // mdi:battery-charging-high
}
} else {
if (pct < 15.0f) {
icon = "\U000F10CD"; // mdi:battery-alert-variant-outline
} else if (pct < 40.0f) {
icon = "\U000F12A1"; // mdi:battery-low
} else if (pct < 80.0f) {
icon = "\U000F12A2"; // mdi:battery-medium
} else {
icon = "\U000F12A3"; // mdi:battery-high
}
}
lv_label_set_text(id(nav_battery_icon_home), icon);
lv_label_set_text(id(nav_battery_icon_living_room), icon);
lv_label_set_text(id(nav_battery_icon_kitchen), icon);
lv_label_set_text(id(nav_battery_icon_upstairs), icon);
lv_label_set_text(id(nav_battery_icon_office), icon);
lv_label_set_text(id(nav_battery_icon_garage), icon);
lv_label_set_text(id(nav_battery_icon_outside), icon);