diff --git a/gaugemqttcontinuous.py b/gaugemqttcontinuous.py index 1576f08..093d812 100644 --- a/gaugemqttcontinuous.py +++ b/gaugemqttcontinuous.py @@ -667,9 +667,10 @@ def publish_discovery(client): ) info(f"Discovery: gauge {i} ({g['name']})") - # Process MQTT messages between publishes - client.check_msg() - utime.sleep_ms(30) + # Process MQTT messages between gauges + for _ in range(5): + client.check_msg() + utime.sleep_ms(10) client.publish( gt["led_red_disc"], @@ -771,6 +772,11 @@ def publish_discovery(client): ) info(f"Discovery: gauge {i} status green") + # Process between gauges to avoid MQTT blocking + for _ in range(5): + client.check_msg() + utime.sleep_ms(10) + def publish_state(client): for i, g in enumerate(gauge_objects): @@ -794,14 +800,17 @@ def main(): connect_wifi(WIFI_SSID, WIFI_PASSWORD) # Check if we need to publish discovery + discovery_published = False try: with open(".discovery_ok", "r") as f: - discovery_published = f.read() - info("Discovery flag file exists, skipping discovery") - discovery_published = True + content = f.read() + if content.strip() == "ok": + discovery_published = True + info("Discovery flag exists - skipping discovery") except: - info("No discovery flag file - publishing discovery") - discovery_published = False + info("No discovery flag - will publish discovery") + + info(f"Discovery: {discovery_published}") # Connect MQTT first (needed for discovery) connect_mqtt()