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
- Homekit has been set up with the second person invited to your home. Instructions can be found here:
https://support.apple.com/en-us/HT207057 - Hass.io has been installed and configured. https://home-assistant.io/hassio/
- SSH to Hass.io has been set up and configured: https://home-assistant.io/addons/ssh/
Configuring Homebridge
-
With Hass.io, installing Homebridge is very easy. Just follow the instructions found here: https://github.com/hassio-addons/addon-homebridge
If you aren’t running Hass.io, follow my instructions to install Homebridge below: Homekit/iPhone Multi-user Presence Detection Using Homebridge -
SSH to Hass.io and edit the /config/homebridge/config.json file
-
This is my config.json file
{
"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.