Skip to content

Fix MQTT last will on graceful disconnect#12133

Closed
OttoWinter wants to merge 1 commit intohome-assistant:devfrom
OttoWinter:fix-mqtt-last-will
Closed

Fix MQTT last will on graceful disconnect#12133
OttoWinter wants to merge 1 commit intohome-assistant:devfrom
OttoWinter:fix-mqtt-last-will

Conversation

@OttoWinter
Copy link
Copy Markdown
Member

@OttoWinter OttoWinter commented Feb 2, 2018

Description:

See linked issues. In a nutshell, the MQTT client only sends the last will message if the connection is dropped, but not on a graceful disconnect.

  • Expected behavior: The MQTT client also sends the last will message on a "graceful" disconnect / Home Assistant shutdown.
  • Actual behavior: The MQTT client only sends the last will message on a connection drop.

Note: There's a point to be made in favor of the current behavior. Sometimes we want to know when a disconnect was "ungraceful". I would say though that in most cases, the last will message is used to determine whether Home Assistant is online or offline (even the docs are using it this way)

Related issue (if applicable): fixes #6240, fixes #9017

Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.io#4566

Example entry for configuration.yaml (if applicable):

mqtt:
  birth_message:
    topic: 'hass/status'
    payload: 'online'
  will_message:
    topic: 'hass/status'
    payload: 'offline'

Checklist:

  • The code change is tested and works locally.

If user exposed functionality or configuration variables are added/changed:

If the code does not interact with devices:

  • Local tests with tox run successfully. Your PR cannot be merged unless tests pass
  • Tests have been added to verify that the new code works.

@balloob
Copy link
Copy Markdown
Member

balloob commented Feb 2, 2018

There's a point to be made in favor of the current behavior. Sometimes we want to know when a disconnect was "ungraceful".

You made the right observation.

https://www.hivemq.com/blog/mqtt-essentials-part-9-last-will-and-testament states:

The Last Will and Testament (LWT) feature is used in MQTT to notify other clients about an ungracefully disconnected client.

So last will is only for ungracefully disconnections. I think an update to the docs is appropriate.

I think that alway sending a message can already be done today by having an automation listen for Home Assistant shutting down?

Copy link
Copy Markdown
Member

@balloob balloob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Home Assistant is the foundation for others to build upon, we need to follow the spec to the letter. Anytime we diverge, we are causing confusion and problems down the line.

@OttoWinter
Copy link
Copy Markdown
Member Author

Sorry for the late reply, just lots of stuff going on right now. Anyway... I agree Home Assistant should remain free of unexpected behavior and I'm happy this project choses this path since many other projects have failed because their custom unexpected behavior accumulated over time.

So, to still address the underlying problem, as you hinted at, it would be best to update the docs instead. But while doing so, I noticed that manually publishing a MQTT message on homeassistant.shutdown unfortunately doesn't seem to work (my automation config is attached). I'm still debugging the issue, but most probably the MQTT client shuts down before the automation component does. Either way I'm closing this PR right now since code probably doesn't need to be updated (further discussion therefore in home-assistant/home-assistant.io#4566)

- alias: MQTT last will
  id: [...]
  trigger:
  - event: shutdown
    platform: homeassistant
  action:
  - service: mqtt.publish
    data:
      topic: hass/status
      payload: offline

(Unrelated, but is there any update on #12004? I would like to have that issue fixed 😀)

@OttoWinter OttoWinter closed this Feb 5, 2018
@OttoWinter OttoWinter deleted the fix-mqtt-last-will branch March 13, 2018 20:18
@home-assistant home-assistant locked and limited conversation to collaborators Jul 26, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mqtt message not being published on platform home assistant event shutdown MQTT last will doesn't work

3 participants