Presence detection with multiple devices & multiple trackers

Thank you for this, this helped me along for my presence detection system. The problem I had with yours was on the away part. It would trigger if any one of the sensors were ‘away’. I found that things like wifi sleep on a phone would give a false positive.

I started looking for a way to only trigger ‘away’ when all of the configured sensors were ‘away’. I came up with a solution using a value template for the away trigger.

Below is my automation code.

-alias: "Away"
  condition:
    condition: state
    entity_id: input_boolean.home    
    state: 'on'
# Will only trigger if all are 'not_home'
  trigger:
      platform: template
      value_template: "{% if (is_state('device_tracker.1','not_home')) and (is_state('device_tracker.2','not_home')) and (is_state('device_tracker.3','not_home')) %}true{% endif %}"
  action:
      service: homeassistant.turn_off
      entity_id: input_boolean.home

- alias: "Home"
  condition:
    condition: state
    entity_id: input_boolean.home
    state: 'off'
# Will trigger if any of the below are 'home'
  trigger:
    - platform: state
      entity_id: device_tracker.1
      state: 'home'
    - platform: state
      entity_id: device_tracker.2
      state: 'home'
    - platform: state
      entity_id: device_tracker.3
      state: 'home'
  action:
      service: homeassistant.turn_on
      entity_id: input_boolean.home
7 Likes

This is a good idea, I also go to test look, thank you!

for should take care of wifi sleeping issues. Moreover, I have updated the sensors to take care of such issues and you can find the updated automations here.

1 Like

this is right

(is_state(‘device_tracker.2’’,‘not_home’)) device_tracker.2 behind You more than one ’

{%- if states.input_boolean.rashmihome.last_changed -%}
  {{ (as_timestamp(now()) - as_timestamp(states.input_boolean.rashmihome.last_changed)) > 300 }}
{%- else -%}
  true
{%- endif -%}

what does this mean? thank you

It checks if the input_boolean.rashmihome was changed in the last 5 minutes and prevents frequent changes.

No, a typo while transferring and anonymising the code here. Fixed now

good :咧嘴笑:

I want to share my best practice when dealing with presence detection and multiple trackers.

I have been a long time user now and my biggest issue have been presence detection.

When you have a family of 4 persons, with all diffrent devices (Apple, samsung, oneplus, sony) and so on, all the devices respond diffrent to diffrent device trackers.

The easy part is to detect when a device is home.
Using asuswrt, ping, owntracks, tasker (on android), bluetooth_tracker and nmap_tracker

My biggest problem is all the false “away” signals because some phone goes in to some powersave / doze mode or somehow just disconnects from the network. Or owntracks not update the location or the gps signal is weak and gives a position outside my home zone.

I guess im not the only one that have a lot of these false “away” entries in your logs, and if you have automations depending on these statuses it can give you some trouble…

My so far best sollution was when i finaly got the idea to use my hallway motoion sensor as a condition.
I dont know why i didn’t think of it earlier… Because no one can leave the house without triggering the hallway motion sensor.
So adding this condition reduced my false aways ALOT…
There are still some false ones, because some nightly trips to the bathroom will allso trigger the hallway motion sensor. but this could be totaly removed if i get my self a door sensor, or a electric door lock to use as the condition instead.

This method allso reduced the time it sometimes took to trigger the away…
On both asuswrt and ping i use to have quite big delays “consider_home” values to remove some false triggers before, but now when i use both asuswrt and ping with the hallway motion sensor as a condition i get some acceptable results.

11 Likes

would you mind giving me a sample snippet for how you implement this?

I am a noob and it seems like the more complicated I make my automation’s the more likely they are to fail, so while I could muddy through it myself, I would like to see your working elegant code.

Thanks,

The code is available in the first post or the linked repo. Everything is working well, so all you need to do is change the entity_id for it to work for you.

ah ok, thanks. Btw your repo is what I have modeled like 99% of my HA setup after, so thanks for sharing everything!

I am glad you found it useful :slight_smile:

Hi Fellow HA Users!
I’m quite new with it, but I learn more day after day. I hope, I can post my problem here, because I think, it’s related to the topic’s address:
I’ve set up HA with my phone (Asus Padfone S, android 6, stock rom) and Owntracks 4 days ago. It worked untill today. I’ve set up my wife’s phone too (Samsung S5, stock rom. Owntracks) with different username, which I have configured together with mine in mosquitto 4 days ago. Today, I just installed the app, and set up the connection. After this, my avatar disappeared from the map, it showed only my wife’s icon for a few minute. After that, the map only shows the zones.
I’ve tried to send ‘null’ messages to the topics, removed all the know devices. nothing changed.
HA gets the coordinates, my batteri sensor shows the battery %, but the map is empty.
Do you have any suggestions?
Thank you!

If you are ‘home’ the map doesn’t show your icon.

umm this link doesn’t seems to work :frowning:

I have updated the link.

1 Like

@mchinery I am trying to do the same thing - can you share your solution that you used with the template sensor?

Thanks!

Tom

I did not implement it in the end. I changed presence detection to Bluetooth only and this has proved very reliable.

I’ve tried editing your code but I’m just getting started with home assistant and being a real linux newb I seem to get stuck…
So i copied your code an added the devices that I want to track (my iphone, wife’s iphone wit router and with owntracks). But it doesn’t seem to do anything. Probably a newby mistake. Could you please look at the code?

# Automation Update
- alias: 'Update Available Notifications'
  trigger:
    platform: state
    entity_id: updater.updater
  action:
    service: notify.notify
    data:
      message: 'Update for Home Assistant is available.'

###############################################################
## Presence
################################################################
- id: update_meta_tracker
  alias: "Update Device Meta Tracker"
  initial_state: 'on'
  trigger:
    # Delayed action for router-based and Owntracks trackers that are not 100% reliable
    - platform: state
      entity_id:
        - device_tracker.iphone1
        - tracker.owntracks1
        - device_tracker.iphone2
        - device_owntracks2
      to: 'not_home'
      for: '00:07:00'
    - platform: state
      entity_id:
        - device_tracker.iphone1
        - tracker.owntracks1
        - device_tracker.iphone2
        - device_owntracks2
      to: 'home'
  action:
    - service: python_script.meta_device_tracker
      data_template:
        entity_id: '{{trigger.entity_id}}'
    - service: logbook.log
      data_template:
        name: "Tracker updated: "
        message: >-
          {%- for state in states if state.entity_id == trigger.entity_id -%}
            {{ state.attributes.friendly_name }} is {{ trigger.to_state.state }}.
          {%- endfor -%} 

This is my view (playing arround with it). But I don’t understand how I can merge the two trackers?
bewoners:
name: Super personen
entities:

  • device_tracker.iphone1
  • device_tracker.iphone2