Clean stale MQTT discovery entries
esphome_clean_mqttRemove stale MQTT discovery entries for an ESPHome device through Home Assistant's MQTT integration. Use when a renamed or deleted device leaves lingering retained topics.
Instructions
Remove stale MQTT discovery entries for an ESPHome device, through HA's own MQTT integration.
Skips with zero network calls when the config has no mqtt: section.
Otherwise resolves the node name (esphome.name, with substitutions:)
and mqtt.discovery_prefix (default "homeassistant"). With a matching
HA device, reads topics via WS mqtt/device/debug_info; otherwise
briefly mqtt/subscribes to {prefix}/+/{node_name}/# and collects
retained topics. Clears each topic via the mqtt.publish service,
payload="" and retain=True -- MQTT's own way to delete a retained
message. Never touches the ESPHome dashboard, which has no
MQTT-topic-clearing command.
Use when: a removed/renamed device's old MQTT discovery entries still
linger in Home Assistant.
Not for: a config with no mqtt: section; firmware artifacts -- use
esphome_compile_device.
Returns: {"device", "action": "clean_mqtt", "skipped": True, "reason": ...} with no mqtt: section; otherwise {"success": <bool>, "method": "debug_info"|"wildcard_subscribe", "topics_cleared": [...], "count": <int>}, plus "errors": [...] on partial failures.
Errors: {"error": "..."} for an invalid/missing device file, an
unresolvable esphome.name/mqtt.discovery_prefix, or a WebSocket/
service-call failure.
Limits: the wildcard fallback only collects for min(timeout, 3)
seconds -- may miss a slow message or catch an unrelated topic.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| name | Yes | Device name, with or without the '.yaml' suffix, e.g. 'kitchen_sensor'. | |
| timeout | No | Maximum seconds to spend collecting retained MQTT discovery topics via a wildcard subscribe, e.g. 60. Only used when no matching HA device is found for this config's node name. | |
| log_lines | No | Unused by this tool's HA-mqtt-integration implementation (kept only for API/parameter-surface compatibility with earlier versions that spawned a dashboard process and returned trailing log lines), e.g. 200. |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||