From 31848bf26cb0a477a7ca55064f8eae83f946a24c Mon Sep 17 00:00:00 2001 From: Commander1024 Date: Sat, 30 May 2026 16:49:57 +0200 Subject: [PATCH] Add energy monitor using riemann sum algorithm. --- esphome/lab-ble-proxy.h | 10 +++++++--- esphome/lab-ble-proxy.yaml | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/esphome/lab-ble-proxy.h b/esphome/lab-ble-proxy.h index 29e0460..da40bac 100644 --- a/esphome/lab-ble-proxy.h +++ b/esphome/lab-ble-proxy.h @@ -365,7 +365,7 @@ class Meter { esphome::display::BaseFont *value_font, int display_page = 0, bool atorch_connected = false, float atorch_voltage = NAN, float atorch_current = NAN, float atorch_power = NAN, - float atorch_capacity = NAN, float atorch_temperature = NAN) { + float atorch_capacity = NAN, float atorch_energy = NAN, float atorch_temperature = NAN) { const Color bg(0, 0, 0); const Color fg(210, 210, 210); // Chosen to map to a neutral dark gray in the RGB332 8-bit display palette. @@ -452,11 +452,13 @@ class Meter { char current_text[24]; char power_text[24]; char capacity_text[24]; + char energy_text[24]; char temperature_text[24]; this->format_metric_(voltage_text, sizeof(voltage_text), atorch_voltage, "V", 2); this->format_metric_(current_text, sizeof(current_text), atorch_current, "A", 3); this->format_metric_(power_text, sizeof(power_text), atorch_power, "W", 2); this->format_metric_(capacity_text, sizeof(capacity_text), atorch_capacity, "Ah", 3); + this->format_metric_(energy_text, sizeof(energy_text), atorch_energy, "Wh", 3); this->format_metric_(temperature_text, sizeof(temperature_text), atorch_temperature, "°C", 1); it.filled_rectangle(0, 0, 320, 30, atorch_connected ? panel_dim : panel_inactive); @@ -476,8 +478,10 @@ class Meter { it.filled_rectangle(10, 233, 300, 1, border); it.filled_rectangle(10, 212, 1, 22, border); it.filled_rectangle(309, 212, 1, 22, border); - it.print(24, 216, font, inactive, esphome::display::TextAlign::TOP_LEFT, "CAPACITY"); - it.print(306, 212, value_font, value_color, esphome::display::TextAlign::TOP_RIGHT, capacity_text); + it.print(20, 216, font, inactive, esphome::display::TextAlign::TOP_LEFT, "CAP"); + it.print(64, 216, font, value_color, esphome::display::TextAlign::TOP_LEFT, capacity_text); + it.print(170, 216, font, inactive, esphome::display::TextAlign::TOP_LEFT, "ENERGY"); + it.print(306, 216, font, value_color, esphome::display::TextAlign::TOP_RIGHT, energy_text); } } diff --git a/esphome/lab-ble-proxy.yaml b/esphome/lab-ble-proxy.yaml index b364dce..4934182 100644 --- a/esphome/lab-ble-proxy.yaml +++ b/esphome/lab-ble-proxy.yaml @@ -326,6 +326,7 @@ display: id(atorch_current).has_state() ? id(atorch_current).state : NAN, id(atorch_power).has_state() ? id(atorch_power).state : NAN, id(atorch_capacity).has_state() ? id(atorch_capacity).state : NAN, + id(atorch_energy_calculated).has_state() ? id(atorch_energy_calculated).state : NAN, id(atorch_temperature).has_state() ? id(atorch_temperature).state : NAN ); @@ -480,6 +481,19 @@ sensor: name: "${friendly_name} Atorch Dim Backlight" id: atorch_dim_backlight + - platform: integration + name: "${friendly_name} Atorch Energy" + id: atorch_energy_calculated + sensor: atorch_power + time_unit: h + unit_of_measurement: "Wh" + icon: "mdi:lightning-bolt" + device_class: energy + state_class: total_increasing + accuracy_decimals: 3 + restore: true + integration_method: trapezoid + text_sensor: - platform: template name: "${friendly_name} OWON Reading"