Integrations

MQTT

Spook adds an action to MQTT that finds the retained messages a removed device or integration left on your broker, and clears them, so MQTT discovery stops bringing old devices back.

The MQTT integration connects Home Assistant to an MQTT broker, like the Mosquitto broker app. Zigbee2MQTT, rtl_433, ESPHome and many more talk to Home Assistant through it, and with MQTT discovery, their devices show up on their own.

A broker can keep the last message of a topic: a retained message. Discovery depends on it. A device announces itself with a retained message, and the broker hands it to Home Assistant again every time it connects. The catch: a retained message stays until somebody clears it. Remove a device from Zigbee2MQTT, or stop using rtl_433, and what they retained stays on the broker. You delete the device in Home Assistant, restart, and there it is again.

Home Assistant can clear one topic, if you know which one, with an empty retained message from the mqtt.publish action. Nothing in Home Assistant tells you which topics still hold one. Spook adds an action that finds them under a topic filter, and clears them. An assistant with Spook's tools can do it too.

Actions

Spook adds one action to MQTT. You find it wherever you add an action, marked with a 👻. It is for administrators only: clearing what the broker keeps can remove devices from Home Assistant.

Clear retained messages

Finds the topics under a topic filter that hold a retained message, and clears each of them with an empty retained message, the way MQTT clears one. It goes through the connection Home Assistant already has to your broker.

A topic filter works as it does everywhere in MQTT: + stands for one level, and # at the end for every level below. zigbee2mqtt/old_sensor/# is everything of the device old_sensor, and the topic zigbee2mqtt/old_sensor itself too. Give one filter, or a list.

How it works: the action subscribes to the filters, and the broker sends what it retains for them straight away, each message flagged as retained. The action listens for a short while, 2 seconds unless you set Wait, and unsubscribes again. Then it clears what it found. Only a message the broker sends as retained when the action subscribes picks a topic. A message a device publishes during the wait is not flagged as retained, so it does not add its topic, nor does it keep a topic that was found from being cleared.

After clearing, the action asks the broker once more what it retains under the filters. A topic that still holds a message was not cleared: the broker did not let Home Assistant clear it, or a device published it again. The action then fails, and says which. A broker that does not answer at all sends nothing, so the action finds nothing and clears nothing.

Clearing a discovery topic removes that entity or device from Home Assistant. That is what you want for a device that is gone, and a surprise for one that is not. Try a filter with Dry run first. It finds the topics, clears nothing, and the response lists what it would have cleared.

Some things it does not do:

  • A filter of only wildcards, like # or +/#, is refused. It would clear what every device and program on the broker retains.
  • A filter that covers all of MQTT discovery, like homeassistant/#, homeassistant/+/+/config or +/#, is refused. It would remove every device MQTT discovery added. A filter for one device, like homeassistant/+/old_sensor/#, is fine. The action reads your discovery prefix from the MQTT options.
  • A filter for the broker's own topics, starting with $ like $SYS/#, is refused. A broker does not let anyone clear those.
  • Home Assistant's own birth and will topics, homeassistant/status unless you changed them in the MQTT options, are never cleared, also when a filter matches them. Other integrations read them to know whether Home Assistant is online. The response lists them under skipped.
  • When Home Assistant is not connected to the broker, or MQTT is not set up, the action refuses.

How to use it

  1. Open the automation or script that should clean up, and select Add action.
  2. Search for retained, and select MQTT: Clear retained messages 👻.
  3. Fill in Topic filter, like zigbee2mqtt/old_sensor/#.
  4. Turn on Dry run the first time, to see what it finds.
  5. Select Save.

Just want to see it work? Try this action opens it in your Home Assistant, ready to run once. Mind you: without Dry run, running it clears the messages for real.

Use cases

What people use this for:

  • Clear what Zigbee2MQTT left on the broker for a device that is gone, so it stops coming back after every restart.
  • Remove the discovery messages of an integration or tool you stopped using, like rtl_433, in one go.
  • See which topics under a prefix still hold a retained message, with a dry run.

Missing something here?

An action, an entity or a repair this integration should have? Say so. Stuck, or found a bug? There is help.