HomeKit as a Presence Sensor

Tags: #<Tag:0x00007f7c62d5d7f8>

I know others have used HomeKit bridge to do this, but now that the native HomeKit support is in I thought I would share what I did to get MUCH better presence detection.

First, in my configuration.yaml file, I added two input_boolean devices which I will use as indicators whether or not my wife and I are present, like so:

input_boolean:
  me_present:
    name: Me
    icon: mdi:account
  wife_present:
    name: Wife
    icon: mdi:account

I also added the HomeKit component and filtered to just those two input_boolean devices:

homekit:
  filter:
    include_entities:
      - input_boolean.me_present
      - input_boolean.wife_present

That’s it for the configuration.yaml editing, simply restart HA.

When HA comes back, you will have a card on the main screen that you will see a card that has the HomeKit Setup pin. Follow the setup instructions here:

Now that you have those two switches in your Home app on your iOS device, toggle the switch in iOS and make sure you see the input_boolean switches in HA flip as well. If so, you’re all set.

Add other people to your Home. In the Home app:

  1. Press the “nav-arrow”-looking icon in the upper left.
  2. Under people, you should see yourself listed, select “Invite…”
  3. Invite whoever else you want to be in your home. Note: You will want as many input_booleans defined as the total people in your home

Ok, now that you have all people you want added, on your (the primary phone) Home app create an automation for each person that flips their input_boolean when they leave or arrive:

  1. Press “automations” in the lower right, then the “+” icon in the upper right to add an automation.
  2. Press “When People Arrive”
  3. Press the “i” button a select only one person
  4. Press “Next”
  5. Select the “Switch” you want to control (which corresponds to the input_boolean in HA
  6. Press “Next”
  7. Select the state of the switch you want when this person arrives (i.e., on).
  8. Then press “Done”
  9. Repeat 1-8 again to create an automation that turns the switch off when that person leaves.
  10. Repeat 1-9 for each additional person

At the end, you should have two automations per person: one to turn their switch on when they arrive, and one to turn the switch off when they leave.

Then in HA, you can use those input_booleans to trigger whatever you want. Also, if you put all of your input_booleans, in a group, you can use the group status for actions to do when the last person leaves or first person arrives.

So far, this has been 100 reliable for me (about a week) for two iPhones. I figured that since it’s built into iOS, they’d make it rock solid.

EDIT: I should add. In order for this to work, you need an AppleTV, iPad, or HomePod to act as your homekit hub. It doesn’t actually require any setup (other than you need to be signed in to iCloud on those devices). HomeKit will just automatically use what’s available. But you need an always-on-your-network homekit hub-capable device. In my case, I have two AppleTVs which HomeKit just auto-detected and used.

28 Likes

If only I had an ipad/apple tv I’d try this.

Yeah, I should have added that it requires that you have an iPad, AppleTV, or HomePod to act as a “base station” - I didn’t mention it in my writeup because I’ve had AppleTV for a while and so it automagically “just worked”

Thnx! Just what I was looking for. I am going to give this a try!

Works perfectly, thanks. Super reliable so far when creating automations based on it.

I’m confused then how to make a device tracker in HA based on this input boolean.
Could I have help on how to create a device tracker template based on it? I think it would be a binary sensor with a device class, right?

Update: Never mind, I got it:
in binary_sensor.yaml:

  - platform: template
    sensors:
      hk_presence_josee:
        friendly_name: "Présence Josée (HK)"
        device_class: presence
        value_template: >-
          {{ is_state('input_boolean.josee_presence_hk', 'on') }}      
  - platform: template
    sensors:
      hk_presence_sylvain:
        friendly_name: "Présence Sylvain (HK)"
        device_class: presence
        value_template: >-
          {{ is_state('input_boolean.sylvain_presence_hk', 'on') }}

Then I added those binary sensors back in HK as presence sensors visible in HK (and not switchable manually)

Most reliable presence tracker so far.
After few tests, I only had less than a minute delay to trigger…

2 Likes

Nice - I hadn’t taken the extra step to make a binary sensor and device_class it to presence. I just triggered automations based on the state of the boolean_input.

This is the sole reason why I am still on iOS! Homekit use with HA is excellent and very realible.

Thanks for the write up @ha_steve also thanks @sfnetwork for the presence based template. Didn’t know it existed and needed something like this for my Bayesian sensor!

Do you know if this can be done in Hassio?

I’m getting a error.

Invalid config for [sensor.template]: value is not allowed for dictionary value @ data[‘sensors’][‘corey_home’][‘device_class’]. Got ‘presence’. (See ?, line ?). Please check the docs at https://home-assistant.io/components/sensor.template/
Invalid config for [sensor.template]: value is not allowed for dictionary value @ data[‘sensors’][‘wendy_home’][‘device_class’]. Got ‘presence’. (See ?, line ?). Please check the docs at https://home-assistant.io/components/sensor.template/

any help would be appreciated.

I’ve been using homekit for presence detection for a longtime already and has proven to be very reliable (setting a hue lightbulb to 10% when leaving and 20% when arriving). @ha_steve Your implementation using the homekit component is way easier, thanks for sharing.

@Corey_Maxim - post your section from your configuration.yaml file. Please use the code-fence so the formatting is correct in the post

HI Steve, thanks for your help.

I’m currently unable to even load up my UI.

I’ll post what I have if you can see anything that will allow everything to load up I would be grateful. I would really hate to have to start over :frowning:

configuration.yaml

 homeassistant:
  # Name of the location where Home Assistant is running
  name: Home
  # Location required to calculate the time the sun rises and sets
  latitude: 41.0844
  longitude: -80.7182
  # Impacts weather/sunrise data (altitude above sea level in meters)
  elevation: 0
  # metric for Metric, imperial for Imperial
  unit_system: imperial
  # Pick yours from here: http://en.wikipedia.org/wiki/List_of_tz_database_time_zones
  time_zone: America/New_York
  customize: !include customize.yaml

# Enables configuration UI
config:


# Cloud
cloud:
# Enables the frontend
frontend:

http:


  # Secrets are defined in the file secrets.yaml
  # api_password: !secret http_password
  # Uncomment this if you are using SSL/TLS, running in Docker container, etc.
  # base_url: example.duckdns.org:8123

# Allows you to issue voice commands from the frontend in enabled browsers
conversation:

# Discover some devices automatically
discovery:



weather:
  - platform: openweathermap
    api_key: 253196fc9c0b0c13fa971ecab0d4884d

ios:

notify:

proximity:

media_player:
  - platform: mpd
    name: 'Fire Speaker'
    arguments: 'alsa-audio-device=hw:0,0'
    host: 192.168.86.85
    port: 6600
  - platform: mpd
    name: 'woof'
    arguments: 'alsa-audio-device=hw:0,0'
    host: 192.168.86.85
    port: 6600
  - platform: mpd
    name: 'morning'
    arguments: 'alsa-audio-device=hw:0,0'
    host: 192.168.86.85
    port: 6600
  - platform: mpd
    name: 'mood'
    arguments: 'alsa-audio-device=hw:0,0'
    host: 192.168.86.85
    port: 6600
  - platform: mpd
    name: 'control'
    arguments: 'alsa-audio-device=hw:0,0'
    host: 192.168.86.85
    port: 6600
  - platform: mpd
    name: 'school'
    arguments: 'alsa-audio-device=hw:0,0'
    host: 192.168.86.85
    port: 6600
  - platform: mpd
    name: 'dog'
    arguments: 'alsa-audio-device=hw:0,0'
    host: 192.168.86.85
    port: 6600
  - platform: mpd
    name: 'welcomecorey'
    arguments: 'alsa-audio-device=hw:0,0'
    host: 192.168.86.85
    port: 6600
  - platform: mpd
    name: 'welcomewendy'
    arguments: 'alsa-audio-device=hw:0,0'
    host: 192.168.86.85
    port: 6600
  - platform: mpd
    name: 'welcomedarlene'
    arguments: 'alsa-audio-device=hw:0,0'
    host: 192.168.86.85
    port: 6600
# Enables support for tracking state changes over time
history:

# View all events in a logbook
logbook:

# Enables a map showing the location of tracked devices
map:

remote:
  - platform: harmony
    name: Living Room
    host: 192.168.1.120 #Add the IP address of your harmony remote
    # go to the secrets.yaml file and add your passwords there
    username: !secret harmony_username
    password: !secret harmony_password

# Track the sun
sun:

# Text to speech
tts:
  - platform: google

# Checks for available updates
updater:
  # Optional, allows Home Assistant developers to focus on popular components.

# Note: This component will send some information about your system to
# the developers to assist with development of Home Assistant.
# For more information, please see:
# https://home-assistant.io/blog/2016/10/25/explaining-the-updater/

  home:
  latitude: 41.0846
  longitude: -80.7177
  radius: 150
  icon: mdi:home


############################################################
#             THESE SECTIONS ARE IN OTHER FILES            #
#    e.g. If you add a sensor, just put the platform in    #
#    the sensors.yaml file and not in this configuration   #
############################################################
zone: !include zones.yaml
sensor: !include sensors.yaml
input_boolean: !include input_booleans.yaml
input_select: !include input_selects.yaml
group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
input_number: !include input_number.yaml
camera: !include cameras.yaml
binary_sensor: !include binary_sensor.yaml
############################################################

customize.yaml

switch.bed_light:
  friendly_name: Bed
  icon: 'mdi: light'
switch.dr_light:
  friendly_name: Diningroom Fan
  icon: 'mdi: fan'
zone.home:
  homebridge_hidden: true
  latitude: 41.0846
  longitude: -80.7177
  radius: 75
remote.living_room:
  hidden: true
  homebridge_hidden: true
switch.living_room_fan:
  friendly_name: Fan (livingroom)
  icon: mdi:fan
script.living_room_fireplace_backlight:
  hidden: true
  homebridge_hidden: true
script.living_room_fireplace_flame_color:
  friendly_name: living_room_fireplace_flame_color
  hidden: true
  homebridge_hidden: true
script.living_room_fireplace_heat:
  friendly_name: living_room_fireplace_heat
  hidden: true
  homebridge_hidden: true
script.living_room_harmony_off:
  hidden: true
  homebridge_hidden: true
remote.livingroom:
  hidden: true
  homebridge_hidden: true
script.livingroom_harmony_tv:
  hidden: true
  homebridge_hidden: true
remote.photo_frame:
  hidden: true
  homebridge_hidden: true
media_player.photo_frame:
  hidden: true
  homebridge_hidden: true
media_player.living_room:
  hidden: true
  homebridge_hidden: true
sensor.weather_humidity:
  hidden: true
  homebridge_hidden: true
sensor.weather_pressure:
  hidden: true
  homebridge_hidden: true
group.all_switches:
  homebridge_hidden: true
group.all_remotes:
  homebridge_hidden: true
group.all_devices:
  homebridge_hidden: true
input_select.harmony_remote_living_room:
  hidden: true
  homebridge_hidden: true
group.fireplace:
  homebridge_hidden: true
group.default_view:
  homebridge_hidden: true
automation.living_room_harmony_hub:
  homebridge_hidden: true
automation.living_room_harmony_hub_input:
  homebridge_hidden: true
automation.turn_off_fireplace:
  homebridge_hidden: true
automation.turn_on_fireplace:
  homebridge_hidden: true
sensor.weather_symbol:
  homebridge_hidden: true
group.all_automations:
  homebridge_hidden: true
group.all_scripts:
  homebridge_hidden: true

  hidden: true
  homebridge_hidden: true
script.livingroom_fireplace_flame_color:
  hidden: true
  homebridge_hidden: true
script.livingroom_fireplace_heat:
  hidden: true
  homebridge_hidden: true
script.livingroom_harmony_off:
  homebridge_hidden: true
script.livingroom_harmony_pace_dvr_0:
  hidden: true
  homebridge_hidden: true
script.livingroom_harmony_pace_dvr_1:
  hidden: true
  homebridge_hidden: true
script.livingroom_harmony_pace_dvr_2:
  hidden: true
  homebridge_hidden: true
script.livingroom_harmony_pace_dvr_3:
  hidden: true
  homebridge_hidden: true
script.livingroom_harmony_pace_dvr_4:
  hidden: true
  homebridge_hidden: true
script.livingroom_harmony_pace_dvr_5:
  hidden: true
  homebridge_hidden: true
script.livingroom_harmony_pace_dvr_6:
  hidden: true
  homebridge_hidden: true
script.livingroom_harmony_pace_dvr_7:
  hidden: true
  homebridge_hidden: true
script.livingroom_harmony_pace_dvr_8:
  hidden: true
  homebridge_hidden: true
script.livingroom_harmony_pace_dvr_9:
  hidden: true
  homebridge_hidden: true
zone.ams:
  homebridge_hidden: true
zone.alexs_house:
  homebridge_hidden: true
zone.fitch:
  homebridge_hidden: true
zone.ysu:
  homebridge_hidden: true
script.abc:
  friendly_name: ABC
  hidden: true
script.cavs:
  friendly_name: Cavs
  hidden: true
script.cbs:
  friendly_name: CBS
  hidden: true
script.disney_channel:
  friendly_name: Disney Channel
  hidden: true
script.fox:
  friendly_name: FOX
  hidden: true
script.freeform:
  friendly_name: FreeForm
  hidden: true
sensor.harmony_remote_living_room:
  homebridge_hidden: true
script.hdmi1:
  friendly_name: Mirror TV
  hidden: true
script.hdmi2:
  friendly_name: Digital Picture
  hidden: true
script.hdmi3:
  friendly_name: Mirror Apple TV
  hidden: true
script.input1:
  friendly_name: Armstrong
  hidden: true
script.input2:
  hidden: true
script.input3:
  friendly_name: Apple TV
  hidden: true
script.input4:
  hidden: true
script.livingroom_fireplace_backlight:
  homebridge_hidden: true
script.livingroom_harmony_pace_dvr_select:
  friendly_name: Select
script.volume_up:
  friendly_name: Volume Up
  hidden: true
script.volume_down:
  friendly_name: Volume Down
  hidden: true
script.vizio_on:
  hidden: true
script.vizio_off:
  hidden: true
script.tv_on:
  hidden: true
script.tv_off:
  hidden: true
script.tbs:
  friendly_name: TBS
  hidden: true
script.play:
  friendly_name: Play
  hidden: true
script.pause:
  friendly_name: Play
  hidden: true
script.nickelodeon:
  friendly_name: Nickelodeon
  hidden: true
script.nfl_network:
  friendly_name: NFL Network
  hidden: true
script.netflix:
  friendly_name: Netflix
  hidden: true
script.nbc:
  friendly_name: NBC
  hidden: true
script.mute:
  friendly_name: Mute the TV
  hidden: true

device_tracker.corey:
  battery: 12
  icon: 'mdi: iphone'
  latitude: 41.08457837946463
  source_type: gps

script.select:
  hidden: true

script.back:
  hidden: true
script.clear:
  hidden: true
switch.courtney_box_fan_:
  friendly_name: Courtney's Fan
  icon: 'mdi: fan'
group.family_info:
  homebridge_hidden: true
script.goodnight:
  hidden: true
persistent_notification.invalid_config:
  homebridge_hidden: true
media_player.woof:
  homebridge_hidden: true
media_player.fire_speaker:
  homebridge_hidden: true
device_tracker.cameron:
script.mood: {}
media_player.mood:
  homebridge_hidden: true

input_select.center_map_on:
  homebridge_hidden: true
automation.corey_has_arrived:
  homebridge_hidden: true
input_boolean.corey_home:

group.location_map:
  homebridge_hidden: true
weather.openweathermap:
  homebridge_hidden: true

group.family_home_group:
  homebridge_hidden: true

group.map_view:
  homebridge_hidden: true
group.media_player:
  homebridge_hidden: true

automation.push_travel_time_from_corey_to_wendy:
  homebridge_hidden: true
automation.push_travel_time_from_wendy_to_corey:
  homebridge_hidden: true

input_boolean.push_notification_distance_wendy_to_corey:
  homebridge_hidden: true
input_boolean.push_notification_distance_corey_to_wendy:
  homebridge_hidden: true
group.device_tracker_street_view:
  homebridge_hidden: true
sun.sun:
  homebridge_hidden: true
group.send_push_updates:
  homebridge_hidden: true

sensor.weather_wind_speed:
  homebridge_hidden: true
automation.wendy_has_arrived:
  homebridge_hidden: true

input_number.map_zoom:
  homebridge_hidden: true
group.all_lights:
  homebridge_hidden: true
zone.bos:
  homebridge_hidden: true
media_player.control:
  homebridge_hidden: true

group.ui_view:
  homebridge_hidden: true
zone.jessicas:
  homebridge_hidden: true
camera.location_map:
  homebridge_hidden: true
group.map:
  homebridge_hidden: true
media_player.morning:
  homebridge_hidden: true
camera.street_view_corey_gps:
  homebridge_hidden: true
camera.street_view_wendy_gps:
  homebridge_hidden: true
persistent_notification.config_entry_discovery:
  hidden: true
  homebridge_hidden: true
input_boolean.mom_home:
input_boolean.morgan_home:
input_boolean.wendy_home:
media_player.welcomecorey:
  homebridge_hidden: true
script.welcomecorey:
  homebridge_hidden: true
script.welcomedarlene:
  homebridge_hidden: true
media_player.welcomedarlene:
  homebridge_hidden: true
script.welcomewendy:
  homebridge_hidden: true
media_player.welcomewendy:
  homebridge_hidden: true
media_player.school:
  homebridge_hidden: true
sensor.iphone_battery_state:
  homebridge_hidden: true
sensor.iphone_battery_level:
  homebridge_hidden: true
sensor.google_travel_time_wendy_to_corey:
  destination_addresses:
  - My Address, USA
  homebridge_hidden: true
  origin_addresses:
  - My Address, USA
sensor.google_travel_time_corey_to_wendy:
  destination_addresses:
  - My Address, USA
  homebridge_hidden: true
  origin_addresses:
  - My address, USA
media_player.dog:
  homebridge_hidden: true
camera.yng:
  homebridge_hidden: true
device_tracker.msmas_iphone:
sensor.weather_precipitation:
  homebridge_hidden: true
sensor.msmas_iphone_battery_state:
  homebridge_hidden: true
sensor.msmas_iphone_battery_level:
  homebridge_hidden: true
device_tracker.morgan:
device_tracker.darlenes_iphone_6:

sensor.darlenes_iphone_6_battery_state:
  homebridge_hidden: true
sensor.darlenes_iphone_6_battery_level:
  homebridge_hidden: true
sensor.coreys_iphone_x_battery_state:
  homebridge_hidden: true
sensor.coreys_iphone_x_battery_level:
  homebridge_hidden: true
automation.corey_is_home:
  homebridge_hidden: true
sensor.camerons_iphone_battery_state:
  homebridge_hidden: true
sensor.camerons_iphone_battery_level:
  homebridge_hidden: true

binary_sensor.yaml

  - platform: template
    sensors:
      corey_home:
        friendly_name: "Presence Corey"
        device_class: presence
        value_template: >-
          {{ is_state('input_boolean.corey_home', 'on') }}
  - platform: template
    sensors:
      wendy_home:
        friendly_name: "Presence Wendy"
        device_class: presence
        value_template: >-
          {{ is_state('input_boolean.wendy_home', 'on') }}
  - platform: template
    sensors:
      cameron_home:
        friendly_name: "Presence Cameron"
        device_class: presence
        value_template: >-
          {{ is_state('input_boolean.cameron_home', 'on') }}
  - platform: template
    sensors:
      morgan_home:
        friendly_name: "Presence Morgan"
        device_class: presence
        value_template: >-
          {{ is_state('input_boolean.morgan_home', 'on') }}
  - platform: template
    sensors:
      mom_home:
        friendly_name: "Presence Darlene"
        device_class: presence
        value_template: >-
          {{ is_state('input_boolean.mom_home', 'on') }}

Thanks Again to everybody here!

Well, first off, you have that block of text right near the bottom of your configuration.yaml file that starts with home: that’s way out of place. What are you trying to do with that block?

Are you using the “Check Config” option in the GUI before you try to restart HA? That would tell you where your errors are…

I’m not 100% sure why Home: is there to be honest.

I will move that back to the correct spot.

I always use the check configuration before restarting but I must have forgotten this last time.

Sometimes it’s hard to figure out what exactly is wrong but I usually figure it out sooner or later.

But this time I’m not sure why the Frontend won’t load.

I can still gain access from Samba on my laptop, is there something that you think would fix it then restart my Pi and get everything back?

you should remove that block that starts with home: - I’m not even sure what that is supposed to do. The way you have it now, it considers that whole block as part of your updater: component.

FYI, you can use the command line to check your config like so:

If you have incorrect entries in your configuration files you can use the check_config script to assist in identifying them: hass --script check_config. If you need to provide the path for your configuration you can do this using the -c argument like this: hass --script check_config -c /path/to/your/config/dir.

as explained here:

Ty bro, I’ll give it a try. Much appreciated!

Maybe a dumb question, but homekit will not pass on any GPS information, to be used with zones, etc. Just can turn on/off switches based on presence, Is the presence when the iphone connects to the same network as the apple tv, or bluetooth, or either?

I’m not sure what your’re asking… but the phone determines its own presence via GPS, then through the rule on the phone, it turns the switch in HA on/off. You need an AppleTV/HomePod/iPad on the same network as HA to act as a bridge. For example, when you leave your house, your phone is no longer on your WiFi, so through iCloud it tells AppleTV to do whatever actions are supposed to happen when you leave.

I’m not super familiar with Homekit, and dont currently have an apple TV, or current Ipad. I’m considering getting one.

So I guess what Iam asking is on the homekit side of the house, what is used to determine your presence? I get that when you’re present homekit will turn on a “virtual switch”. Does homekit use geo fencing? just wifi?, etc.

I ask because they both have thier pros and cons for automation. WiFi would be more accurate, but it might take my phone a little bit longer to connect to wifi, so if I wanted to do something like unlock a door when that switch turns on, it might not register in time.

Thanks

Great! Was using this same trick in Domoticz… But I’ve completely switched to Home Assistant. Will implement this for sure!