MQTT
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/+/+/configor+/#, is refused. It would remove every device MQTT discovery added. A filter for one device, likehomeassistant/+/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/statusunless 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 underskipped. - When Home Assistant is not connected to the broker, or MQTT is not set up, the action refuses.
How to use it
- Open the automation or script that should clean up, and select Add action.
- Search for retained, and select MQTT: Clear retained messages 👻.
- Fill in Topic filter, like
zigbee2mqtt/old_sensor/#. - Turn on Dry run the first time, to see what it finds.
- 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.
| Action properties | |
|---|---|
| Action | MQTT: Clear retained messages 👻 |
| Action name | mqtt.clear_retained |
| Action targets | No targets |
| Action response | Optional, the topics found, and whether they were cleared |
| Spook's influence | Newly added action |
| Tools | Try this action |
| Action data parameters | |||
|---|---|---|---|
| Attribute | Type | Required | Default / Example |
topic | string | list of strings | Yes | zigbee2mqtt/old_sensor/# |
wait | string | No | 00:00:02 |
dry_run | boolean | No | false |
wait is how long to listen after the broker confirmed the subscription, at most 30 seconds. A broker sends everything it retains for a filter right after it confirms the subscription, one message after the other, but nothing marks the last one. Thousands of messages from a broker on your own network take well under a second, so the default leaves room. A slow broker, or one far away, can use a little more. Give it in hours, minutes and seconds like 00:00:05, or as seconds: 5 in YAML.
The response:
| Attribute | Type | Description |
|---|---|---|
topics | list | The topics that held a retained message, sorted. Cleared, or with a dry run the ones that would be. |
cleared | boolean | Whether they were cleared, and checked to be gone: false on a dry run. |
skipped | list | Home Assistant's own birth and will topics the filters matched, left alone. |
You removed the sensor old_sensor from Zigbee2MQTT, but it keeps coming back. Two places hold what it left: its state and availability under zigbee2mqtt/old_sensor, and its discovery messages under the discovery prefix. Zigbee2MQTT names those after the IEEE address of the device, like 0x00158d0001a2b3c4, which its page in Zigbee2MQTT shows. See what is there first, with a dry run:
action: mqtt.clear_retained
data:
topic:
- "zigbee2mqtt/old_sensor/#"
- "homeassistant/+/0x00158d0001a2b3c4/#"
dry_run: true
response_variable: found
The response:
topics:
- homeassistant/sensor/0x00158d0001a2b3c4/battery/config
- homeassistant/sensor/0x00158d0001a2b3c4/temperature/config
- zigbee2mqtt/old_sensor
- zigbee2mqtt/old_sensor/availability
cleared: false
skipped: []
Those are all of that device. The same call without dry_run clears them, and the device stays gone.
You stopped using rtl_433, and the weather stations of the neighbours keep coming back. Its discovery messages are under the discovery prefix, one level per device, named after the device. See what is there first, with a dry run:
action: mqtt.clear_retained
data:
topic:
- "homeassistant/+/Acurite-Tower-A-1234/#"
- "homeassistant/+/Oregon-THGR122N-1-55/#"
dry_run: true
response_variable: found
The response:
topics:
- homeassistant/sensor/Acurite-Tower-A-1234/Acurite-Tower-A-1234-H/config
- homeassistant/sensor/Acurite-Tower-A-1234/Acurite-Tower-A-1234-T/config
- homeassistant/sensor/Oregon-THGR122N-1-55/Oregon-THGR122N-1-55-T/config
cleared: false
skipped: []
Those are the ones. The same call without dry_run clears them, and their sensors leave Home Assistant.
A script that lists the topics with a retained message under a prefix, in a notification, and clears nothing:
sequence:
- action: mqtt.clear_retained
data:
topic: "{{ prefix }}/#"
dry_run: true
response_variable: found
- action: notify.persistent_notification
data:
title: "Retained under {{ prefix }}"
message: >-
{{ found.topics | join('\n') or 'Nothing retained.' }}
fields:
prefix:
name: Prefix
example: zigbee2mqtt
selector:
text:
- A device that is still around puts a retained message back the next time it publishes one, like when its state changes or it starts. Clearing only sticks for what is gone.
- Clearing a discovery topic removes that entity or device from Home Assistant. Use Dry run first.
- A broad filter on a bridge, like
zigbee2mqtt/#, also clears the bridge's own topics, likezigbee2mqtt/bridge/state. Its entities then show as unavailable until the bridge restarts. Aim at the topics of one device, likezigbee2mqtt/old_sensor/#. - The action finds what the broker sends while it listens. A broker that takes longer than Wait leaves topics out. Run it again, or give it more time.
- Your broker decides who may clear what. A topic the MQTT user of Home Assistant may not publish to stays as it is. The broker does not always say so, but the check after clearing finds it, and the action fails.
- This takes an admin when a user calls it. Automations and scripts are not affected.
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.
Tag
Spook adds actions to create, change and delete the NFC tags and QR codes Home Assistant knows, from your automations and scripts, without a trip to the Tags page.
Logbook
Spook adds an action that hands an automation what the logbook shows, like who unlocked the front door in the last hour, with who or what caused each entry.
