Add daily energy history tracking

This commit is contained in:
2026-11-13 11:33:13 +01:00
parent 60f2e6296d
commit aac3efac2b
3 changed files with 622 additions and 38 deletions
+196 -38
View File
@@ -12,6 +12,10 @@ esphome:
project:
name: monitor.energymonitor
version: "1.0"
includes:
# ESPHome emits angle-bracket includes before globals; the local entry copies the header.
- <energy_day_history.h>
- energy_day_history.h
on_boot:
then:
- light.turn_on: display_backlight
@@ -102,6 +106,13 @@ api:
id: api_server
encryption:
key: !secret apikey
# Ignore log-only clients; each HA connection must deliver fresh counter states.
on_client_connected:
- lambda: |-
if (client_info.rfind("Home Assistant", 0) == 0) id(reset_energy_subscriptions).execute();
on_client_disconnected:
- lambda: |-
if (client_info.rfind("Home Assistant", 0) == 0) id(reset_energy_subscriptions).execute();
ota:
platform: esphome
@@ -173,6 +184,12 @@ font:
glyphsets:
- GF_Latin_Kernel
glyphs: ["äöüÄÖÜß"]
- file: "fonts/Roboto-Regular.ttf"
id: font_chart
size: 10
glyphsets:
- GF_Latin_Kernel
glyphs: ["äöüÄÖÜß"]
- file: "fonts/materialdesignicons-webfont.ttf"
id: font_icon
size: 24
@@ -201,7 +218,19 @@ globals:
- id: page_one_gauge_view
type: bool
restore_value: true
initial_value: 'false'
initial_value: 'true'
- id: energy_day_history
type: EnergyDayHistory
restore_value: true
initial_value: 'EnergyDayHistory{}'
- id: energy_sources_ready
type: std::array<bool, 3>
restore_value: false
initial_value: 'std::array<bool, 3>{}'
- id: energy_daily_sample_day
type: std::array<uint32_t, 3>
restore_value: false
initial_value: 'std::array<uint32_t, 3>{}'
- id: clock_digital_view
type: bool
restore_value: true
@@ -312,14 +341,48 @@ sensor:
id: solar_energy_daily
entity_id: sensor.energy_solar_daily
internal: true
on_value:
- script.execute:
id: record_daily_energy_received
channel: 2
value: !lambda return x;
- platform: homeassistant
id: grid_import_energy_daily
entity_id: sensor.energy_import_daily
internal: true
on_value:
- script.execute:
id: record_daily_energy_received
channel: 0
value: !lambda return x;
- platform: homeassistant
id: grid_export_energy_daily
entity_id: sensor.energy_export_daily
internal: true
on_value:
- script.execute:
id: record_daily_energy_received
channel: 1
value: !lambda return x;
# Lifetime kWh counters avoid races with the daily meters' midnight resets.
- platform: homeassistant
id: grid_import_energy_total
entity_id: sensor.energy_import_sum
internal: true
on_value:
- lambda: id(energy_sources_ready)[0] = std::isfinite(x) && x >= 0.0f;
- platform: homeassistant
id: grid_export_energy_total
entity_id: sensor.energy_export_sum
internal: true
on_value:
- lambda: id(energy_sources_ready)[1] = std::isfinite(x) && x >= 0.0f;
- platform: homeassistant
id: solar_energy_total
entity_id: sensor.balkonkraftwerk_energy
internal: true
on_value:
- lambda: id(energy_sources_ready)[2] = std::isfinite(x) && x >= 0.0f;
- platform: homeassistant
id: hallway_fragrance_power
entity_id: sensor.flur_raumduft_power
@@ -453,7 +516,42 @@ interval:
id(climate_history_next) = (climate_slot + 1) % 361;
if (id(climate_history_count) < 361) id(climate_history_count)++;
// Record energy independently of the selected page or backlight state.
const auto energy_now = id(sntp_time).now();
if (energy_now.is_valid()) {
const uint32_t day = energy_now.year * 10000 + energy_now.month * 100 + energy_now.day_of_month;
sensor::Sensor *counters[] = {id(grid_import_energy_total),
id(grid_export_energy_total), id(solar_energy_total)};
for (int channel = 0; channel < 3; channel++) {
// A fresh sample received before SNTP sync belongs to the first valid day.
if (id(energy_daily_sample_day)[channel] == UINT32_MAX) id(energy_daily_sample_day)[channel] = day;
const float total = connected && id(energy_sources_ready)[channel] && counters[channel]->has_state()
? counters[channel]->state : NAN;
id(energy_day_history).observe(channel, total, day, energy_now.hour,
energy_now.minute * 60 + energy_now.second, energy_now.timestamp);
}
}
script:
- id: reset_energy_subscriptions
then:
- lambda: |-
for (int channel = 0; channel < 3; channel++) {
id(energy_sources_ready)[channel] = false;
id(energy_daily_sample_day)[channel] = 0;
id(energy_day_history).invalidate(channel);
}
- id: record_daily_energy_received
parameters:
channel: int
value: float
then:
- lambda: |-
const auto sample_time = id(sntp_time).now();
id(energy_daily_sample_day)[channel] = !std::isfinite(value) || value < 0.0f ? 0
: sample_time.is_valid()
? sample_time.year * 10000 + sample_time.month * 100 + sample_time.day_of_month
: UINT32_MAX;
- id: refresh_display
then:
# The partial-buffer driver draws multiple strips; freeze time for the entire frame.
@@ -601,46 +699,106 @@ display:
};
if (id(page_index) == 0 && !id(page_one_gauge_view)) {
draw_page_title("Netzleistung");
if (id(grid_power).has_state()) {
const Color grid_power_color = id(grid_power).state >= 0
? Color(143, 190, 223) // #8FBEDF: positive grid power
: Color(150, 110, 217); // #966ED9: negative grid power
it.printf(308, 32, id(font_value), grid_power_color, TextAlign::TOP_RIGHT,
"%+.0f W", id(grid_power).state);
} else {
it.printf(308, 42, id(font_value), Color::WHITE, TextAlign::TOP_RIGHT,
"-- W");
draw_page_title("Energie · Heute");
const Color import_color(143, 190, 223);
const Color export_color(150, 110, 217);
const Color solar_color(255, 170, 40);
const Color grid_color(30, 38, 48);
const Color missing_color(85, 95, 110);
const bool connected = id(api_server).is_connected_with_state_subscription();
const uint32_t day = now.is_valid()
? now.year * 10000 + now.month * 100 + now.day_of_month : 0;
const char *labels[] = {"Bezug", "Einspeisung", "Solar"};
const Color colors[] = {import_color, export_color, solar_color};
sensor::Sensor *daily[] = {id(grid_import_energy_daily),
id(grid_export_energy_daily), id(solar_energy_daily)};
// The same three accents identify the totals and their hourly bars.
for (int channel = 0; channel < 3; channel++) {
const int x = 10 + channel * 102;
it.filled_rectangle(x, 39, 96, 64, Color(14, 20, 28));
it.filled_rectangle(x, 39, 96, 2, colors[channel]);
it.print(x + 48, 46, id(font_device), colors[channel], TextAlign::TOP_CENTER,
labels[channel]);
if (connected && day != 0 && id(energy_daily_sample_day)[channel] == day
&& daily[channel]->has_state() && std::isfinite(daily[channel]->state)
&& daily[channel]->state >= 0.0f) {
it.printf(x + 48, 63, id(font_internet_value), Color::WHITE,
TextAlign::TOP_CENTER, "%.2f", daily[channel]->state);
} else {
it.print(x + 48, 63, id(font_internet_value), missing_color,
TextAlign::TOP_CENTER, "--");
}
it.print(x + 48, 89, id(font_chart), colors[channel], TextAlign::TOP_CENTER, "kWh");
}
it.print(12, 108, id(font_device), Color::WHITE, "Stunden · kWh");
it.print(308, 108, id(font_chart), missing_color, TextAlign::TOP_RIGHT, "lokal · Teildaten grau");
const int left = 40, right = 304, top = 129, bottom = 193, step = 11, bar_width = 8;
const auto &history = id(energy_day_history);
const bool today = day != 0 && history.day() == day;
float positive_peak = 0.0f, negative_peak = 0.0f;
bool any_data = false;
for (int hour = 0; today && hour < 24; hour++) {
positive_peak = fmaxf(positive_peak, history.value(0, hour) + history.value(2, hour));
negative_peak = fmaxf(negative_peak, history.value(1, hour));
any_data |= history.has_data(0, hour) || history.has_data(1, hour) || history.has_data(2, hour);
}
// One kWh has the same height above and below zero; reserve space for export.
const float positive_max = gauge_maximum(positive_peak, 0.1f);
const float negative_max = gauge_maximum(negative_peak, positive_max / 3.0f);
const float pixels_per_kwh = (bottom - top) / (positive_max + negative_max);
const int zero = top + lroundf(positive_max * pixels_per_kwh);
for (int hour = 0; hour <= 24; hour += 6) {
const int x = left + hour * step;
it.line(x, top, x, bottom, grid_color);
it.printf(x, 199, id(font_chart), missing_color,
hour == 0 ? TextAlign::TOP_LEFT : hour == 24 ? TextAlign::TOP_RIGHT : TextAlign::TOP_CENTER,
"%02d", hour);
}
it.line(left, top, right, top, grid_color);
it.line(left, bottom, right, bottom, grid_color);
it.line(left, zero, right, zero, Color(75, 85, 100));
it.printf(left - 5, top, id(font_chart), missing_color, TextAlign::CENTER_RIGHT, "%.2g", positive_max);
it.print(left - 5, zero, id(font_chart), Color::WHITE, TextAlign::CENTER_RIGHT, "0");
it.printf(left - 5, bottom, id(font_chart), missing_color, TextAlign::CENTER_RIGHT, "-%.2g", negative_max);
if (id(grid_import_energy_daily).has_state()) {
it.printf(12, 96, id(font_small), Color::WHITE, "Bezug: %.2f kWh",
id(grid_import_energy_daily).state);
} else {
it.printf(12, 96, id(font_small), Color::WHITE, "Bezug: -- kWh");
for (int hour = 0; today && hour <= now.hour; hour++) {
const int x = left + hour * step + 2;
const int solar_height = lroundf(history.value(2, hour) * pixels_per_kwh);
int import_height = lroundf(history.value(0, hour) * pixels_per_kwh);
if (solar_height + import_height > zero - top) import_height = zero - top - solar_height;
int export_height = lroundf(history.value(1, hour) * pixels_per_kwh);
if (export_height > bottom - zero) export_height = bottom - zero;
if (solar_height > 0) {
it.filled_rectangle(x, zero - solar_height, bar_width, solar_height, Color(165, 100, 22));
it.line(x, zero - solar_height, x + bar_width - 1, zero - solar_height, solar_color);
}
if (import_height > 0) {
const int y = zero - solar_height - import_height;
it.filled_rectangle(x, y, bar_width, import_height, Color(56, 91, 118));
it.line(x, y, x + bar_width - 1, y, import_color);
}
if (export_height > 0) {
it.filled_rectangle(x, zero + 1, bar_width, export_height, Color(80, 57, 113));
it.line(x, zero + export_height, x + bar_width - 1, zero + export_height, export_color);
}
const uint32_t expected = hour < now.hour ? 3600 : now.minute * 60 + now.second;
if (!history.has_data(0, hour) || !history.has_data(1, hour) || !history.has_data(2, hour)
|| history.coverage_seconds(0, hour) < expected || history.coverage_seconds(1, hour) < expected
|| history.coverage_seconds(2, hour) < expected) {
it.line(x + 2, zero - 2, x + bar_width - 3, zero - 2, missing_color);
}
}
if (id(grid_export_energy_daily).has_state()) {
it.printf(12, 116, id(font_small), Color::WHITE, "Einspeisung: %.2f kWh",
id(grid_export_energy_daily).state);
} else {
it.printf(12, 116, id(font_small), Color::WHITE, "Einspeisung: -- kWh");
if (now.is_valid()) {
const int cursor_x = left + now.hour * step + (now.minute * 60 + now.second) * step / 3600;
for (int y = top; y < bottom; y += 5) {
it.line(cursor_x, y, cursor_x, y + 2, Color(110, 135, 155));
}
}
it.printf(12, 138, id(font_small), Color::WHITE, "Balkonkraftwerk");
// Daily solar energy is aligned to the left of the current solar-power reading.
if (id(solar_energy_daily).has_state()) {
it.printf(12, 180, id(font_small), Color(255, 170, 40), "Solar: %.2f kWh",
id(solar_energy_daily).state);
} else {
it.printf(12, 180, id(font_small), Color(255, 170, 40), "Solar: -- kWh");
}
if (id(balcony_solar_power).has_state()) {
it.printf(308, 158, id(font_value), Color(255, 170, 40), TextAlign::TOP_RIGHT,
"%.0f W", id(balcony_solar_power).state);
} else {
it.printf(308, 168, id(font_value), Color(255, 170, 40), TextAlign::TOP_RIGHT,
"-- W");
if (!any_data) {
it.print(172, 148, id(font_device), Color::WHITE, TextAlign::TOP_CENTER,
now.is_valid() ? "Stunden werden aufgezeichnet" : "Warte auf Zeitsynchronisation");
}
} else if (id(page_index) == 0) {
draw_page_title("Energie");
@@ -1102,8 +1260,8 @@ display:
"Weiter >");
if (id(page_index) == 0) {
it.printf(160, 239, id(font_small), Color::WHITE, TextAlign::BOTTOM_CENTER,
"%s · %d / 2", id(page_one_gauge_view) ? "Tacho" : "Werte",
id(page_one_gauge_view) ? 2 : 1);
"%s · %d / 2", id(page_one_gauge_view) ? "Tacho" : "Tag",
id(page_one_gauge_view) ? 1 : 2);
} else if (id(page_index) == 1) {
it.printf(160, 239, id(font_small), Color::WHITE, TextAlign::BOTTOM_CENTER,
"Geräte · %d / 2", id(device_usage_view) + 1);