Homekit - persistent storage

I have some troubles with my entities in homekit.

I know that the entities have to be “online” before homekit gets startet and most of the time this works.

Lately I had some internet connection troubles. When I restart HA while there is a connectivity issue some entities will to come up and will be deleted in homekit. I than have to chose the rooms again and so on. Really annoying.

So I reed about that I should set up an automation. The thing is a simple time offset won’t do the trick. I also don’t know if a ping test will always work. Because there might the option that the internet connection is available but the entities are not set up right, because the connection was failure at the ha restart.

I think the best way to do this would be to proof if the entities (which are included in homekit) are available in “current entities”. One example are my netatmo entities. I have auto discovery enabled for that because I have quite a few of those. So if netatmo fails (could also be their server) the entities will be delated in homekit.

Any suggestions?

The only thing I can think of would be to create template entities for those you would like to stay in HomeKit. I don’t know if that’s possible for all your devices and it’s probably a lot work to set it up, but it should solve your issue.

That really would be a lot of work and a lot of unnecessary entities.

Isn’t there a way to search in the “current entities” list for the homekit entities. And if one value doesn’t appear it should go false. Else true. Something like that.

I guess you could create a template sensor that checks for all states and returns false if one entity is unavailable. Combine this with a delayed HomeKit startup and a a condition in the automation and it should work. Still lots of ifs though.

The result could look something like this. Adjust the if-statements accordingly. Just keep in mind that I haven’t tested it, so there might still be some issues with it.

sensor:
  - platform: template
    sensors:
      entity_status:
        value_template: >
          {% if states('sensor.outside_temperature') is None %}
            false
          {% elif states('sensor.outside_temperature') is None %}
            false
          {% else %}
            true
          {% endif %}


automation:
  - alias: 'Start HomeKit'
    trigger:
      - platform: homeassistant
        event: start
    condition:
      - platform: state
        entity_id: 'sensor.entity_status'
        state: 'true'
    action:
      - delay: 00:05
      - service: homekit.start

Edit

If you need help, let me know.

I think this could be useful for others as well, so it could make sense to update the homekit docs with a working example later here: https://www.home-assistant.io/components/homekit/#disable-auto-start

Thanks or your example. I tried it with the templating tool.

I don’t know if this will work.

As an example: I believe the entities of netatmo will disappear in the “current entities” list, when it is offline and ha gets restarted.
In your example, if I use a random entitiy (which is not in my ha), it will output “true”. The disappeared entity cannot be “none”, because its not in the list. It looks like the the code above will ignore those unknown entities (because output is true)

I agree that this could be useful for other too.

EDIT:
So instead of “None” we’d need some like an “isn’t even in the list”

Try

{% if not states.sensor.outside_temperature %}
false
{% else %}
true
{% endif %}

I tried it in templated. I think this will work thanks.

When I have my example configured I will post it here.

EDIT:
This works. Thanks @cdce8p. Would be nice if someone adds this to the homekit documentation

in binary_sensor.yaml:

- platform: template
  sensors:
    homekit_entity_status:
      friendly_name: "Homekit Entity Status"
      device_class: connectivity
      value_template: >
        {% if not states.light.light1 %}
        false
        {% elif not states.sensor.sen1 %}
        false
        {% else %}
        true
        {% endif %} 

automation:
    - alias: 'HomeKit Start'
      trigger:
        - platform: homeassistant
          event: start
      action:
        - delay: 00:01:00
        - condition: state
          entity_id: 'binary_sensor.homekit_entity_status'
          state: 'on'
        - service: homekit.start

I went ahead and opened a doc PR: https://github.com/home-assistant/home-assistant.io/pull/8005

The automation could even be improved further by using a wait_template instead of the condition: https://github.com/home-assistant/home-assistant.io/pull/8005/files#diff-58bffa051b86c544c0ebf03ad1801960R258

So you want this to be a standard feature of the homekit component, right? Would this look for the exposed entites by default?

The automation could even be improved further by using a wait_template instead of the condition :

Great idea, already copied it to my automations.yaml
Thanks!

Not by default. I just wanted to add the example to the official docs so other users could find and use it quicker. If they are in a similiar situation as you have been.

Ok I see. Will this appear here:

homekit

too?

Year, once the PR is accepted. Probably in the next few days.

1 Like

@h4nc During the review @frenck noticed one more improvement.
Instead of using a binary_sensor, the entity check should be in the wait_template. This improves performance a bit, since the state isn’t logged in the database and not checked if HomeKit is already started.

However, it might be that you actually want the state to be logged. In that case the binary_sensor works as well.


The example code. This will be in the HomeKit doc, too, once the PR is accepted.

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

Thanks for your update.
I prefer to track the state and be able to see the state and my UI.

Latly it seems like HomeKit autostarts (sometimes) after a ha reboot even if the binary sensor is off, because my ha entities are in the standard room again.

Also very annoying: The settings go my garage door get reset on all our devices and we get the garage door open/close notification, which we don’t need/want.

I did not figure out which could cause this. Is there any known issue?