From 1471494fac6e9051eee7b725f9109ea164f472cb Mon Sep 17 00:00:00 2001 From: andicoder <3944775+andicoder@users.noreply.github.com> Date: Tue, 16 Jun 2026 22:32:25 +0200 Subject: [PATCH] fix: re-send HA MQTT autodiscovery on every reconnect Discovery configs are published as retained messages and were sent only once, on the first connect after boot (guarded by lastMqttHassAutodiscoverySent==0). When the broker restarts without persistence, all retained messages are lost and the device never re-announces, so Home Assistant entities disappear until the device is rebooted. mqttReconnect() only runs on an actual (re)connect, so re-announcing unconditionally is idempotent and self-heals a broker restart. Co-Authored-By: Claude Opus 4.8 --- WAdapter/WNetwork.h | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/WAdapter/WNetwork.h b/WAdapter/WNetwork.h index 8d8b4b8..02346e7 100644 --- a/WAdapter/WNetwork.h +++ b/WAdapter/WNetwork.h @@ -183,7 +183,6 @@ class WNetwork { this->apStartedAt = 0; this->lastLoopLog = 0; #ifndef MINIMAL - this->lastMqttHassAutodiscoverySent = 0; this->mqttClient = nullptr; #endif @@ -1046,7 +1045,6 @@ void handleHttpFirmwareUpdateFinished(AsyncWebServerRequest *request) { #ifndef MINIMAL WAdapterMqtt *mqttClient; long lastMqttConnect; - unsigned long lastMqttHassAutodiscoverySent; #endif WProperty *ssid; WProperty *idx; @@ -1239,9 +1237,9 @@ void handleHttpFirmwareUpdateFinished(AsyncWebServerRequest *request) { notify(false); #ifndef MINIMAL - if (lastMqttHassAutodiscoverySent==0){ - if (sendMqttHassAutodiscover(false) ) lastMqttHassAutodiscoverySent=millis(); - } + // always re-announce HASS autodiscovery on (re)connect so a broker restart + // (which drops retained messages) self-heals without a device reboot + sendMqttHassAutodiscover(false); #endif return true; } else {