List only one device tracker

Hello,

in my automations I use this

for_each: '{{ expand(''group.users'') | selectattr(''state'', ''eq'',''home'')
        | map(attribute=''attributes.source'') | list  | replace(''device_tracker.'',
        "notify.mobile_app_") }}'

to list users who are at home and only notify them.

the issue now is that I added ping as an aditional devicetracker, because on android phones gps sometimes was to laggy.

now people have multiple device trackers. and I only want to send notifications to phones because I cannot send other notifications.
For now I just added a bunch of replaces like this.

{{ expand('group.users') | selectattr('state', 'eq','home') |
  map(attribute='attributes.source') | list  | replace('device_tracker.',
  "notify.mobile_app_") | replace('192_168_180_42',
  "sm_g780f") 
  | replace('192_168_180_43',
  "bkmobile")}}

but I would prefer to list the services like this

{{ expand('group.users') | selectattr('state', 'eq','home') |
  map(attribute='attributes.device_trackers') | list  | replace('device_tracker.',
  "notify.mobile_app_") }}

result:

[
  [
    "notify.mobile_app_sm_g780f",
    "notify.mobile_app_192_168_180_42"
  ],
  [
    "notify.mobile_app_bkmobile",
    "notify.mobile_app_192_168_180_43"
  ]
]

and then exlude the once having 192 in it and also get rid of the double array.
does someone know how to accomplish this? I tried but couldn’t find the correct way.

You can use the reject() filter function in combination with integration_entities() to reject the device trackers from the ping integration and a list of lists can be flattened using sum():

{{ expand(state_attr('group.users', 'entity_id') | select('is_state', 'home'))
| sum(attribute='attributes.device_trackers', start=[])
| reject('in', integration_entities('ping')) | list
| replace('device_tracker.', 'notify.mobile_app_') }}

EDIT: Updated to check state of person entity.

1 Like

really nice,
I will try when I get home, maybe changing reject into select would be even nicer if in feature I decide to add some other trackers.

{{ expand(state_attr('group.users', 'entity_id'))
| sum(attribute='attributes.device_trackers', start=[])
| select('in', integration_entities('mobile_app')) | select('is_state', 'home') | list 
| replace('device_tracker.', "notify.mobile_app_") }}

edit:
I made some other minor twist,
if the user is at home the notification should apply, not when the device is at home

{{ expand(state_attr('group.users', 'entity_id')) | selectattr('state', 'eq','home')
| sum(attribute='attributes.device_trackers', start=[])
| select('in', integration_entities('mobile_app')) | list 
| replace('device_tracker.', "notify.mobile_app_") }}

android can be a little behind on gps update, therefor the ping is added