HomeKit doesn't save device room assignment

I just started using the HomeKit component for HA and previously was running the Homebridge add-on in Hassio. One thing I’ve noticed is when I had Homebridge on Hassio, I could restart my RPi3 and when everything loaded up, all of my room assignments in my iOS Home app were saved. However, with the HomeKit component, everything appears to change back to the same room the Home Assistant homekit.bridge is located. This obviously isn’t ideal as I have to go back through each device and re-assign the rooms but worse is all of my automations in HomeKit lose their devices as well.

I noticed on the HomeKit component page it mentions HomeKit doesn’t support any kind of persistent storage. I’m not sure if this is what the page is referring to but any ideas how Homebridge was able to make this work?

1 Like

I would assume room assignments are stored in the Home app, not in home assistant. In that case, I suspect the accessoiries presumably have been given a new unique id.

I see. So each time HA is restarted, it assigns a new unique_id to each device? If so, then this statement would explain it from the HomeKit component page which basically matches with what you said:

Currently this component uses the entity_id to generate a unique accessory id (aid) for HomeKit. The aid is used to identify a device and save all configurations made for it. This however means that if you decide to change an entity_id all configurations for this accessory made in the Home app will be lost.

That being said, is there a way for the unique_id to not change after HA restarts? It’s pretty frustrating having to re-assign rooms to each device and re-build all my automations in HomeKit after a HA restart.

No. Unique IDs only change when you change the entity id. So it is safe to do restarts if you have not changed entity id.

However you should be aware that all entities need to be available before HomeKit is started. I myself have built in a 1 minute delay to let hue and zwave start.

I also have the exact same issue about the HA devices brought by Homekit component in my Homekit setup; loses room assignment (and Homekit kit custom name BTW).
Although, this is not happening when I reboot HA (I also have the start delay of 1 minute).
This is only happening when my entire house has a power failure (so everything reboots (Apple TV, HA, router, etc…))

The way I understand it, if the device isn’t loaded in HA when HK component starts, it won’t show up in Homekit (and I get that that).
But in what scenario the device would get a different ID (and appear back in Homekit in default HA room with default name)?

Just trying to figure out the order of things to avoid this issue… Thanks in advance!

You just need to ensure that all of your devices are loaded (with the same entity_id as previously) before HomeKit.

This could be a problem on the RPi when using discovery, for example, because discovery could take some time to find all of your devices. A longer delay before starting HomeKit would help this.

This also means (I think) that if your device is in HomeKit, then is powered off, then you restart HA, then the device will be removed from HomeKit. Even if you then power it on and restart HA, it will revert back to its default room as HomeKit is seeing it again for the first time (this is also the case with Homebridge).

ok but if the delay is too short and device wasn’t loaded before HK component starts, the device won,t be there at all no? So what scenario would cause the device to get a different ID (sorry, I might just not understand correctly)

Only if you change the entity_id; but my point was that there may be other reasons that an accessory reverts to the default room other than that.

ah well, it must be something else causing this in this case…
thanks

I think there’s something else causing my issue because the device_id is not changing but HomeKit is losing room assignments when HA is restarted.

Edit: So disabling auto_start and adding an automation to start HomeKit 1 minute after HA is started seems to keep my device room assignments in the iOS Home app. However, my automations still lose all of their device assignments. Additionally, my ‘Favorite Accessories’ section on the iOS Home app setup seems to get reset (i.e. devices are not setup in the order I had them) along with ‘Include in Favorites’ and ‘Status and Notification’ settings. I didn’t have these issues with Homebridge so hopefully it’s something that can be adjusted for this HomeKit component.

Well, I didn’t change anything but I restarted HA several times today and all of my HomeKit room, zones, automations, etc. all saved. I also removed the HomeKit one minute delay start automation as I’m pretty certain that’s unnecessary for my setup (all of my devices are via Abode), and all of the HomeKit settings remained save.

Not sure what was causing the issue before…

I’ve also been experiencing the same problem. Seems to me that the problem resides in the homekit component for Home Assistant. In init.py, there is a function that generates the accessory ID for each accessory that relies on the adler32 function of the zlib package to compute a checksum on the entity ID.

According to the Python documentation, the adler32 function changed in version 3.0:

This seems to imply that depending upon your platform and Python version you might generate different accessory IDs if you do not perform a bitwise AND on the output of the adler32 function with 0xffffffff. I manually updated the homekit component to do this and afterwards I can restart Home Assistant and my HomeKit accessories do not move to the same room as the hub. Here is the resulting code:

1 Like

Hi, this sounds like a great fix - because I also have the same issue.
Can you provide this fix to Home Asssistant Source, to include this fix in future releases?

thanks
BR
Chris

I was strongly considering doing this but just yesterday I had to restart Home Assistant and sure enough all the entities exported through the HomeKit Controller component again ended up in the same HomeKit room as the Home Assistant “hub”. They had persisted in the correct rooms across numerous HA restarts as I have been doing a lot of work building up my HA instance so I’m not sure why this most recent restart caused a regression. I will keep testing and if I can determine a fix I will certainly submit a pull request.

I had the same problem with my Homekit.
What has worked for me:
removed the homeassistant from my homekit (ios). (this is optional I think)
I restart HomeAssistant and didn’t start homekit (HA) and removed the file .homekit.state same folder as my configuration.yaml.
After restarting homekit (HA) the file was recreated.
Readded the homeassistant bridge to my homekit (ios)

I had to re-assign all my entities in homekit but this seems to have solved the problem ever since. (1 week and counting)

PS: I also made an automation so homekit doesn’t start automatically when not all components are loaded. This to avoid the same problem over again.

see link from @shred (3th post)

# Example checking specific entities to be available before start
homekit:
  auto_start: False

automation:
  - alias: 'Start HomeKit'
    trigger:
      - platform: homeassistant
        event: start
    action:
      - wait_template: >-
          {% if not states.light.kitchen_lights %}
            false
          {% elif not states.sensor.outside_temperature %}
            false
          # Repeat for every entity
          {% else %}
            true
          {% endif %}
        timeout: 00:15  # Waits 15 minutes
        continue_on_timeout: false
      - service: homekit.start
2 Likes

I had this problem too, and after struggling for it for so long (entities sometimes do not load after an update of HA, which could break everything in Homekit).

What I did is trying to minimize the Homekit usage. (I personally shifted away entirely from Homekit and use lovelace instead now). If you want to use Homekit anyways without constantly losing the automations you have made. I’d suggest you make all automations in HA instead. So Homekit will be nothing more than just a bridge with buttons. This works for me. Whenever I feel like using the Homekit app, I can. But no longer do I rely on the usually unstable outside connection from Homekit and the problem that I had which was super annoying was that every so often my “home” location would shift in Homekit is which is suddenly 2 streets away from my actual location, obviously breaking all automations made with Homekit that relies on location. (Apple gives you no options to change the home location, only way to fix it is to reset homekit and start over).

No more, all automations are made with HA now, and Homekit for me just serves as a set of buttons.

Correct.
I do this also. All my automation are in HA and I only show the needed entities to Homekit.
Even If I need a trigger in Homekit I create a Boolean special for triggering the automation.
That way if I expose to Homekit or Alexa or google home the automation still stands.

Eg. trigger sleep -
Create Boolean in HA + expose to homekit, google, alexa and lovelace
When boolean turns on, turn on Automation in HA
kill the lights + turn of TV + turn boolean back off

But I still need the check if all components are loaded before starting Homekit! else rooms might be lost in Homekit. (see my comment above)

I’m facing the same problem. Is there a BestPractice how to solve/avoid this?

Best regards,

Stefan

Is there a recommended solution to this problem? Seems like many are having the same issue.