Separate group.all_devices by device tracker

I have 2 device trackers. One is google_maps the other is unifi. The group.all_devices combines all devices found by both trackers automatically. But what I am trying to do is create a separate group for each of these trackers, and have device added to each group automatically by the tracker. I can’t seem to do this…the only way I can create a group is to manually add each entity to that group which doesn’t make sense. Is there another way to do this? I’d like to create a separate tab/view at the top for each tracker and have devices only appear in the correct one.

I don’t think you can do this automatically, you’ll have to manually add the entities you wish to belong to each group…

Damn. It is very tedious and doesn’t make much sense to have to manually add devices that connect to my network each time there is a new device or I let someone connect to wifi. It would be nice to have something like “group.device_tracker.unifi” automatically created that includes all entities created from only that tracker.

Maybe check this post to see if gives you an inspiration to approach it in a different way?

Alternatively you could look at appdaemon?

There is a group.set service, so in theory you could write automations that maintain a group, at HA startup and/or as device_tracker entities are created (by triggering on the corresponding device_tracker_new_device event.) The trick is figuring out how to filter the entities. In your case, how can one tell the difference between a google_maps entity and a unifi entity? Is there some consistency in naming, attributes, etc.?

E.g., with my Life360 device tracker platform, I could write an automation that used the group.set service and specified the following for the entities parameter:

{% for s in states.device_tracker if 'at_loc_since' in s.attributes -%}
{% if not loop.first %},{% endif %}{{ s.entity_id }}
{%- endfor %}

Yes google_maps creates entities like this:

device_tracker.google_maps_###########
with an attribute:
source_type: gps

Unifi creates entities:

device_tracker.harmonyhub
with an attribute:
source_type: router

Ok, that’s good. Then this might work, or at least be a start:

  - alias: Maintain Google Maps entity group
    trigger:
      - platform: homeassistant
        event: start
      - platform: event
        event_type: device_tracker_new_device
    action:
      - service: group.set
        data_template:
          object_id: dt_google_maps
          name: Google Maps Device Trackers
          entities: >
            {% for s in states.device_trackers
                 if s.entity_id.startswith('device_tracker.google_maps_') -%}
              {% if not loop.first %},{% endif %}{{ s.entity_id }}
            {% endfor %}

Add another one just like this for the unifi group, but of course, change the names, and for the for loop, change it to:

            {% for s in states.device_trackers
                 if s.attributes.source_type == 'router' -%}

Haven’t really tried this exactly, so it might need some tweaking.

Great thanks I will try it out and let you know.

Thanks for posting this.

states.device_trackers
did not work for me.

I changed your snippet to that and sort my devices per scanner:

Unifi:
{% for s in states.device_tracker if s.attributes.scanner == 'UnifiScanner' -%}            
{% if not loop.first %}{% endif %}{{ s.entity_id }}  {{ s.attributes.friendly_name }} {{ s.state }}
{% endfor %}
Nmap:
{% for s in states.device_tracker if s.attributes.scanner == 'NmapDeviceScanner' -%}            
{% if not loop.first %}{% endif %}{{ s.entity_id }} {{ s.attributes.friendly_name }} {{ s.state }}
{% endfor %}
DDWrt:
{% for s in states.device_tracker if s.attributes.scanner == 'DdWrtDeviceScanner' -%}            
{% if not loop.first %}{% endif %}{{ s.entity_id }}  {{ s.attributes.friendly_name }} {{ s.state }}
{% endfor %}

Yep, sorry, that was a typo. I said I didn’t try it. :wink: I’ll go back and update my post without the s. Thanks! Hmm, apparently it won’t let me edit that post anymore.