Homekit location to trigger automations in Home-assistant, using a software switch. Requires no Homekit-hardware!

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:

  1. Install homebridge-plugin
  2. Modify location in homekit-app
  3. Read status of homekit in Home-assistant
  4. 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.

11 Likes

Hi, thanks for sharing this - as an iOS house; accurate location tracking will be great…

As someone who likes to follow guides to the letter, what do you recommend as being the path of choice ?

I have it in /home/USER/homebridge_status

The place shouldn’t matter, it’s more of not giving R+W-permissions to the user homebridge to a path that it shoulnd’t have.

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.

1 Like

Awesome, thanks! Worked like a charm.

Next step would be to integrate Siri, right? :slight_smile: Is there any approach?

Yes you could use this method for Siri. Or just use homeassistant plugin for homebridge. :wink:

This is a good solution if you wan HomeKit location to trigger HA or you want HomeKit devices that doesn’t communicate with HA normally.

This can be modified into a little more elegent way, instead of creating a folder we can use the HA REST-api to directly turn on or off a input_boolean.

I’m on my job now, but I’ll modify the post when I’m home to give an example.

1 Like

As promised, a little more sofisticated way to update our location. This is also a bit faster since we call the built-in HA REST-API directly instead of using a sensor to check if a folder exists.

If you do this, please remove the “no password” for homebridge user by removing the line you added in the first post

sudo visudo

later revoke the R/W-permissons for the user homebridge to the path that you chose above in post 1

sudo setfacl -m u:homebridge:r /path-in-post-1-where-you-created-the-folder aka "PATH OF CHOICE"/

##Create an input_boolean in HA

input_boolean:
    USERishome:
      icon: mdi:home
      name: Home

##Call HA REST-API
Now create the following .sh files and places them where you want, I placed them in /var/homebridge/ as my congif.json-file is located there:

  • USERhome.sh
  • USERaway.sh
  • USERstatus.sh

USERhome.sh

curl -X POST -H "Content-Type: application/json" -d '{"entity_id":"input_boolean.USERishome"}' "http://IP-to-HA:8123/api/services/input_boolean/turn_on"

USERaway.sh

curl -X POST -H "Content-Type: application/json" -d '{"entity_id":"input_boolean.USERishome"}' "http://IP-to-HA:8123/api/services/input_boolean/turn_off"

USERstatus.sh

curl --silent -X GET  "http://IP-to-HA:8123/api/states/input_boolean.USERishome" | grep -i '"state": "on"'

Make all 3 .sh-files excutable, eg.

sudo chmod +x /path/USERstatus.sh

Modify homebridge configuration

Change the following rows that you added in first post

"on_cmd": "/dir-.sh-file/USERhome.sh",
"off_cmd": "/dir-.sh-file/USERaway.sh",
"state_cmd": "/dir-.sh-file/USERstatus.sh"

In my case since I placed the .sh-files in /var/homebridge I modify the config-file according to

"on_cmd": "/var/homebridge/rittehome.sh",
 ....

Restart homebridge and home-assistant and you’re good to go!

Let me know if any step is unclear, I’ve tested this “improvement” during the evening and it should work as well as the method in post 1, but this is a little more nice solution and quicker since we call the REST-API directly instead of using a sensor that checks if a folder is present or not.

1 Like

I am curious why you need to use a directory or HA rest-api. I do something similar but simply use the switch that homekit turns on as the trigger. Do you do it like that to help preserve the state on restart?

I created a blank / dummy command line switch called home and exposed it to homekit through homebridge. Homekit is automated to turn the switch.home on if i’m home and off when I leave. HA can read the state of that switch to determine if homekit see me home or not.

I don’t see the difference beside a slightly different approach? I wanted an entity in HA that represents my status. This could be great if I want to modify the look of the entity. Also I can trigger the status in HomeKit by switching on/off in HA.

If I understood what you do:
HomeKit turns on a command line switch, HA automation reads the status of this.

What I do:
HomeKit turns on an input boolean, HA automation reads the status of it.

I have tiny single step more that is command line to turn on input boolean in HA.

As long as both ways works, it should matter right? :slight_smile:

Please share your setup if somebody wants an alternative method (that maybe is working better or is more stable).

Hi guys! I really liked this idea, mostly because whatever I do I don’t seem to make device tracking with ping, nmap or snmp reliable…

I don’t get it however. It works brilliant for me, but when I told my kids to do the same it doesn’t any more. The automations in iOS home app doesn’t distinguish between me and my kids.

Am I doing something wrong?

Regards,
Johan

Hi Johan, did you create separate files for your children? HomeKit doesn’t support multiple users for location triggers. (Ios11 introduces this).

What you need is to redo the steps in step one for each of your children and have unique trigger rule in HomeKit for each child. The scripts above should be named uniquely also.

This is a really good idea. Since I’m already using homebridge-homeassistant, I actually ended doing what RobDIY suggested, and created an input_boolean in HA that is exposed in HomeKit. (With the REST api setup, I had 2 switches in HomeKit, and if I hit the cmd switch, both turned on).

I set up an automation in the Home App to switch the boolean to on when I arrive home, and off when I leave. So far it is working great. Now I want to set up the same for my wife.

My question is this. In the Home App, my wife isn’t able to set up automations, since she is not the ‘owner’ of the HomeKit setup. Does the Eve app allow other users to set up automations based on their location?

EDIT: I just saw you answered that question in the previous post… iOS11. Whoops!

I prefer to be able to set the state in home-assistant, especially for restart. I think it’s great when using appdaemon too.
Anyway, it’s the same logic and really doesn’t matter.

As I said, you don’t need iOS 11. You can just trigger different inout_boolean.:

  • X’s home app triggers X_isHome
  • Y’s home app triggers Y_isHome

Then you can put both input_boleans in a group and set rules based on the state of the group. If somebody is home, then the group is set to on. If both is away, the groups state is off.

Glad you liked it!

1 Like

You are right. I realized that I needed to grant my wife the ability to edit in the Home app. Now she has the ability to create automation, and we got a new trigger added for her.

So far it is working well, and seems much faster than other methods.

You can use the same concept to control any homekit device from HA. I use it for a schlage sense lock.,

or magnet sensor

Yes, I wrote that a couple of post up. I’m using it for my elgato eve door sensor.

No, I actually followed RobDYIs method. But I think you’re correct, that doesn’t really work for more than one person per “Homekit home”…

Thanks, will try your method during the weekend!

/Johan

I just setup everything with the input_boolean method too… need to try how well it works. In any case, why you think it won’t work for more than one person per HomeKit home?

Ios10 doesn’t support multi-user automation, it’s a limitation in ios10. It’s fixed in ios11.

If you have ios10 and multiple users, use this: