Make internet gauges scale to live rates

This commit is contained in:
2026-11-06 09:33:46 +01:00
parent 8e40e30a45
commit c743670724
+15 -6
View File
@@ -4,8 +4,6 @@ substitutions:
friendly_name: "Energy Monitor" friendly_name: "Energy Monitor"
grid_power_gauge_max_w: "5000.0" grid_power_gauge_max_w: "5000.0"
balcony_solar_gauge_max_w: "800.0" balcony_solar_gauge_max_w: "800.0"
internet_download_gauge_max_mbps: "1000.0"
internet_upload_gauge_max_mbps: "1000.0"
esphome: esphome:
comment: ${device_description} comment: ${device_description}
@@ -729,10 +727,20 @@ display:
if (id(internet_view) == 0) { if (id(internet_view) == 0) {
it.print(12, 10, id(font_small), Color::WHITE, "Internet"); it.print(12, 10, id(font_small), Color::WHITE, "Internet");
const int cx = 160, cy = 188; const int cx = 160, cy = 188;
// Round each live rate up to a 1/2/5 scale, with a 1 kbps minimum.
auto gauge_maximum = [](float rate) -> float {
const float peak = fmaxf(rate, 0.001f);
const float magnitude = powf(10.0f, floorf(log10f(peak)));
const float normalized = peak / magnitude;
return magnitude * (normalized <= 1 ? 1 : normalized <= 2 ? 2
: normalized <= 5 ? 5 : 10);
};
const float download_maximum = gauge_maximum(download_valid ? download : 0.0f);
const float upload_maximum = gauge_maximum(upload_valid ? upload : 0.0f);
const float download_fraction = download_valid const float download_fraction = download_valid
? fminf(download / ${internet_download_gauge_max_mbps}, 1.0f) : 0.0f; ? fminf(download / download_maximum, 1.0f) : 0.0f;
const float upload_fraction = upload_valid const float upload_fraction = upload_valid
? fminf(upload / ${internet_upload_gauge_max_mbps}, 1.0f) : 0.0f; ? fminf(upload / upload_maximum, 1.0f) : 0.0f;
// Match the energy gauge: download outside, upload inside. // Match the energy gauge: download outside, upload inside.
it.filled_gauge(cx, cy, 140, 122, 100, muted); it.filled_gauge(cx, cy, 140, 122, 100, muted);
it.filled_gauge(cx, cy, 140, 122, static_cast<int>(download_fraction * 100), download_color); it.filled_gauge(cx, cy, 140, 122, static_cast<int>(download_fraction * 100), download_color);
@@ -745,8 +753,9 @@ display:
it.printf(cx, 169, id(font_internet_value), upload_color, TextAlign::TOP_CENTER, it.printf(cx, 169, id(font_internet_value), upload_color, TextAlign::TOP_CENTER,
"%s", upload_valid ? format_rate(upload).c_str() : "--"); "%s", upload_valid ? format_rate(upload).c_str() : "--");
it.printf(cx, 204, id(font_device), Color::WHITE, TextAlign::TOP_CENTER, it.printf(cx, 204, id(font_device), Color::WHITE, TextAlign::TOP_CENTER,
"Max: DL %.0f / UL %.0f Mbps", "Max: DL %s / UL %s",
${internet_download_gauge_max_mbps}, ${internet_upload_gauge_max_mbps}); download_valid ? format_rate(download_maximum).c_str() : "--",
upload_valid ? format_rate(upload_maximum).c_str() : "--");
} else if (id(internet_view) == 1) { } else if (id(internet_view) == 1) {
it.print(12, 10, id(font_device), Color::WHITE, "Internet · 1 h"); it.print(12, 10, id(font_device), Color::WHITE, "Internet · 1 h");
const int left = 28, right = 308, top = 62, zero = 122, bottom = 182; const int left = 28, right = 308, top = 62, zero = 122, bottom = 182;