Homekit/iPhone Multi-user Presence Detection Using Homebridge

All credit goes to @mikeg1130 for highlighting this elegant solution on the other HomeKit thread.

I wanted to write this up in an easy to find post to save anyone else having to dig around other threads to find it.

So, this project results in a reliable multi-user presence detection that triggers an input boolean in Home Assistant via an addon called Homebridge. HomeKit in iOS 11 has a new feature to trigger an automation when the last person leaves or the first person arrives home, and with it being Apple this is actually very reliable! No need for device trackers, router detection, nmap, owntracks, etc!

How does this work?

In HA, you configure an input_boolean in configuration.yaml which is then passed to Homekit via Homebridge. This appears as a switch in Homekit which you can manually turn on and off and see it update in HA straight away. Within the Home app on your phone, you then set up an Automation to trigger the input_boolean switch when people leave or arrive home.

What do I need?

  • Apple TV 4th-gen with tvOS 11 or iPad with iOS 10 or later
  • Single or multiple iPhones
  • Hass.io (or the standard installation of Home Assistant)
  • Homebridge Addon to Home Assistant (installation covered below)

Assumptions

Configuring Homebridge

{
 "bridge": {
   "name": "Home Assistant",
   "username": "00:00:00:00:00:00",
   "port": 51826,
   "pin": "<your homebridge pin>"
 },
 "description": "Homebridge for Home Assistant",
 "accessories": [],
 "platforms": [
   {
     "platform": "HomeAssistant",
     "name": "HomeAssistant",
     "host": "http://<hass.io host IP>:8123",
     "password": "<hass.io password>",
     "default_visibility": "visible",
     "supported_types": [
       "input_boolean"
     ],
     "logging": true
   }
 ]
}
  • Note that I only have “input_boolean” in the “supported_types” section, but you can add much more to this list, just be aware that you may end up having to configuring a lot of accesseries within Homeket if you add all these types. e.g.
    "supported_types": ["alarm_control_panel","cover", "device_tracker", "fan", "input_boolean", "light", "lock", "scene", "switch"]

Configuring the Homekit Home App

  • Launch the Home app on your iPhone
  • Press the + icon top left and select Add Accessory
  • Press “Don’t Have a Code or Can’t Scan”
  • HomeAssistant should appear as a device here, so press to select it
  • It will complain about an Uncertified Accessory, just press Add Anyway
  • Enter the PIN when requested
  • You should then be prompted to add the input_boolean name to a room (you may need to press Next a few times)
  • Once done, test the switch by pressing the boolean name on the Home app and watching the input_boolean switch in States turn on and off.
  • Within the Home app, press Automation
  • Where it asks you to choose when you want this automation to occur, select People Arrive, and then The First Person Arrives.
  • Choose your boolean as the trigger, remember to set the on or off state of the switch
  • Repeat the same process for leaving the house

Setting up configuration.yaml

  • SSH to Hass.io and edit configuration.yaml
    nano /config/configuration.yaml
  • This is how it all ties together. Change it for your purposes.
input_boolean:
  wearehome:
    icon: mdi:home
    name: Hassio Homebridge Trigger
    initial: on

automation:
  - alias: "Homekit Home Mode"
    trigger:
      - platform: state
        entity_id: input_boolean.wearehome
        from: 'off'
        to: 'on'
    action:
      - service: shell_command.turnkitoff
      - service: shell_command.turnhalloff
      - service: shell_command.sensorsoff
      - delay:
          seconds: 15
      - service: switch.turn_off
        entity_id: switch.Socket_1
      - service: switch.turn_off
        entity_id: switch.Socket_2
  - alias: "Homekit Away Mode"
    trigger:
      - platform: state
        entity_id: input_boolean.wearehome
        from: 'on'
        to: 'off'
    action:
      - service: switch.turn_on
        entity_id: switch.Socket_1
      - service: switch.turn_on
        entity_id: switch.Socket_2
      - delay:
          seconds: 30
      - service: shell_command.sensorson
      - service: shell_command.kiton
      - service: shell_command.hallon

(My actions basically turn off some Raspberry Pi’s with attached cameras that record video when motion is detected - see here for more details: https://elinux.org/RPi-Cam-Web-Interface)

Hope this helps others, and if there are any steps that need clarifying let me know and I’ll update this post.

11 Likes

Hello.

This looks quite elegant solution, but it will only work with iOS 11, right?

It will work ok with iOS 10 too, just that multi-user support is iOS 11 only.

However you can still do multi-user within HA instead of iOS 11 using multiple booleans - @mikeg1130 posted his solution here: Homekit location to trigger automations in Home-assistant, using a software switch. Requires no Homekit-hardware!

2 Likes

Installing Homebridge on a non-hass.io build

This guide shows how to install Homebridge if you’re using a standard Home Assistant install such as Hassbian or the All-in-one Installer. It pulls together various instructions from the source links below into one handy location.

These instructions are for a Raspberry Pi with ARMv7 (run “uname -a” to check)

pi@hassbian:~ $ uname -a
Linux hassbian 4.9.41-v7+ #1023 SMP Tue Aug 8 16:00:15 BST 2017 armv7l GNU/Linux
  • Update repositories
sudo apt-get update
  • Install NodeJS
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs
  • Install dependancies
sudo apt-get install libavahi-compat-libdnssd-dev
  • Install Homebridge
sudo npm install -g --unsafe-perm homebridge
  • Test homebridge works by running
homebridge
  • To make homebridge run at boot, edit/create a new file /etc/default/homebridge
sudo nano /etc/default/homebridge

Copy the following text into that file

# Defaults / Configuration options for homebridge
# The following settings tells homebridge where to find the config.json file and where to persist the data (i.e. pairing and others)
HOMEBRIDGE_OPTS=-U /var/lib/homebridge
 
# If you uncomment the following line, homebridge will log more 
# You can display this via systemd's journalctl: journalctl -f -u homebridge
# DEBUG=*
  • Create a new file called /etc/systemd/system/homebridge.service
sudo nano /etc/systemd/system/homebridge.service

Copy the following text into that file.

[Unit]
Description=Node.js HomeKit Server 
After=syslog.target network-online.target
 
[Service]
Type=simple
User=homebridge
EnvironmentFile=/etc/default/homebridge
# Adapt this to your specific setup (could be /usr/bin/homebridge)
# See comments below for more information
ExecStart=/usr/bin/homebridge $HOMEBRIDGE_OPTS
Restart=on-failure
RestartSec=10
KillMode=process
 
[Install]
WantedBy=multi-user.target
  • Create a homebridge user and folder, and set permissions
sudo useradd -M --system homebridge
sudo mkdir /var/lib/homebridge
sudo chown homebridge:homebridge /var/lib/homebridge
  • Create a new file called /var/lib/homebridge/config.json
sudo nano /var/lib/homebridge/config.json

Copy the following text into that file, changing Username, PIN, Host, and Password for your needs

{
  "bridge": {
    "name": "Home Assistant",
    "username": "B8:27:EB:BE:E6:52",   #Create your own MAC username
    "port": 51826,
    "pin": "xxx-xx-xxx" #Create your own PIN here, eg. 123-45-678
  },
  "description": "Homebridge for Home Assistant",
  "accessories": [],
  "platforms": [
    {
      "platform": "HomeAssistant",
      "name": "HomeAssistant",
      "host": "http://192.168.0.5:8123",
      "password": "myHApass",
      "default_visibility": "visible",
      "supported_types": [
        "input_boolean",
        "switch"
      ],
      "logging": true
    }
  ]
}
  • Set the ownership of that file
sudo chown homebridge: homebridge /var/lib/homebridge/config.json
  • Install the homebridge-homeassistant plugin
sudo npm install -g homebridge-homeassistant
  • Set the systemctl startup state
sudo systemctl daemon-reload
sudo systemctl enable homebridge
sudo systemctl start homebridge
  • Check the status
systemctl status homebridge
  • If its failed, run the following to check the logs (press shift+g to jump to the end of the file to see current status)
journalctl -u homebridge

Sources
Main install instructions: https://github.com/nfarina/homebridge
Pi Setup: https://github.com/nfarina/homebridge/wiki/Running-HomeBridge-on-a-Raspberry-Pi
Boot instructions: https://gist.github.com/johannrichard/0ad0de1feb6adb9eb61a/

1 Like

very nice, and thank you for sharing

This is awesome work. Just what I’ve been looking for. I’m still very new to hass, can anyone offer some tips on how to use this method to keep a count of people who are ‘home’?

if you already have the sensor or device tracker in your ha to count people home or not home, then expose to homebridge, you will see it in homekit. Sorry for my english language. Hope you understand.

Mine I use owntracks.

no need. you can run on pi user

As new ios update to latest have change the way to have multiple apple devices to share homekit. Not like before that you can add manually.

You don’t have to change username port and pin if your homebridge is already run and you have added to your phone or apple device. Just do below.

First device that add homebridge to the homekit, look on the top left of homekit page arrow sign click that, there will be invite people which allow to use homekit.

Another thing which I’m not sure 100% involved or not is the family sharing. I did family sharing before I add homebridge to my phone and then from homekit I invite member of my family in to use devices shown in homekit.

Any idea how to add media devices to HomeBridge? I can see my light switch in HomeKit. Can’t figure out how to make my TV (LG WebOS) added to Apple HomeKit. WebOS TV is discovered and works fine in Home Assistant

if you use homebridge-homeassistant. it already support media player as on/off switch.

however. you tv has to enable wake on lan in order homeassistant to be able to control on/off.

If your media player didn’t have wake on lan. then do the ir code and make as on/off switch to send command from broadlink.

@Sunonline the TV supports wake on lan, it is setup in home assistant and works fine, I can turn on/off TV from home assistance, so it has been setup properly. When I added HomeBridge I chose to turn off all entities and only turn on the ones I want e.g. light switches, sensors (Only some values e.g. Temperature and humidity) etc…I did all this from “customize” file, but the media_player / TV entity is not listed in the “customize” file, where do I change this to make it view-able in HomeKit?

can you show your config.json

also you can see how the setting of media_player from github homebridge-homeassistant

It looks like you just add “media_player” to the supported types line, e.g.

"supported_types": [
        "input_boolean",
        "switch",
        "media_player"
      ],

Here’s my customize file. The TV works fine on HomeAssistant, I’ve added the WebOS components. I used the “HomeBridge_visible” option and can see the three entities in Apple Home Kit, I just can’t figure out how to do the same for the TV

zwave.aeotec_zw100_multisensor_6:
  friendly_name: Outside Front Sensor
  wake_up_interval: 60
device_tracker.xxxx_iphone:
  friendly_name: xxxx
sensor.xxxx_ipad_battery_state:
  hidden: true
sensor.xxxx_iphone_battery_state:
  hidden: true
device_tracker.xxxx_iphone:
  friendly_name: xxxx
sensor.xxxx_iphone_battery_state:
  hidden: true
  icon: mdi:power-plug-off
sensor.aeotec_zw132_dual_nano_switch_exporting_3:
  hidden: true
sensor.aeotec_zw132_dual_nano_switch_exporting_2:
  hidden: true
sensor.aeotec_zw132_dual_nano_switch_exporting:
  hidden: true
sensor.aeotec_zw132_dual_nano_switch_interval:
  hidden: true
sensor.aeotec_zw132_dual_nano_switch_interval_3:
  hidden: true
sensor.aeotec_zw132_dual_nano_switch_power:
  hidden: true
sensor.aeotec_zw132_dual_nano_switch_power_3:
  friendly_name: Power
  icon: mdi:power
sensor.aeotec_zw132_dual_nano_switch_previous_reading_2:
  hidden: true
sensor.aeotec_zw132_dual_nano_switch_previous_reading:
  hidden: true
sensor.aeotec_zw132_dual_nano_switch_voltage_3:
  hidden: true
sensor.aeotec_zw132_dual_nano_switch_voltage_2:
  hidden: true
sensor.aeotec_zw132_dual_nano_switch_energy:
  hidden: true
  new_entity_id: sensor.aeotec_zw132_dual_nano_switch_energy_4_0
sensor.aeotec_zw132_dual_nano_switch_energy_3:
  hidden: true
  new_entity_id: sensor.aeotec_zw132_dual_nano_switch_energy_4_0_3
sensor.aeotec_zw132_dual_nano_switch_current_3:
  hidden: true
  new_entity_id: sensor.aeotec_zw132_dual_nano_switch_current_4_20_3
sensor.aeotec_zw132_dual_nano_switch_current:
  hidden: true
  new_entity_id: sensor.aeotec_zw132_dual_nano_switch_current_4_20
sensor.aeotec_zw132_dual_nano_switch_alarm_level:
  hidden: true
  new_entity_id: sensor.aeotec_zw132_dual_nano_switch_alarm_level_4_1
sensor.aeotec_zw132_dual_nano_switch_alarm_type:
  hidden: true
zone.home: {}
switch.aeotec_zw132_dual_nano_switch_switch:
  friendly_name: Outside Front Lights
  homebridge_visible: true
sensor.aeotec_zw100_multisensor_6_alarm_level:
  friendly_name: Alarm Level
  icon: mdi:alarm
sensor.aeotec_zw100_multisensor_6_alarm_type:
  friendly_name: Alarm Type
  icon: mdi:alarm-bell
sensor.aeotec_zw100_multisensor_6_burglar:
  friendly_name: Burglar
  icon: mdi:security
sensor.aeotec_zw100_multisensor_6_luminance:
  friendly_name: Luminance
  icon: mdi:brightness-5
sensor.aeotec_zw100_multisensor_6_relative_humidity:
  friendly_name: Humidity
  icon: mdi:water-percent
  homebridge_visible: true
binary_sensor.aeotec_zw100_multisensor_6_sensor:
  friendly_name: Binary Sensor
sensor.aeotec_zw100_multisensor_6_temperature:
  friendly_name: Temperature
  homebridge_visible: true
sensor.aeotec_zw100_multisensor_6_ultraviolet:
  friendly_name: Ultraviolet
  icon: mdi:white-balance-sunny
sensor.aeotec_zw100_multisensor_6_sourcenodeid:
  hidden: true
sensor.aeotec_zw132_dual_nano_switch_voltage:
  friendly_name: Voltage
  icon: mdi:flash-circle
sensor.aeotec_zw132_dual_nano_switch_previous_reading_3:
  friendly_name: Previous Reading
  icon: mdi:file-document
sensor.aeotec_zw132_dual_nano_switch_power_management:
  friendly_name: Power Management
  icon: mdi:file-powerpoint-box
sensor.aeotec_zw132_dual_nano_switch_power_2:
  friendly_name: Aeotec ZW132 Dual Nano Switch Power (sensor)
  hidden: true
sensor.aeotec_zw132_dual_nano_switch_heat:
  friendly_name: Heat
  icon: mdi:oil-temperature
sensor.aeotec_zw132_dual_nano_switch_energy_2:
  friendly_name: Energy
  icon: mdi:battery-charging
sensor.aeotec_zw132_dual_nano_switch_interval_2:
  friendly_name: Interval
  icon: mdi:format-line-spacing
sensor.aeotec_zw132_dual_nano_switch_current_2:
  friendly_name: Current
  icon: mdi:flash-outline
sensor.aeotec_zw132_dual_nano_switch_sourcenodeid:
  hidden: true
group.all_automations:
  hidden: false

Show me the config.json (from homebridge side).
and what the entity_id of your media_player for tv web os.

Then I can help with it.

@Sunonline here you go. Thanks in advance.

{
  "bridge": {
    "name": "Home Assistant",
    "username": "xxxxx",
    "port": xxxxx,
    "pin": "xxxxx"
  },
  "description": "Homebridge for Home Assistant",
  "accessories": [],
  "platforms": [
    {
      "platform": "HomeAssistant",
      "name": "HomeAssistant",
      "host": "my-xxxxx:8123",
      "password": "xxxxxxxxxx#",
      "default_visibility": "hidden",
      "supported_types": [
        "automation",
        "binary_sensor",
        "climate",
        "cover",
        "device_tracker",
        "fan",
        "group",
        "input_boolean",
        "light",
        "lock",
        "media_player",
        "remote",
        "scene",
        "script",
        "sensor",
        "switch",
        "vacuum"
      ],
      "logging": true,
      "verify_ssl": false
    }
  ]
}

ok. if you don’t have any customize for homebridge in customize.yaml.

you have to change in config.json / after change below, all devices in the support_types will be expose to homebridge then these devices will show in your homekit.

“default_visibility”: “hidden”, <== change to visible

@Sunonline I’m aware of that. By changing it to “Visible” everything shows up in Home Kit, including every entity and every value. I don’t want that because it gets very messy. I changed default_visibility to hidden so I can only select the nodes / entities I want to see in Home Kit

if you don’t visible on config.json. You have to do customize the entity_id you want to be seen by homebridge.

customize:
  switch.example:  <== change to media_player  check the entity_id
    homebridge_hidden: true <== true or false to be seen by homebridge
    homebridge_name: My awesome switch  <=== name to be seen by homebridge