Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HA discovery: expire discovery topics after 30 minutes, re-publish them in regular intervals #79

Closed
wants to merge 1 commit into from

Conversation

b3nn0
Copy link
Contributor

@b3nn0 b3nn0 commented Nov 2, 2023

Hi,
this is a small improvement on the HA discovery implementation.
Right now, the discovery topics are "retain"ed. This is needed, so that HA still finds them after a restart.
However, it has the drawback, that these topics will never ever be deleted, e.g. when the parameters.json is updated, the device is renamed, etc.
In cases like that, users need to manually delete the leftover discovery topics.

This PR switches to MQTT v5, which allows setting an expiry timeout for publishes, and does just that:

  • Discovery topics are retained only for 1800s (30 minutes)
  • ism7mqtt will re-publish the discovery messages every 1700s to renew the expiry.

This way, HA will always find the topics as long as ism7mqtt is running/was running recently, but when topics disappear, HA will pick that up after some time and remove the stale entities.

Again, feel free to do coding style changes if needed - not really a C# expert.

@zivillian
Copy link
Owner

Thank you very much. Please forgive me if it takes a few days to review...

@zivillian
Copy link
Owner

Are you sure, that this works? Wikipedia states, that v5 introduced message expiry for undelivered messages

Message expiry: Messages can include an expiry date and are deleted if they are not delivered within this time period.

but when HA reads the messages it's delivered so no longer available or retained. If I'm correct this also means, that you can restart HA only once every thirty minutes.

@b3nn0
Copy link
Contributor Author

b3nn0 commented Nov 4, 2023

Yes, I was confused by this too, but tested it for the last few days.
The expiry seems to do two things: for persistent sessions, it decides how long to keep stuff in the queue if the client is offline, but it also decides how long retained messages should be retained.
At least mosquito handles it that way.

As HA doesn't use a persistent session, all info about the client is discarded when HA restarts, and retained messages will be redelivered.

@b3nn0
Copy link
Contributor Author

b3nn0 commented Jan 17, 2024

FYI: Home assistant seems to have updated its documentation on how to deal with the issue of left-over devices that don't exist any more when using retained messages:
https://www.home-assistant.io/integrations/mqtt#how-to-use-discovery-messages

They now recommend using HAs birth message to re-trigger discovery topics. I think it's a nicer solution, too. And doesn't require MQTT v5.

Would it be merged if I create a PR doing that?

@b3nn0
Copy link
Contributor Author

b3nn0 commented Jan 17, 2024

Closed in favor of #94

@b3nn0 b3nn0 closed this Jan 17, 2024
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.

None yet

2 participants