HomeKit as a Presence Sensor

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
.

Could you send a screen shot of the bayesian card for each so I can see the probabilities listed? I get very different behavior then you; so that may help debug.

I only have one Bayesian card. It is set up exactly as listed above. Not sure what you’re asking me to send.

If you click on the bayesian sensor on the screenshot you posted above does it pop open a card that shows the probabilities?