DMBlakeley/homebridge-awair2

Awair Homekit Room reset when Homebridge restarts

Closed this issue · 28 comments

When restarting Homebridge server my Awair Element room is reset to the room the Homebridge is assigned to in Homekit. Behavior may not occur every restart.

Example:

  1. Homebridge assigned to Garage room
  2. Awair Element located in Master Bedroom
  3. Restart Homebridge server
  4. Go into Homekit and see Awair is now assigned to Garage

Hi Ryan,

Devices assigned to rooms in Homebridge are not mirrored to same named rooms in HomeKit except for the location of the bridge.

In your case you but the Homebridge in the "garage" room. When the Homebridge is added to HomeKit the bridge and the attached devices are all added to the HomeKit "garage" room. You need to change the room for your Awair Element to "master bedroom" within HomeKit (open the device and select the settings gear to change). Note that if your make use of the Display or LED modes for the Awair Element these also need to be changed to the "master bedroom".

Hope this helps. If not please don't hesitate to reach out again.

Regards, Doug

Closing for now. Please resubmit if additional support needed.

Thanks for replying to this issue and all the work you have put into this plugin. The plug-in is great and I know in the grand scheme of things this issue is small potatoes.

Yes, when a device is first added to homebridge it is assigned to the same room in homekit as the homebridge itself (garage). You can then change the room in homekit to the room the device is actually in (as you describe). On subsequent restarts of homebridge my other homebridge devices stay in their homekit assigned room. My Awair is reset to garage.

You have me a bit stumped with your issue. The Awair plugin as well as Homebridge do not define the HomeKit rooms. It's almost seems as Homebridge is reinitializing the Awair devices on Homebridge reboot HomeKit sees them as new devices putting them initially puts them in their default room. Puzzling.

Can you let me know:

  • What platform and OS version are you running Homebridge on.
  • Homebridge version
  • Awair plugin version
  • npm version
  • node.js version

Fully aware (heh!) that it could be something to do with my particular system. Also frustrating that it doesn’t happen every time homebridge restarts. Unfortunately, I haven’t been able to nail down a solid occurrence pattern. It might only happen when the machine is restarted.

Here are the deets:

  • MacOS Monterey (although this happened on Big Sur too)
  • Homebridge 1.3.8
  • Homebridge Awair2 5.8.12
  • npm 6.14.9
  • Node 12.16.2

I am also running Homebridge on a Mac ... Mac Mini M1 with macOS Monterey. Homebridge and Awair plug-in are at the same version. All of my Awair devices are in the Default Homebridge room. I haven't found any real benefit to setting up rooms in Homebridge as my primary access is through HomeKit. The only additional plug-in that I have running is for MyQ. What other plug-ins are you running?

I am using Node v16.13.0 and npm v8.1.0 which are much new versions. I tend to update Node and let it install npm although there may be new versions. Easiest way to do the update is through the hb-service.

Your problem almost seems like Homebridge is reinitializing the Awair plug-in and putting your devices in your default room. HomeKit in turn sees as new devices. May be a device cache issue. You can see the cached devices on the Homebridge Settings page.

If all else fails I would suggest that you uninstall the Awair plug-in, reboot Homebridge to clear the devices from the cache, and reinstall. Make sure that you copy your developer token before uninstalling. You can also copy the config.sys information and then past it back in after reloading. Easiest way in going to the plug-in page, select the wrench icon on the Awair plug-in, select </> JSON Config, highlight text, and save to Notes for reuse.

Interesting but frustrating problem!

Closing for now. Will continue discussion as needed.

Looping back to this after months. This week I followed your instructions above and they failed to fix the issue. My power just went out and came back on and the Awair was reset to the default Homebridge room. This is really frustrating because any automatons I setup involving the Awair sensors are lost when the Awair is reassigned to the default Homebridge room.

I am now trying to put Awair in a child bridge and assigning the default room for the child bridge to the MBR where the Awair actually lives.

Hi Ryan,

I still do not believe that your problem is on the Homebridge / Awair plug-in side of things. My thinking is as follows ...

Homebridge appears to HomeKit as a bridge which connects non-native accessories. This is not too dissimilar to Philips hue where there is a bridge which connects lighting products to HomeKit. You can see this when you select the "Home" icon in the upper left hand corner of the Home App, then "Home Settings", and finally "Home Hubs & Bridges".

When you do an initial setup of Homebridge each of the accessories are discovered and you have the option to define the room for the accessory as well as icons and automations. Rooms and automations are defined in the Home App, not in Homebridge. Putting Awair in a child bridge really does not change this.

My understanding is that Home configuration data is saved in your iCloud account. To my knowledge there is no "Apple" was to create a HomeKit backup. There are a couple of third party apps, "Controller for HomeKit" for example, that allow you to make a backup. In this way you are always protected for the unexpected.

Questions that come to mind:

  • What are the OS versions on your Mac for Homebridge, iPhone and iPad? There were recent updates for the iOS family to 15.5 and MacOS is at 12.4.
  • Do you have Home selected for iCloud storage? On your iPhone -> Settings, Apple ID, iCloud and check that Home is selected. Make you have sufficient iCloud storage. Also check on your Mac(s).
  • What are using for your Home hub (Apple TV, HomePod or iPad)? What version of tvOS are you using on you Apple TV or HomePod?

Personally I have had more issues with network configuration and stability with respect to HomeKit than anything else. When you have a power failure the modem, router(s) and devices all need to power up and then reconnect. At this point HomeKit re-establishes connections. You may also want to check your router settings to make sure that you don't have (stale) multiple entries for Homebridge or other devices. I have set up fixed IPs for a number of devices including the Mac Mini that I am using for Homebridge.

My thoughts for now. Curious if this triggers any thoughts on your side.

Regards, Doug

Doug,

The reason I suspect the Awair plugin is because no other plugin I have installed exhibits this behavior (Nest, Wyze, Shelly). That being said, I could also see this being a caching issue on the Homekit/iCloud side of things.

In Homekit a bridge is also assigned a default room. My thinking with creating a child bridge is that the Awair plugin is getting re-initialized in homekit and being assigned to the default room of its bridge. My main Homebridge bridge is located in my Garage. I've assigned the child/Awair bridge to my MBR. In this way I hope that if the Awair is reinitialized it will be placed in the default room of the child/Awair bridge instead of the main bridge that it was previously on.

OS Versions
MacOS 12.4
iOS 15.5

Homekit Hub(s)
AppleTV 4K (2nd Gen) - tvOS 15.5
HomePod Minis (4) - All version 15.5

I do have home selected for iCloud storage and I have 140/200GB available.

I do have network issues for some things with Homekit (particularly first get Leviton light switches) when the power fails but generally not too much else. I don't believe I have a fixed IP for the Mac mini but I have not had issues with the IP changing.

Thanks,
Ryan

Ryan,

Thanks for the additional details. There must be something I do not have implemented correctly with respect to device caching for the plug-in. On Homebridge reboot your Awair device is being reinitialized as a new device and appearing in your default room.

Please let me know if the child bridge corrects the issue for you.

Thanks,
Doug

Ryan,

A couple more questions so that I can simulate your setup:

  • Do you lose the Awair Homebridge configuration on Homebride restart, power fail, or both?
  • You have an Awair Element. Are you making use of the Display Mode switches? Are you making use of the Total VOC and PM2.5 binary switches?
  • If you don't mind could you turn on Logging on the Awair plugin (verbose not necessary), restart Homebridge, and send me your log from reboot ("Got SIGTERM") to end of initial boot. Most interested in the messages from the Awair plugin if you feel the need to filter.

Thanks,
Doug

Doug,

Unfortunately, I haven't yet been able to nail down when exactly it decides to reset. My Mac mini restarts once a week and sometimes it happens then. If I just restart HB it generally does not happen but sometimes does. It did happen just recently with the power failure. Sorry I know very frustrating when you are trying to recreate the issue to not know the exact circumstances under which the issue is occurring.

I don't believe I'm making use of those binary switches. I don't have any switches exposed in Homekit from the Awair. I just have the humidity, IAQ, and temp exposed as sensors in HK.

[5/24/2022, 10:54:08 AM] ---
[5/24/2022, 10:54:09 AM] Loaded plugin: homebridge-awair2@5.9.5
[5/24/2022, 10:54:09 AM] Registering platform 'homebridge-awair2.Awair2'
[5/24/2022, 10:54:10 AM] [Awair2] Initializing Awair2 platform...
[5/24/2022, 10:54:10 AM] [Awair2] Initializing child bridge XX:XX:XX:XX:XX:XX
[5/24/2022, 10:54:10 AM] [homebridge-awair2] Launched child bridge with PID 77927
[5/24/2022, 10:54:10 AM] Registering platform 'homebridge-awair2.Awair2'
[5/24/2022, 10:54:10 AM] [homebridge-awair2] Loaded homebridge-awair2 v5.9.5 child bridge successfully
[5/24/2022, 10:54:10 AM] Loaded 1 cached accessories from cachedAccessories.XXXXXXXXXXXX.
[5/24/2022, 10:54:10 AM] [homebridge-awair2] Configuring cached accessory Jasper’s Awair
[5/24/2022, 10:54:10 AM] [homebridge-awair2] homebridge-awair2 platform didFinishLaunching
[5/24/2022, 10:54:10 AM] Homebridge v1.4.1 (HAP v0.10.2) (homebridge-awair2) is running on port 41908.
[5/24/2022, 10:54:11 AM] [homebridge-awair2] getAwairDevices: number discovered: 1
[5/24/2022, 10:54:11 AM] [homebridge-awair2] [70886B140C0D] Initializing platform accessory Jasper’s Awair...
[5/24/2022, 10:54:11 AM] [homebridge-awair2] [70886B140C0D] awair-element_XXX IAQ accessory exists, using data from cache
[5/24/2022, 10:54:11 AM] [homebridge-awair2] [70886B140C0D] Getting initial IAQ status for awair-element_XXX
[5/24/2022, 10:54:11 AM] [homebridge-awair2] [70886B140C0D] VOC: (213.8 ppb) => TVOC: (642.1616895144947 ug/m^3)
[5/24/2022, 10:54:11 AM] [homebridge-awair2] [70886B140C0D] CO2 NORMAL: 536.32 < 800
[5/24/2022, 10:54:11 AM] [homebridge-awair2] [70886B140C0D] PM2.5: 3.64 ug/m^3)

I tried to obscure anything remotely identifying with Xs. The cached accessory that is loaded has the same MAC address as the child bridge toward the top.

Cheers,
Ryan

Ryan,

I have carefully gone back through the code, did some housekeeping, and added a bit of additional logging to make sure that boot up is going as expected. I have not found any issue that could explain what you observed unless the home bridge cash is being corrupted in some manner. I do have my MacMini running Homebridge connected to a UPS so it never really is powered off suddenly unless there is an extended power outage.

Have you experienced any additional issues?

Regards, Doug

I should have asked what other plugins are you running? I'm researching the if homebridge, HAP-NodeJS or other plugins may be causing cache corruption.

One other suggestion. In the Homebridge settings select the Network Interface that is being used by your Mac Mini and select Ciao as your network interface. Setting the network interface keeps Homebridge from searching through the options at boot. Ciao is also supposed to be a more efficient mDNS Advertiser. When I set both, Homebridge seemed to boot much more quickly.

Ran a number of scenarios with/without switches, adding/subtracting, ran dozens of reboots all with the Awair plug-in starting up as expected. v5.9.6 has now been posted with the housekeeping updates.

Thanks,
Doug

Hi Doug,

As luck (or the opposite of luck) would have it my power blinked off again today. I can confirm that placing the awair in a child bridge fixed the room placement issue, but I can tell it was reinitialized because the automations I created based on the Awair were deleted.

Plugins I'm Running

  • HB UI
  • Awair2
  • Nest
  • Shelly
  • Wyze Connected Home v3

I have Ciao as the network interface.

I also just updated the Awair plugin.

Thanks,
Ryan

Hi Ryan,

Curious what automations that you running for your Awair device. Do you have automations for the other plugins?

Thanks,
Doug

Doug,

I have two automations in the home app based on the awair temp sensor.

When the temp rises above 76 I turn a fan on.
When the temp drops below 75 I turn the fan off.

I have plenty of other automations but only a few based on homebridge devices. I turn the nest thermostat off of eco mode when I return home because the nest app was becoming unreliable at doing that itself.

Cheers,
Ryan

Hi Ryan,

Will look at this during the coming week to see if I can determine what may be corrupting your homebridge cache based on your plugins and automations.

In the meantime, please take a look at this post from @adriancable describing a very similar situation to yours. I believe that Adrian is stating the same as I am but maybe in clearer terms.

chrisjshull/homebridge-nest#544

Thanks,
Doug

Hi Ryan,

I have taken a look at Nest (link provided above), Shelly and Wyze. As described by @adriancable, if the contents of the ~/.homebridge/persist directory are lost then devices are considered new.

Of the 3 plugins, Shelly is the most suspect with 163 open issues. Some are feature requests and other just need to be cleared out by the author as they are non-issues. Shelly seams to cover so many devices I think it is possible to have a configuration error. What devices do you have that are dependent on Shelly?

Thanks,
Doug

Hi @DMBlakeley, @ryan86 et al. I came here since I'm the author of the Nest plug-in, so thought I would throw in my 2 cents. (I'm also the developer of a commercial HomeKit product, so I think I have a reasonable amount of experience of what can go wrong with HomeKit and why.)

The UUID cache not being persisted in ~/.homebridge/persist across restarts is really the only likely explanation. You need to figure out why this occurring in your specific situation. My educated guess since this seems to happen when you lose power is that aggressive write caching on whatever machine is running Homebridge is causing the update to the UUID cache not to actually be flushed to the storage. Maybe the Awair plug-in is the last to be loaded by Homebridge, so everything else does get flushed apart from that one. Who knows, but I think it is unlikely that your issue is caused by a bug in any specific plug-in (including Awair), or even by Homebridge itself.

If you regularly experience power outages and this is causing filesystem corruption, then I would install a UPS on machines that are sensitive to this, since one day you might experience something a lot worse than losing your HomeKit room. You could also try some 'softer' solutions e.g. if the machine is Linux based, a cron script that runs every hour (or even every minute) and runs sync to flush.

@DMBlakeley - looking quickly through your code, the only thing I would check on your side is that deviceUUID in DeviceConfig really is stable over time. I don't use Awair so have no way of verifying this myself. If you don't already do this, you could log the deviceUUIDs to the console on start-up and then ask @ryan86 to confirm those stay the same across restarts, even when his problem occurs. Once you have verified that, you can I think claim with near 100% confidence that your plug-in is not responsible for this.

Hi @adriancable, thanks for the additional insight and suggestions, very much appreciated.

The deviceUUID referred to in the DeviceConfig is the value provided for a registered device from the Awair account. For @ryan86, this will be of the format awair-element_XXX. This in turn is used to generate the UUID used by Homebridge. As the deviceUUID is consistent, the generated UUID is consistent. (I added some logging in my local version to confirm.) If there was a problem reading the device details from the Awair account there would be an error logged but this does not appear to be the case.

In @ryan86 case, it also appears to me that the UUID cache is being corrupted in some manner. I have Homebridge running on a MacMini powered via a UPS so I do not experience a power failure. I am a little hesitant to take the MacMini off the UPS to see if UUID cache corruption occurs on power failure.

I am at a bit of a loss on how to help @ryan86 find the root cause. I will release an update with the additional logging to see if provide any additional insight.

Thanks again,
Doug

I made a couple more updates today. No functionality changes but checks for device existence were based on a mix of deviceUUID and serial. All checks are now based on deviceUUID.

If Awair2 logging is enable, the deviceUUID and UUID will be logged. @ryan86, if you find that the UUID before a cache issue is the same as after, then the Awair2 plugin is acting consistently and this is an indication of the cache being corrupted through another means.

If the UUID changes, please let me know and please provide the logs that were generated on reboot. This would be a great help.

Thanks,
Doug

@ryan86, last evening I was experimenting with a new plugin and in the process of adding and removing the plugin the cache was corrupted a couple of my Awair devices were removed and then added as new on next restart of Homebridge. In this case, the plugin did not use the config-ui interface and I was updating the config.json manually and why the mistake.

Thinking about this goof, I wondered if connection to the Awair account from the MacMini was established before the Awair device connected if the device was removed? To test, I unplugged my Awair R2, confirmed that it was not connected to Awair via the iOS app, and restarted Homebridge -> no issue. Then I powered down the MacMini and powered back up which in turn restarted Homebridge -> no issue. Appears that if the device is not connected the last collected data is returned to the plugin.

If you have any other scenarios that I could test please let me know. Don't like that you are randomly losing connection to your Awair device.

Thanks,
Doug

Hi All,

Thank you so much for taking the time to look at this. It definitely sounds like the issue only occurs with unexpected power failures and the flush/lack of flush of the cache. I've not had an issue since I lasted wrote here and I have my homebridge server do a scheduled restart once a week (no problems with the restart is scheduled).

I really appreciate all the eyes on this and I think that the issue is sufficiently concluded for my purposes. I will think about getting a ups for my homebridge server, although besides this minor inconvenience there's not really a whole lot important running on it.

Thanks again.

@ryan86 - sure thing!

Out of interest, why are you restarting your Homebridge server once a week? There's no specific 'harm' in this but in my experience, if people feel they have to regularly restart components of their network setup to keep things stable, it usually indicates a more fundamental network issue which should be resolved instead of 'band-aided' via regular restarts.

Homebridge is pretty mature/robust these days, should be 24/7/365 stable, and shouldn't generally require restarts.

@adriancable - I started doing it a few years ago to resolve some issues that I can’t even remember now. I think hb was randomly becoming unresponsive or maybe it was another issue. I’ve re-engineered some things on this machine and am using docker for a few things I wasn’t previously so I could probably stop doing this.

@adriancable also have to admit I probably also reboot more than is needed due to historical experience. Do agree that Homebridge is quite stable these days and rarely, rarely has a problem. Working to keep my mouse off the restart button.

I am going to close the Issue for now. Please don't hesitate to reopen if there is additional data to share.