Trying to unlock door when garage door opens - but ONLY when done via homekit

I’d like to have a way to open my garage door AND unlock my door from my car’s CarPlay setup. HOWEVER, I don’t want the door to unlock whenever someone opens the garage because I consider that a security concern. I can open the garage door via my iphone and the homekit integration which appears in carplay.

Is there a way to unlock my door ONLY when the garage door is opened via homekit? Is there another way to achieve something similar?

I know there’s a context on triggers that can be accessed in automations (I do this). If you act on something from the HA UI there will be a user context. I don’t know what will be the case for actions from HomeKit (assuming you use the HomeKit controller integration), but this might give you something to look into.

I have a HomeKit-only front door lock. With three different home automation ecosystems now, I have used HomeKit’s automations and arrival/departure presence detection to automate unlocking the front door and turning the alarm system on/off.

Unless something has changed recently, the big gotcha is that Apple’s automations cowardly refuse to unlock doors based on arrival. I have to use a virtual/dummy switch (input_boolean helper in HA) on all of my back-end HA platforms to control the lock, with four different HomeKit automation routines to keep the lock and virtual switch in sync based on whether the lock was opened/closed via the virtual switch or at the physical lock.

Anyway, I assume that you can set something similar up from the HomeKit automation side…

Do you have any good examples? I get the general concept, but I’m having trouble determining the event type and how to use use the context to filter for only HomeKit events.

Can you share your method or link to something similar? Obviously from a programmatic standpoint its simple but I’m new to working with these kind of entities. I haven’t yet learned homw to explore and experiment with them.

I should add that I can see the logbook Garage Door is opening by HomeKit so the info is definitely there, its just getting to it.

I don’t specifically do what you need, so I don’t have a good example of exactly that, but this is an example of how I determine which user triggered a script. This just shows how to access the context.

    - service: persistent_notification.create
      data:
        title: System
        message: >-
          {% set user_id = states.script.shutdown_remote_host.context.user_id %}
          {% set triggered_by = (states.person | selectattr('attributes.user_id','==', user_id)) | list | first or "system" %}
          {% set first_name = "System" if triggered_by == "system" else state_attr(triggered_by.entity_id, "friendly_name").split()[0] %}
          {{ first_name }} shut down {{ name }}.

Under the developer tools, try to listen to events to see the full JSON of what’s received. I didn’t know myself what to listen for when doing a quick test, so just my main gate via HomeKit while listening for all events (* – this results in a lot of data very quickly in my case).

I got this:

{
    "event_type": "homekit_state_change",
    "data": {
        "entity_id": "cover.main_gate",
        "display_name": "Main Gate",
        "service": "open_cover",
        "value": null
    },
    "origin": "LOCAL",
    "time_fired": "2022-01-14T06:37:39.947632+00:00",
    "context": {
        "id": "6710e34dac1eaa23d1127b04e1c688d8",
        "parent_id": null,
        "user_id": null
    }
}

So it actually seems a lot easier since there is a specific event. I thought there might’ve been a special context on a state_changed event.

Knowing this, you can now write an automation using this (untested):

  trigger:
    - platform: event
      event_type: homekit_state_change
      event_data:
        entity_id: cover.some_cover
        service: open_cover

Should probably submit an update to the docs about this…
HomeKit - Home Assistant (home-assistant.io)
Document that the integration emits an event by parautenbach · Pull Request #21189 · home-assistant/home-assistant.io (github.com)

1 Like

It took me quite a while to get back to this but its working! Really not bad to set up, I think I got sidetracked by more complex problems.

2 Likes