Using multiple trackers in groups to detect presence

I thought I’d share what’s working well for me using multiple trackers for presence (in this example, Owntracks, Nmap, and iBeacons). I gathered the information in bits and pieces thanks to the wealth of information I gathered from others, but nothing seemed to be clearly spelled out in one place. So I hope this is helpful.

For me, the wrinkle that made the difference was figuring out how to use individual trackers in known_devices.yaml and then grouping them in groups.yaml. I found examples that suggested including a device’s MAC addresses within the MQTT tracker. Going that route, just one ‘away’ condition’ from a tracker causes the device to be ‘away.’ In the configuration below, if just one tracker reports a ‘home’ condition, the user’s device is considered to be ‘at home.’ This configuration works a lot better for me because my phone’s Owntracks position tends to periodically drift outside of my Home zone. This method prevents false ‘away’ states.


(1) Set up an MQTT broker.
(2) Set up Owntracks on your phone.
(3) Edit configuration.yaml for Owntracks, discovery, and Nmap

# Discover some devices automatically
discovery:

device_tracker:
  - platform: owntracks
    track_new_devices: yes
    consider_home: 0:03
    waypoints: True
    waypoint_whitelist:
      - user1
      - user2
      - user3
  - platform: nmap_tracker
    hosts:
      - 192.168.1.3
      - 192.168.1.8
      - 192.168.1.83
      # Highly recommended: assign IP address for devices you want to track so you can know where to find them. The three IP addresses above are examples for three mobile devices.
    interval_seconds: 30
mqtt:
  broker: [input your value here]
  port: [input your value here]
  client_id: [input your value here]
  username: [input your value here]
  password: [input your value here]

Home Assistant will create and populate a known_devices.yaml based on discovery that you can edit as apporpriate. Example entries for User 1’s phone:

user1_owntracks:
  hide_if_away: false
  mac: 
  name: User 1 Owntracks
  picture: /local/user1_headshot.png
  track: true
  vendor: 

user1_nmap:
  name: User 1 Nmap
  mac: [input your value here]
  picture: https://home-assistant.io/images/favicon-192x192.png
  track: true
  hide_if_away: no

Note: Other examples suggest combining the device’s MAC address with Owntracks. Leaving trackers separate in known_devices and then creating a group of trackers for each device allows the trackers to individually contribute to the device’s location. If just one tracker reports that it is home, then HA will regard the device as being at home.

(4) Edit configuration.yaml to refer to groups.yaml and automations.yaml:

group: !include groups.yaml
automation: !include automations.yaml 

(5) Create a groups.yaml file and in it list the trackers for each user:

user1: device_tracker.user1_owntracks, device_tracker.user1_nmap
user2: device_tracker.user2_owntracks, device_tracker.user2_nmap
user3: device_tracker.user3_owntracks, device_tracker.user3_nmap

(6) Configure your iBeacons:

One or more iBeacons can be used with Owntracks to help ‘anchor’ your Home location. More info is available here. Power on your iBeacons, configure them as instructed by the manufacturer, and then add a zone for each iBeacon in Owntracks.

(7) Create automation in automations.yaml based on a user’s presence. This example toggles a light when User 1 is home/away:

  - alias: User 1 is away
    trigger:
      platform: state
      entity_id: group.user1
      to: 'not_home'
    action:
      - service: homeassistant.turn_off
        entity_id: light.user1_presence

  - alias: User 1 is home
    trigger:
      platform: state
      entity_id: group.user1
      to: 'home'
    action:
      - service: homeassistant.turn_on
        entity_id: light.user1_presence

(8) Create automation in automations.yaml based on the group’s presence. This example toggles a light when all users are home/away:

   - alias: Group is away
    trigger:
      platform: state
      entity_id: group.all_devices
      to: 'not_home'
    action:
      - service: homeassistant.turn_off
        entity_id: light.group_presence

  - alias: Group is home
    trigger:
      platform: state
      entity_id: group.all_devices
      to: 'home'
    action:
      - service: homeassistant.turn_on
        entity_id: light.group_presence
11 Likes

Nice guide. I was speaking to you on Reddit about this!

1 Like

While this is a great workaround, I somewhat feel there should be a global setting to set / flip the default tracker logic to the users preference…

Any away or any home.

Which beacons do you use ?

Lars

I opted for a few of the RadBeacon USBs:

Thanks!

One question though - I use a group for ‘all occupants’. If each occupant is a group, can I nest each ‘occupant group’ into the ‘all occupants’ group?

I will test this soon when I get a chance, and edit an answer (if not already answered)

In the config I describe, you’ll wind up with a card in the HA UI for each user, and each user will have the devices “belonging” to them under their names. Those cards technically aren’t a “group,” though I suppose it makes sense to think of them that way since the user’s devices are… um… grouped. :wink:

That’s thanks to these entries in groups.yaml:

user1: device_tracker.user1_owntracks, device_tracker.user1_nmap
user2: device_tracker.user2_owntracks, device_tracker.user2_nmap
user3: device_tracker.user3_owntracks, device_tracker.user3_nmap

The presence of all users together – so all users, and all of their devices – is then handled by entity_id: group.all_devices. That’s not anything that you have to code; it’s an entity provided by HA. Here’s what’s in my automations.yaml that acts on the state of entity_id: group.all_devices:

  - alias: Group is away
    hide_entity: True
    initial_state: 'on'
    trigger:
      platform: state
      entity_id: group.all_devices
      to: 'not_home'
    action:
      - service: homeassistant.turn_off
        entity_id: light.garage_group_presence

(Not to confuse the issue but in the above, that’s not really a “light” in my system. It’s virtual – a kludged on/off to keep track of who’s home and who’s not.)

Does that answer your question?

2 Likes

Yes, thanks. I have implemented this and it works great.

Regarding the approach I was thinking about - I am using a custom UI, the ‘Tiles’ interface - Custom UI: Tiles - which relies on Groups for the display. So I tried to nest the “device groups” (Kelly and Matt) into a group used for “anyone home” (the ‘Us’ group) - and as you can see here, it worked. So it does appear you can nest groups to achieve your approach.

image

Hey there, thanks for sharing your setup. It definitely seems to work out fine. I’m curious though if you have found a way to add the icon on top of the default view showing the device tracking, but then as group. Because groups (like the group of device trackers) of course are shown as tile and not on top…

My setup makes pretty extensive use of groups and views, and I have a group named Presence and Sensors. At the top of that tab is where my three user cards appear. Maybe that would work for you? In config.yaml:

group: !include groups.yaml

And then in groups.yaml:

default_view:
  view: yes
  name: Name_Of_View
  entities:
    - group.User 1
    - group.User 2
    - group.User 3

Each user will have his own tile, but they’ll all appear at the top if you list any other entities/groups after that. For example:

default_view:
  view: yes
  name: Presence and Sensors
  entities:
    - group.User 1
    - group.User 2
    - group.User 3
    - group.motion_events
    - group.weather

… etc. …

Thanks for further sharing your setup. Interesting things to configure and try out.

I conclude that showing an icon with home/away indicator from a group isn’t a possiblity. Maybe something for a future release!

Someone’s probably figured that out. I haven’t tackled that one, but I’m sure it’s doable. What I did do (of necessity) is to have users’ presence turn on/off virtual switches: one for each user, and then another one representing the group. My instance of Home Assistant is part of a larger system, and I use the virtual switch on the HA side to communicate presence conditions to that larger system. So in addition to the cards, there’s also a visual indicator of presence from the switch position.

Those virtual switches are what I was referring to in that earlier post in this thread.

Reviving this to say thanks and share a quick template sensor i threw together.

- platform: template
  sensors:
    josh_home_away:
      friendly_name: Josh
      value_template: >-
        {% if is_state('group.josh', 'home') %}
          Home
        {% elif states.device_tracker.josh_phone.state != 'not_home' %}
          {{ states.device_tracker.josh_phone.state }}
        {% else %}
          Away
        {% endif %}
      icon_template: >-
        {% if is_state('group.josh', 'home') %}
          mdi:account
        {% else %}
          mdi:account-outline
        {% endif %}

I haven’t done a ton of testing on it yet but basically it checks the group status for home first. If not home it will check for the status of the Owntracks device. In my case this is ‘josh_phone’. If a zone is found then that will be displayed. And finally it gives up and displays Away.

The icon template will flip from a blue user icon if home to an outlined icon if not home.

With a little work I may be able to build this sensor into a package that loops across a specific groups such as ‘users’ to auto build these sensors when HA starts. Not sure on that tho as I’m still very new to package building.

EDIT: I built this so i could hide the card with multiple devices in it and display a single presence icon/status in my Presence view.

2 Likes

Nice work!

How it works? I mean, when given group is considered as home or out of home? Does all the tracker’s need to have the same value? If not, which one has the priority?

Say you have four family members who are tracked:

Fred
Sally
Big Sis
Kiddo

If one of the individuals is home, then the Group is considered to be Home. That’s handled by Home Assistant automatically, and otherwise there is no priority or dependency among the individual trackers. But you could pretty easily create automations based on, say, when Fred and Sally are Away but Big Sis and Kiddo are At Home.

When using groups have you figured out a way to show if your in a zone?
Mine show only home or away, even if owntracks is in a zone.
i have a workaround for now usind nodered and mqtt but if you have a better soulution im interested

Yes, it’s not hard. You use Region Monitoring

https://owntracks.org/booklet/features/location/

in OwnTracks in conjunction with Zones in Home Assistant

Once you’ve got a good system going with your own mobile device then you can Export Waypoints from OwnTracks to easily share the list with other users. Your recipient just imports them.

Did you figure this out? Would you mind share your solution?
Thanks

Here’s what my setup looks like. Is this what you’re after?

The 4 boxes to the left are for each of the four family members. The zone’s name will show for each person if the name of the zone is known; otherwise it shows “Away.”

My entire system is really three systems that work in tandem. So the right box is for presence switches that talk to those other systems.

1 Like