Please see post 8 for a better implementation of this method:
To use multiple user as trigger/automation see post 14:
##Original Post:
I prefer to use Homekits geofencing to determine if enter or exit a zone, it doesn’t drain battery and it’s reliable.
The logic behind this is:
Location update → Toggle software switch in Homekit → Read status of software switch in HA → Automate in HA.
For this you to work, I’ll assume that you have:
- Homebridge running and installed
- iPhone with ios9+
This is what we will do:
- Install homebridge-plugin
- Modify location in homekit-app
- Read status of homekit in Home-assistant
- Automate in Home-assistant
1. Install homebridge-plugin
What we want to do is to add a command-switch that sends a command when you’re home, and another when your’re not home. In this case we will create a folder called “USER_home” if we are home, and remove this folder when we are not home.
on_cmd will create the folder, off_cmd will remove the folder and set_cmd will read the status to report to homekit.
If you don’t have homebridge installed, follow this guide. Please use this side to familiarise your self with homebridge, I’ll assume that you are familiar with homebridge in this guide.
If you do have homebridge installed, install this plugin
In your homebridge configuration-file (config.json) add this:
"platforms": [{
"platform": "cmdSwitch2",
"name": " CMD switch",
"switches": [{
"name": "User isHome",
"on_cmd": "sudo rm -r /PATH-OF-CHOICE/USER_home; sudo mkdir /PATH-OF-CHOICE/USER_home",
"off_cmd": "sudo rm -r /PATH-OF-CHOICE/USER_home",
"state_cmd": "ls /PATH-OF-CHOICE | grep -i 'USER_home'",
"polling": "true"
}]
make sure that homebridge can make a folder and remove it without a password, type in terminal:
sudo visudo
and add at the last line
homebridge ALL = NOPASSWD: /bin/rm, /bin/mkdir
make sure that homebridge has r/w-access to your path of choise “PATH-OF-CHOICE”, use this in terminal:
sudo setfacl -m u:homebridge:rwx /PATH-OF-CHOICE/
Please remember that since the user homebridge r/w-permissions to the chosen folder, make sure that your environment is secure and not available from outside your lan. Make sure you have a safe password for the user homebridge.
2. Modify location in homekit-app
I like to use the eve-app for homekit. But you can use you homekit-app of choice. In the app add scene “Home” that switchs on our cmd_switch (make sure that you can see this switch in home/homkit-app, otherwise redo step 1.).
Create a scene that turns off the cmd_switch when we leave home.
3. Read status of homekit in Home-assistant
binary_sensor:
- platform: command_line
command: 'ls /PATH-OF-CHOICE | grep -i "USER_home" | wc -l'
name: USERishome
payload_on: 1
payload_off: 0
scan_interval: 30
4. Automate in Home-assistant
automation:
alias: 'Demo: trigger HA by using homekit, we are home in this example'
trigger:
platform: 'state'
entity_id: binary_sensor.USERishome
from: 'off'
to: 'on'
action:
service: light.turn_on
entity_id: light.livingroom
This can also be used to get updates from homekit-hardware that is not supported in HA. For example I have an Elgato EVE door&window-sensor. I bought this before starting with HA and it does not communicate with HA.
In your homekit-app (I use eve-app), you can turn on a command when the door sensor is triggered.
By importing this information into HA, I can determine with a motion sensor if the door was opened for a person that left or that is entering my home.