diff --git a/esphome/nspanel_esphome_core.yaml b/esphome/nspanel_esphome_core.yaml index 282b9b6..54bac1b 100644 --- a/esphome/nspanel_esphome_core.yaml +++ b/esphome/nspanel_esphome_core.yaml @@ -1713,28 +1713,33 @@ text_sensor: internal: false disabled_by_default: false lambda: |- - #ifdef NSPANEL_HA_BLUEPRINT_PREBUILT - std::string suffix; - uint8_t mac[6] = {0,0,0,0,0,0}; - esp_read_mac(mac, ESP_MAC_WIFI_STA); - for (int i = 3; i < 6; ++i) { - char hex[3]; - snprintf(hex, sizeof(hex), "%02X", mac[i]); - suffix += hex; - } - return {"${name}" + "-" + suffix}; - #else return {"${name}"}; - #endif filters: - lambda: |- + #ifdef NSPANEL_HA_BLUEPRINT_PREBUILT + std::string suffix = "00ERROR"; // Default suffix in case of an error + uint8_t mac[6] = {0,0,0,0,0,0}; + if (esp_read_mac(mac, ESP_MAC_WIFI_STA) == ESP_OK) { + suffix.clear(); // Clear the default error suffix + for (int i = 3; i < 6; ++i) { // Use last 3 bytes of MAC + char hex[3]; + snprintf(hex, sizeof(hex), "%02X", mac[i]); + suffix += hex; + } + } + // Proceed with suffix (either MAC-based or default error indicator) + const std::string raw_name = (x + "-" + suffix); + #else + const std::string raw_name = x; + #endif + std::string result; bool last_was_underscore = false; - for (char& c : x) { + for (char& c : raw_name) { if (isalnum(c)) { - result += tolower(c); // Add alphanumeric characters as lowercase + result += tolower(c); // Add alphanumeric characters as lowercase last_was_underscore = false; - } else if (!last_was_underscore) { // Replace non-alphanumeric with '_' but avoid consecutive '_' + } else if (!last_was_underscore) { // Replace non-alphanumeric with '_' but avoid consecutive '_' result += '_'; last_was_underscore = true; }