Skip to content

Fix MQTT discovery reliability for Home Assistant#24

Open
Abrechen2 wants to merge 1 commit intomainfrom
fix/mqtt-discovery-reliability
Open

Fix MQTT discovery reliability for Home Assistant#24
Abrechen2 wants to merge 1 commit intomainfrom
fix/mqtt-discovery-reliability

Conversation

@Abrechen2
Copy link
Copy Markdown
Owner

Summary

  • Remove device_class: "aqi" from AQI sensor discovery - causes HA compatibility issues where the entity stays unavailable after rediscovery
  • Track individual sensor discovery publish failures - publishDiscoverySensor now returns bool, publishDiscovery only marks complete when ALL sensors succeed, ensuring failed sensors are retried on the next loop cycle
  • Add periodic discovery re-publish every 60 minutes - if the MQTT broker restarts without persistent storage enabled, all retained discovery messages are lost and HA can no longer map incoming sensor data to entities. This periodic refresh ensures discovery configs are always present.

Background

When the MQTT broker restarts, retained discovery messages are lost. The device continues sending sensor data to tele/<topic>/state, but Home Assistant marks entities as unavailable / restored because it has no discovery config to map the data. Previously, discovery was only published on MQTT connect - if the TCP connection survived the broker restart, discovery was never re-sent.

Test plan

  • Flash updated firmware to one IKEA Air Monitor
  • Verify all 9 sensors appear in HA with correct values
  • Restart the MQTT broker and confirm sensors recover within 60 minutes without device reboot
  • Check serial debug output for "Periodic discovery re-publish" log messages
  • Verify AQI sensor works correctly without device_class

Generated with Claude Code

Three fixes to prevent sensors from becoming unavailable after broker
restarts or HA reboots:

1. Remove device_class "aqi" from AQI sensor - causes compatibility
   issues with some Home Assistant versions where the entity stays
   unavailable after discovery

2. Track individual discovery publish failures - publishDiscoverySensor
   now returns bool and publishDiscovery only sets discoveryPublished
   when ALL sensors succeed, ensuring failed sensors are retried

3. Add periodic discovery re-publish every 60 minutes - if the MQTT
   broker restarts without persistent storage, retained discovery
   messages are lost. This ensures they are refreshed regularly.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants