From 1c43b8a8745c2f0d5847933b8e309ef393497722 Mon Sep 17 00:00:00 2001 From: Edward Firmo <94725493+edwardtfn@users.noreply.github.com> Date: Wed, 24 Apr 2024 11:00:06 +0200 Subject: [PATCH] Display embedded temperature in F or C accordingly Partially solves #2063 --- esphome/nspanel_esphome_core.yaml | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/esphome/nspanel_esphome_core.yaml b/esphome/nspanel_esphome_core.yaml index e7bab57..6e75437 100644 --- a/esphome/nspanel_esphome_core.yaml +++ b/esphome/nspanel_esphome_core.yaml @@ -2120,15 +2120,17 @@ script: then: - lambda: |- if (id(embedded_indoor_temp) or !wifi_component->is_connected() or !api_server->is_connected()) { - float unit_based_temperature = id(temp_nspanel).state; - char buffer[15]; // Buffer for formatted temperature string - if ("${temp_units}"[0] == 'F' || "${temp_units}"[0] == 'f' || "${temp_units}"[1] == 'F' || "${temp_units}"[1] == 'f') { - unit_based_temperature = (unit_based_temperature * 9.0 / 5.0) + 32; // Convert to Fahrenheit if necessary - snprintf(buffer, sizeof(buffer), "%.0f${temp_units}", unit_based_temperature); // Fahrenheit with no decimal - } else { - snprintf(buffer, sizeof(buffer), "%.1f${temp_units}", unit_based_temperature); // Celsius with one decimal + const std::string temp_units = "${temp_units}"; + if (!temp_units.empty()) { + const char last_char = std::tolower(temp_units.back()); // Access the last character and convert to lowercase + char buffer[15]; // Buffer for formatted temperature string + if (last_char == 'f') { + snprintf(buffer, sizeof(buffer), "%.0f${temp_units}", (id(temp_nspanel).state * 9.0 / 5.0) + 32); // Fahrenheit with no decimal + } else { + snprintf(buffer, sizeof(buffer), "%.1f${temp_units}", id(temp_nspanel).state); // Celsius with one decimal + } + id(disp1)->set_component_text("home.indr_temp", adjustDecimalSeparator(buffer, id(mui_decimal_separator)).c_str()); } - id(disp1)->set_component_text("home.indr_temp", adjustDecimalSeparator(buffer, id(mui_decimal_separator)).c_str()); } - id: display_wrapped_text