Detecting HomeKit mdns failures / device offline and automating recovery

One of the short comings of HomeKit is when it tells you something is offline it's 100% on you to fix it with very limited automation possiblities. In true Apple fashion the fix is typically super simple, restart a service somewhere and mdns will magically "fix it" and the device(s) will come online... until the next time it happens.

For me this is scrypted. It works 99.99% of the time, but every so often mdns goes wonky and i get a notice my doorbell is offline and i loose HomeKit recording until i restart scripted. Rinse, repeat until the next time it happens.

I recently ran into a mac app called homeclaw. No, not that one this one. It gives you scriptable command line access to HK devices and bridges you select, and it can be an MCP server too if you wish. There are a lot of possibilities with this app.

Of the possibilities the following command will use homeclaw to detect if a bridge and a device on the bridge is available. This can be scripted, habitat or any other HomeKit bridge or device. Launchd or crontab on a Mac (required) can be used to schedule it to run every few mins or as needed to poll the device for availability.

#!/bin/sh
vals=$(
/Applications/HomeClaw.app/Contents/MacOS/homeclaw-cli get "My Bridge" --json | jq -r '.reachable' && /Applications/HomeClaw.app/Contents/MacOS/homeclaw-cli get "My Device on My Bridge" --json | jq -r '.reachable, (.services[].characteristics[] | select(.name=="status_active") | .value)'
)
if echo "$vals" | grep -qxv 'true' >/dev/null; then
  curl -X POST http://ipaddress:port/api/token
fi

Should the bridge/device become unavailable the curl command can be used to trigger the exported HomeKit Bridge mode switch to restart the integration or trigger an automation to do addition checks (like verify your AP is up) before restarting the needed service or device.

For whomever finds this useful, may it bring a little more stability to your HomeKit environment.

I appreciate you posting this, but to be honest, my HomeKit/Apple Home environment is 100% rock solid/stable. I've been using Scrypted with my UniFi cameras for several years and I haven't had a single issue in all that time. It's always been odd to me how many people here post about HomeKit/Apple Home issues.

I totally agree, i know for me this script will run once, maybe twice a year. The only reason i dug that deep into the issue is cause murphy kicked my butt. I got the doorbell offline notice in HomeKit that my doorbell was offline and was "too busy" to do anything about it for too long, so i missed the footage of my porch pirate stealing my Amazon goodies. 24hrs of cloud recording goes by really fast when you are having a good time :joy: