Fix default hostname generation to support single-specifier Format() patterns (#24731)

- Route single-specifier hostnames through Format() so patterns like %NX (last N MAC hex chars) work correctly, while preserving the existing two-specifier %s-%04d behavior via snprintf
- Update comment in my_user_config.h to accurately describe chip ID rather than MAC
This commit is contained in:
Allen Schober
2026-05-11 11:03:27 +02:00
committed by GitHub
parent c7042c767c
commit b05c3004dd
2 changed files with 12 additions and 2 deletions
+1 -1
View File
@@ -95,7 +95,7 @@
#define WIFI_SCAN_AT_RESTART false // [SetOption56] Scan Wi-Fi network at restart for configured AP's
#define WIFI_SCAN_REGULARLY true // [SetOption57] Scan Wi-Fi network every 44 minutes for configured AP's
#define WIFI_NO_SLEEP false // [SetOption127] Sets Wifi in no-sleep mode which improves responsiveness on some routers
#define WIFI_DEFAULT_HOSTNAME "%s-%04d" // [Hostname] Expands to <MQTT_TOPIC>-<last 4 decimal chars of MAC address>
#define WIFI_DEFAULT_HOSTNAME "%s-%04d" // [Hostname] Expands to <MQTT_TOPIC>-<last 4 decimal chars of chip ID>
// -- Syslog --------------------------------------
#define SYS_LOG_HOST "" // [LogHost] (Linux) syslog host
+11 -1
View File
@@ -676,8 +676,18 @@ void setup(void) {
Format(TasmotaGlobal.mqtt_topic, SettingsText(SET_MQTT_TOPIC), sizeof(TasmotaGlobal.mqtt_topic));
if (strchr(SettingsText(SET_HOSTNAME), '%') != nullptr) {
// If hostname in Settings contains % (a format specifier), then reset hostname to WIFI_HOSTNAME from tasmota_globals.h
// and then expand the string.
SettingsUpdateText(SET_HOSTNAME, WIFI_HOSTNAME);
snprintf_P(TasmotaGlobal.hostname, sizeof(TasmotaGlobal.hostname)-1, SettingsText(SET_HOSTNAME), TasmotaGlobal.mqtt_topic, ESP_getChipId() & 0x1FFF);
const char* first_spec = strchr(SettingsText(SET_HOSTNAME), '%');
const char* second_spec = strchr(first_spec + 1, '%');
if (first_spec && second_spec) {
// Two (or more) specifiers: expands first as mqtt topic and second as chip ID
snprintf_P(TasmotaGlobal.hostname, sizeof(TasmotaGlobal.hostname)-1, SettingsText(SET_HOSTNAME), TasmotaGlobal.mqtt_topic, ESP_getChipId() & 0x1FFF);
} else {
// One specifier: use Format() which handles %NX = last N MAC hex chars, %Nd = short chip ID dec, %d = full chip ID dec
Format(TasmotaGlobal.hostname, SettingsText(SET_HOSTNAME), sizeof(TasmotaGlobal.hostname)-1);
}
} else {
snprintf_P(TasmotaGlobal.hostname, sizeof(TasmotaGlobal.hostname)-1, SettingsText(SET_HOSTNAME));
}