Re-paired device stays unavailable in Home Assistant until Z2M restart (availability never published after remove)

Open Beginner friendly
#33,855 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
90/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
typescript
Domain
backend

Research direction

Start with lib/extension/availability.ts, especially the device removal and availability-publication paths, then inspect test/extensions/availability.test.ts for existing availability coverage. Reproduce the remove-and-re-pair sequence with a test that expects the device's retained availability state to be published as online again. The work is done when the regression test passes, the availability behavior is restored without a Z2M restart, and the project checks pass.

Written by the indexing model from the issue text.

Description

What happened?

I removed a contact sensor (SNZB-04P) from Z2M and paired the same device again without restarting Z2M. Pairing and interview went fine and the device kept sending contact/tamper/battery updates, but all of its entities in Home Assistant stayed unavailable.

Checking the MQTT debug info in HA, zigbee2mqtt/0xa4c138121812ffff/availability had no message at all. The removal clears the retained topic, and after the device joined again nothing was published there. Another device (Aqara MCCGQ12LM) that I paired a few minutes earlier got {"state":"online"} right away. It had not been in Z2M before.

After restarting the add-on, publishAvailabilityForAllEntities() published online and the sensor showed up correctly in HA.

Looking at lib/extension/availability.ts, publishAvailability() skips publishing when lastPublishedAvailabilities already holds the same value. When a device is removed, clearDevice() clears the timer and the debouncer but keeps that entry. So the re-paired device, which was online before removal, is never published as online again until a restart.

What did you expect to happen?

After removing a device and pairing it again, <device>/availability should be published as online once the device is seen, the same as for a new device.

How to reproduce it (minimal and precise)
  1. Enable availability. Have a device that is currently online.
  2. Remove it (frontend, or zigbee2mqtt/bridge/request/device/remove).
  3. Pair the same device again, without restarting Z2M.
  4. zigbee2mqtt/<ieee>/availability stays empty, so entities in HA that use it remain unavailable.

I have a small fix with a test for this: clearDevice() also deletes the device from lastPublishedAvailabilities. Two lines in availability.ts plus a test in test/extensions/availability.test.ts that fails before the change. The full test suite and pnpm run check pass. I used an AI assistant to help find the cause and write the fix and test, and I've reviewed and tested it myself. Would a PR for this be welcome?

Zigbee2MQTT version

2.14.2 (the same code is on dev at 3a0a038)

Adapter firmware version

20210708

Adapter

ZStack3x0

Setup

Home Assistant OS add-on

Dominant language
TypeScript
Stars
15.9k
Forks
2.1k
Avg merge
13h 22m
Merged PRs (30d)
37

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Koenkk/zigbee2mqtt

All issues in Koenkk/zigbee2mqtt

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.