HomeKit as a Presence Sensor

The only presence sensor I have found to be 100% accurate is tracking via my router (asus). I’m giving up on the other two (HomeKit and iOS).

I agree for the most part. I have the exact same setup as you (I think).

I use asuswrt, iOS, and Homekit with a switch.

I agree that asuswrt is rock solid, but here are the downsides for me.

  • Even though it my signal strength is strong, it’s not strong enough for some routines, like open the garage door when I get home. With the other sensors (when they are working), my garage door is opening as I pull up, or just opened 10 seconds before. I like that. I don’t want to have to wait for wifi to connect and sit in my car.

  • sometimes I might have a reason to have my wifi off on my phone. I don’t want to trigger away automation’s. I will say this is not super common, but it does happen for me once in a while.

Using these three as a Bayesian sensor has been much, much better for me. If you already have an apple TV, or Ipad for homekit, and have an iphone, why not use them. Just don’t use them in your automations, use the Bayesian sensor.

But again, totally agree with your frustration. It seems to be the hard part for most systems, I know I struggled with this with SmartThings as well. I ended up with three different apps on my phone and wifi as well to get a pretty stable presence sensor.

This really got me thinking and I have a few thoughts/Ideas. For my automations I have two types of needed presence deteection.

  • Am I home? Does not need to be instant, just reasonably quick. I’d use this to send me alerts, if I was not home and the windows were open, or maybe for a possible burglar alarm. Motion in the house, while away.

  • Did I just get home? Things you want to do as soon as you arrive or leave. Unlock doors, open garage, turn on lights, announce arrival with TTS, etc.

The more I thought about it, the more I realize (IMO), that there is not an all in one solution. So I’ve just updated my automations, groups, and sensors based on this.

So for the 1st one. Am I home? I’m using the Bayesian sensor. I’ve tweaked it a bit, so that it requires two of the three to be true to be “ON”. This should be solid.

 - platform: bayesian
   prior: 0.6
   name: MePresence
   probability_threshold: 0.9
   observations:
   - entity_id: 'input_boolean.me_present'  #Homekit Switch
     prob_given_true: 0.9
     prob_given_false: 0.2
     platform: 'state'
     to_state: 'on'
   - entity_id: 'device_tracker.me_iphone'  # iPhone HA/GPS
     prob_given_true: 0.9
     prob_given_false: 0.4
     platform: 'state'
     to_state: 'home'
   - entity_id: 'device_tracker.me_iphone_2'  # ASUSWRT
     prob_given_true: 0.9
     prob_given_false: 0.2
     platform: 'state'
     to_state: 'home'

I then created a group with both Iphones for some automations

me_iphone_presence:
  name: me Iphone Presence
  icon: mdi:account-multiple
  view: no
  entities:
    - device_tracker.me_iphone
    - device_tracker.me_iphone_2

Also created a simple automation to turn off HomeKit, just in case it gets stuck on. I use the Bayesian Sensor as the trigger. Since the Bayesian sensor requires at least two to be Present to be on, if the HK switch is on, then it’s probably stuck.

##########################################
## Homekit Presence Fixes JIC                               ##
##########################################    
- alias: 'Homekit Stuck fix for Me'
  trigger:
   - platform: state
     entity_id:
       - binary_sensor.multiple_presence_me  
     to: 'off' 
  action:
  - service: homeassistant.turn_off
    entity_id: input_boolean.me_present

lastly, for automations that I want to run as soon as I get home, I’m going to use this as my trigger. Of course you’ll have to tweak to make sure automations dont run twice since that could happen.

- alias: generic automation name
  trigger:
   - platform: state
     entity_id:
       - input_boolean.me_present   #HK 
     to: 'on'
   - platform: state
     entity_id:
       - device_tracker.me_iphone  # iPhone/HA GPS
       - device_tracker.me_iphone_2 #AsusWRT
     to: 'home'

I chose to use the device trackers for the Iphone seperatly vs the group, just in case one of them got stuck in the wrong state.

Again, I’ve found that the HK switch is very good at “turning on”, but only pretty good at turning off. The asuswrt like you said, is nealry flawless, but has a smaller range.

Anyway, it’s a bit of extra work, but I think worth it. Easy to repeat. I’m sure I could do this a bit simplier, but the main goal was to break up my presence into those two catgories vs trying to make a all in one solution that only worked 90%

4 Likes

I had this working for a few days. Then I added my girlfriends iPhone 7 to this, and we share the same Apple-ID. Now my presence is linked to when she is home/away. What can I do to address this?

Stop sharing an AppleID.

4 Likes

Guess Ill just have to do that then. Family Sharing, here I come :stuck_out_tongue:

An AppleID has always been meant to be tied to an individual. If you share AppleID’s then your chats, contacts, etc. are also shared. It’s effectively the same as trying to share a single email account.

Apple, of course, didn’t make things easy by not having some sort of “family” system in place until recently.

I moved my gf to her own Apple ID and now all is well. Thanks for a Nice guide, it is working nicely for me

I wanted to provide my current setup and using Homekit. It has been 100% for several weeks now. I’ve posted much of this already, but wanted to add a few automations, and just overall processes that have worked very well.

Although I am using Homekit, this generally would work well with any three presence sensors. I feel like solid router based presence is key. GPS based are all pretty good, but as we all know, none of them seem to be 100%. That’s why for some of my automations I trigger on any of the three turning on, or becoming home, and other automations are based on the bayesian sensor.

 binary_sensors.yaml
# This will change to on if any two are true, but not is only one is true.
# For me most common is for HK then ASUS, and then close behind HA GPS
 - platform: bayesian
   prior: 0.6
   name: me Presence
   probability_threshold: 0.9
   observations:
   - entity_id: 'input_boolean.me_present'  #HK switch
     prob_given_true: 0.9
     prob_given_false: 0.2
     platform: 'state'
     to_state: 'on'
   - entity_id: 'device_tracker.mes_iphone'  #HA Iphone App - GPS
     prob_given_true: 0.9
     prob_given_false: 0.4
     platform: 'state'
     to_state: 'home'
   - entity_id: 'device_tracker.mes_iphone_2' #ASUS/WRT router based 
     prob_given_true: 0.9
     prob_given_false: 0.2
     platform: 'state'
     to_state: 'home'
	 
# useful if you want to use the bayesian sensor as a more traditional presence for automations	 
 - platform: template
   sensors:
     multiple_presence_me:
       friendly_name: "me"
       device_class: presence
       value_template: >-
         {{ is_state('binary_sensor.me_presence', 'on') }}      # bayesian sensor

# I use this as a senor indicating someone is home 		 
 - platform: template
   sensors:
     people_home:
       friendly_name: People Home
       device_class: presence
       value_template: >
         {{ is_state('group.primary_presence', 'on') or
            is_state('input_boolean.guests_present', 'on') or
            is_state('group.extra_device_trackers', 'home')  }}
			
groups.yaml
primary_presence:
  name: Primary Presence for Routines
  icon: mdi:account-multiple
  view: no
  entities:
    - binary_sensor.me_presence          # bayesian sensor
    - binary_sensor.spouse_presence      # bayesian sensor
	
extra_device_trackers:
  name: Guest Presence Sensors
  icon: mdi:account-multiple
  view: no
  entities:
    - device_tracker.common_guest_visitor	#asuswrt 

input_selects.yaml
  house_mode:
      name: Mode
      options:
          - Home
          - Away
          - Night
          - Extended Away
		  
sensors.yaml
# adding this to my automations stops false alerts or triggers
  - platform: uptime
    name: "HA runtime in minutes"
    unit_of_measurement: minutes
	
Automations.yaml
# I use the input select of Home, away, to trigger other autmations
- alias: Home Mode - Leaving
  trigger:
  - platform: state
    entity_id: binary_sensor.people_home
    to: 'off'
  condition:
  - condition: numeric_state
    entity_id: sensor.ha_runtime_in_minutes
    above: 1
  - condition: state
    entity_id: input_select.house_mode
    state: 'Home'   
  action:
  - service: input_select.select_option
    entity_id: input_select.house_mode
    data:
      option: 'Away'

# I list each presence sensor individually just in case I've manually changed the home mode to away for some reason
- alias: Home Mode - Arriving
  trigger:
   - platform: state
     entity_id:
       - input_boolean.me_present       # HK Switch
       - input_boolean.spouse_present   # HK Switch
       - input_boolean.guests_present   # Manually triggered 
     to: 'on'
   - platform: state
     entity_id:
       - device_tracker.mes_iphone          #HA Iphone App - GPS
       - device_tracker.mes_iphone_2        #ASUS/WRT router based 
       - device_tracker.spouse_iphone       #HA Iphone App - GPS
       - device_tracker.spouse_iphone_2     #ASUS/WRT router based 
       - group.extra_device_trackers
     to: 'home'
  condition:
  - condition: numeric_state
    entity_id: sensor.ha_runtime_in_minutes
    above: 1
  - condition: state
    entity_id: input_select.house_mode
    state: 'Away'
  action:
  - service: input_select.select_option
    entity_id: input_select.house_mode
    data:
      option: 'Home'

# I use the to make to turn off the Homekit switch if the bayesian sensor goes off.  More of a safety check
- alias: 'Paul Left'
  trigger:
   - platform: state
     entity_id: binary_sensor.me_presence    # bayesian sensor
     to: 'off'
  condition:
  - condition: numeric_state
    entity_id: sensor.ha_runtime_in_minutes
    above: 1
  action:
  - service: homeassistant.turn_off
    entity_id:
      - input_boolean.me_present                  #HK switch

input_boolean.yaml
guests_present:
  name: We have guests
  icon: mdi:account-multiple 
me_present:
  name: Me
  icon: mdi:account
spouse_present:
  name: Spouse
  icon: mdi:account
2 Likes

This works perfectly! I’d been struggling with “presence” detection. Had tried the ubiquity and nmap compoents. Each kept “losing” my iPhone regularly. Also tried HA iOS app… not so great during development as it seems you need to re-enable location tracking every time you restart HA.

Thanks for the idea and instructions!

That’s interesting, I’ve had much better reliability with nmap than any other component.

iOS app was woeful - long delays between updates, false away reports etc…

Homekit worked for a week or two then started giving false away reports and not registering when the device was home.

I haven’t had one false report with nmap for many weeks. It instantly reports when I’m home but I have it set up to only check every 3 minutes when home so it can take that long to determine I have left the network.

The issue here is the probabilities. Even if one of the sensors is true you will have the bayesian sensor go true. .

To me the point of combining multiple sensors is to get benefit from them; in this case you are not doing so.

FYI - you can also add:

device_class: presence

to the bayesian sensors to set them for presence; that way you don’t need the template sensors doing the same thing.

This does not go true unless I have two other sensors as true. I dont remember the exact probability percentage, but HomeKit OR ASUS being true while the others were false gave me a probability less than .9 so the state was false

Good deal. Makes sense. I did have it as a presence sensor for a while. You’re right about not needing the template though. I guess I just need one of the other. I actually don’t really use the template either. For Automations I just use to ON, or to Off. Having the Bayesian as a presence, really did nothing special for me. Either from Home to Not_home or On to Off both worked the same.

You just don’t have to set the icon to the house; it is by default :wink:

Odd, I have exact same and if any one of the 3 on the overall bayesian goes on…

If two out of three are not true, but one is, and it’s true value is .9, but the others have a false value of X. The combined probability should be less than .9, so false. is your prior and your probabilty threshold the same? I used this notebook viewer

to plug in various numbers and examples to figure it out. Not sure I could figure it out again, haha. Was pretty complex to get set up, at least it was the 1st time I did it

I am identical to you except the input names! I believe the prob_given_false is also added so if one sensor is true; it is 0.9 and then the two false is 0.2+0.4 so you get a 1.0 probability (can’t be over 1).

This is what I see with testing; often a 1.0 probability with only one input on.

- platform: bayesian
  prior: 0.6
  name: 'Johns Presence'
  device_class: presence
  probability_threshold: 0.9
  observations:
   - entity_id: 'input_boolean.john_home'
     prob_given_true: 0.9
     prob_given_false: 0.2
     platform: 'state'
     to_state: 'on'
   - entity_id: 'device_tracker.johns_iphone'
     prob_given_true: 0.9
     prob_given_false: 0.4
     platform: 'state'
     to_state: 'home'
   - entity_id: 'device_tracker.johns_wifi'
     prob_given_true: 0.9
     prob_given_false: 0.2
     platform: 'state'
     to_state: 'home'

- platform: bayesian
  prior: 0.6
  name: 'Kims Presence'
  device_class: presence
  probability_threshold: 0.9
  observations:
   - entity_id: 'input_boolean.kim_home'
     prob_given_true: 0.9
     prob_given_false: 0.2
     platform: 'state'
     to_state: 'on'
   - entity_id: 'device_tracker.kims_iphone'
     prob_given_true: 0.9
     prob_given_false: 0.4
     platform: 'state'
     to_state: 'home'
   - entity_id: 'device_tracker.kims_wifi'
     prob_given_true: 0.9
     prob_given_false: 0.2
     platform: 'state'
     to_state: 'home'

Here’s a couple screen shots of how mine acts

The top presence is HA, then router, then HK, then lastly is the Bayesian Sensor
.