iOS presence detection WITHOUT HomeKit component or HA app (totally native)

I’ve been using the usual iOS presence detection method of exposing an input_boolean through HomeKit and then flipping the switch when I get home, and using that input_boolean switch based on location changes. But I got tired of seeing those switches in the Home app on my iPhone (and they are particularly confusing for family members). Plus, those input_booleans can’t be used with the new person functionality. This method will have update your status with real device_tracker entities.

So I decided to use the new iOS 13 Shortcuts app and HA webhooks to get around needing anything at all. The only requirement for this method is having a public URL for your HA install. The main benefits are:

  1. Actually getting a device_tracker entity. For me this means I can use the person platform more fully.
  2. You don’t need to use the HomeKit platform at all if you don’t want to.
  3. No weird useless switches in my Home app, when you do use the HomeKit platform.

Before you set anything up in iOS, you’ll need a really simple automation to enable a new webhook. Change your webhook_id to something very random!

- alias: iOS Shortcuts Webhook (presence)
  trigger:
    - platform: webhook
      webhook_id: ios_hook_XXXXXXXwhateveryouwantXXXXXXX
      allowed_methods:
        - POST
      local_only: false
  action:
    - service: device_tracker.see
      data_template:
        dev_id: "{{ trigger.json.tracker_name }}"
        location_name: "{{ trigger.json.location_name }}"
        gps_accuracy: 80

(I actually don’t know what the range of values are for gps_accuracy so would love any insight there. I imagine it should be set fairly low since the Shortcuts app has been unreliable in the past.)

After you have the webhook added, reload your automations or restart HA. Then you can get started in the iOS Shortcuts app.

  1. Open the Shortcuts app and create a new “Home” automation (not a “Personal” one). Here I’ve got them for me and another person coming/leaving the house.

  2. Name the Shortcut, and choose the person you want to trigger it. Then tap the “Do” area.

  3. Ignore all the actual HomeKit stuff and scroll to the bottom, and tap the “Convert To Shortcut” button.

  4. Choose the “Web” option.

  5. From the list, choose “Get Contents of URL”

  6. This is the important part. Put in the URL of the webhook you defined in your automation (eg. https://<your public HA address>/api/webhook/<whatever webhook_id you defined> ). Then hit the “Show More” button and make a few changes: change the method to POST, make sure the body is JSON, and add two new text fields: tracker_name and location_name. The tracker_name is the id of the new presence entity you’ll be creating. You don’t need to create this beforehand – simply setting it via the webhook will cause it to exist. The other text field is location_name and I only ever use home and not_home here.

After you’ve got this, you can hit the little play button in the lower right corner to test it, and it should instantiate the new device_tracker the first time, and change state after that (depending on what you set location_name to). Then hit next, confirm the automation, and you are all set!

I’ve only been using this since iOS 13.1 came out a few days ago, but it seems to work as well as the old input_boolean method.

18 Likes

This works great! By the way, you can do it via a ‘personal’ shortcut, as the home shortcuts are limited to users with a homekit base-station (ipad, homepod, apple tv).

Nice, good call. I went with the ‘Home’ shortcut so I could set them for phones that aren’t mine as well, but definitely if you only care about your phone, Personal works as well.

Glad it works for you! I’m still using it for my family’s devices and the person platform definitely seems to rely on this one more often than my Unifi device trackers.

Is anyone still using this? Is it still working well?

Do I have to have a public IP/name set up and a firewall rule to forward port 80 to my Home Assistant for this to work?

Yep, I still use it and it works very well. And you’re correct, this does require a publicly accessible host to call. Not necessarily port 80 though, just whatever port you open up.

Hi !

Sorry to say that, cause it seems to be a great solution :+1: … but it’s not really clear your topic…

Where are we supposed to put the code ?
In the Automations.yaml ?

So there is a problem with the format of your code and the variables, cause it’s impossible to restart HA.

I’ve managed the code and obtained that :

# Apple app trick
- alias: iOS Shortcuts Webhook
  trigger:
  - platform: webhook
    webhook_id: ios_hook_presence
  action:
  - service: device_tracker.see
    data_template:
    dev_id: "{{ trigger.json.tracker_name }}"
    location_name: "{{ trigger.json.location_name }}"
    gps_accuracy: 80

This is the error that I get :

A

Have you the same report ?

Do I miss something somewhere ?

Thanks !

Your indents are wrong. Everything under data_template should be indented. Check mine in the original post :).

I think I’m stupid or I don’t understand the thing…

I’ve copied pasted your code in Automation.yaml and it’s ok now, the code is well written.

What is the ID of the tracker_name that I’m suppose to add ? The ID created in the HASSIO Automation section ?

And when I push the play button it says that there is no server with the host name detected.

Even after that… how do you program your presence in Hassio ?

You have to create an other automation based on the first one ? (iOS Shortcuts Webhook (presence))

Sorry, I’m a little bit confused…

Thanks a lot :+1:

Did you follow my iOS instructions? Adding the automation is only the first step and doesn’t do anything on its own. The automation “listens” but the iOS device is what “talks.” The step-by-step screenshots that I posted are how you make your iOS device do that talking.

The tracker_name in the iOS Shortcuts configuration can be whatever you want, like mykie_iphone_123 or something.

Once you’ve finished the iOS pieces, you can hit the little “play” button on the lower right and it will do a test transmission. The first time you do that, the new device_tracker called mykie_iphone_123 will start showing up in your HA.

1 Like

Hi,

I’ve done exactly what you said. I’ve used my IP public address, and I got the same message about the certificate that is not valide.

With my DuckDNS address it says that there is no server with the host name detected.

I think I have some trouble with the access.

Arfffff… I’ve tried with the Home App on a Mac… and it works… fiouuuuu

Thanks a lot !!!

So it means that when I am away, Mykie_iphone_123 (you example name) will change of status ? Like Not_home or do I have to make other automation on IOS ?

Yep, exactly. You will have an entity called device_tracker.mykie_iphone_123 which will have states of home or not_home.

And yes, you need one automation for “when I leave” and another automation for “when I come home” but they will be identical except for the location_name which you should make home in one case and not_home in another case.

Glad it worked out!

Zacs a big thanks.

My only problem is with DuckDNS, I think it doesn’t work. Have you the same problem ?

And Sadly, not about your topic, but the geolocation is not ok… it doesn’t work in my house.

I think I’m not the only to have a certificate problem :

https://developer.apple.com/forums/thread/651963

Apple doesn’t like link with SSL when it’s outside your network…

Yah you cant selfsign. You’ll need LetsEncrypt or could use CloudFlare. CloudFlare is a bit easier imo since their certs have a ten year lifespan. Someone posted a howto with them but I’m mobile right now so can’t look it up.

Yep, me too, I’m on mobile.

But I think I have a big issue with Homekit, it doesn’t get the fact that I’m away.

You need to keep the Wifi ON when you get away your house ? I mean your Apple TV will calculate the distance with and guess that you are away ?

I tried all this, but my iPhone as well as my Ipad keep saying that the certificate is invalid. Any ideas how to solve this? My router is a fritzbox and (after portforwarding of port 8123) I can connect to the interface of HA.

Thx and kind regards,
Kees

Do you have SSL certs setup for HA? If not, it might work to use http:// instead of https:// in the “Get Contents of URL” setup.

Hi Zacs,

Thank you for your quick reply.

I don’t get the SSL working at this moment. So I tried the HTTP. But HTTP gave me: Can not access server. Aftre some research I discovered that I used the wrong portforwarding on my router. I forwarded port 80 (http) to port 80 of the HA. But that doesn’t work. After that I forwarded port 80 to port 8123, the problem was solved.
So I tried the same with port 443 (HTTPS) but then I get the message: “an ssl error has occurred and a secure connection to the server cannot be made”.

So as a workaround I use the HTTP and in the meanwhile I will try to solve the SSL problem.

Kees

1 Like