From c19e1298214ee435e86a1dc2a0674b890a2b7577 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 11 Jan 2026 14:40:50 -1000 Subject: [PATCH] another pass at reducing the logic --- .../uptime/text_sensor/uptime_text_sensor.cpp | 41 +++++++------------ 1 file changed, 14 insertions(+), 27 deletions(-) diff --git a/esphome/components/uptime/text_sensor/uptime_text_sensor.cpp b/esphome/components/uptime/text_sensor/uptime_text_sensor.cpp index f4fe9b7e33..368061093a 100644 --- a/esphome/components/uptime/text_sensor/uptime_text_sensor.cpp +++ b/esphome/components/uptime/text_sensor/uptime_text_sensor.cpp @@ -54,35 +54,22 @@ void UptimeTextSensor::update() { bool minutes_enabled = interval < 1800; bool hours_enabled = interval < 12 * 3600; - // Determine which units to show - bool show_days, show_hours, show_minutes, show_seconds; + // Show from highest non-zero unit (or all in expand mode) down to smallest enabled + bool show_days = this->expand_ || days > 0; + bool show_hours = hours_enabled && (show_days || hours > 0); + bool show_minutes = minutes_enabled && (show_hours || minutes > 0); + bool show_seconds = seconds_enabled && (show_minutes || seconds > 0); - if (this->expand_) { - // Show all enabled units - show_days = true; - show_hours = hours_enabled; - show_minutes = minutes_enabled; - show_seconds = seconds_enabled; - } else { - // Start with only the smallest enabled unit - show_seconds = seconds_enabled; - show_minutes = minutes_enabled && !show_seconds; - show_hours = hours_enabled && !show_minutes && !show_seconds; - show_days = !show_hours && !show_minutes && !show_seconds; - - // Add larger non-zero units - if (days > 0) + // If nothing shown, show smallest enabled unit + if (!show_days && !show_hours && !show_minutes && !show_seconds) { + if (seconds_enabled) + show_seconds = true; + else if (minutes_enabled) + show_minutes = true; + else if (hours_enabled) + show_hours = true; + else show_days = true; - if (hours > 0 && hours_enabled) - show_hours = true; - if (minutes > 0 && minutes_enabled) - show_minutes = true; - - // Fill in gaps (e.g., show 0h between 1d and 0m) - if (show_days && hours_enabled) - show_hours = true; - if (show_hours && minutes_enabled) - show_minutes = true; } // Build output string on stack