List & count entities / devices connected to specific AP

Hi,
Please could someone give me a hand with a template to list all the devices connected to a specific access point (unifi)?

I understand I can check if a specific device is connected to a specific AP with the following template, however I wish to expand this to list all the devices connected to each AP by mac address on a manual card - is this possible?

{{ is_state('device_tracker.my_phone', 'ap_mac') == "00:00:00:00:00:00" }}

I would also like a count for each AP.

Thanks in advance!

For the count:

{{ states.device_tracker | selectattr('ap_mac', 'eq', '00:00:00:00:00:00') | map(attribute='entity_id') | list | count }}

For a comma separated list of entity_ids:

{{ states.device_tracker | selectattr('ap_mac', 'eq', '00:00:00:00:00:00') | map(attribute='entity_id') | list | join(", ")  }}

For a comma separated list of friendly names:

{{ states.device_tracker | selectattr('ap_mac', 'eq', '00:00:00:00:00:00') | map(attribute='name') | list | join(", ")  }}

Hi, thanks for the swift response!

I have just tried this and the count returns ā€˜0ā€™ (and no devices are listed)

Could this be to do with the comparator used on the ap_mac?

Ta

Jon

Did you replace 00:00:00:00:00:00 with the mac of your AP?

Yep :slight_smile:

Pick any of the device trackers and post the attributes listed in the developer tools states menu.

source_type: router
is_wired: false
hostname: amazon-9fae3ae60
mac: 'd0:d0:d0:d0:d0:d0'
name: Lounge FireTV
oui: ''
_is_guest_by_uap: false
ap_mac: 'a0:a0:a0:a0:a0:a0'
authorized: true
essid: Nacho Wifi
ip: xx.xx.xx.xx
is_11r: false
is_guest: false
noted: true
qos_policy_applied: true
radio: na
radio_proto: ac
vlan: 0
friendly_name: Lounge FireTV

Try removing the quotes from around the mac.

{{ states.device_tracker | selectattr('ap_mac', 'eq', 00:00:00:00:00:00) | map(attribute='entity_id') | list | count }}
{{ states.device_tracker | selectattr('ap_mac', 'eq', 00:00:00:00:00:00) | map(attribute='entity_id') | list | join(", ")  }}
{{ states.device_tracker | selectattr('ap_mac', 'eq', 00:00:00:00:00:00) | map(attribute='name') | list | join(", ")  }}

That works for me (selecting on a different attribute):

It doesnā€™t like the colons in the mac:

Error rendering template: TemplateSyntaxError: expected token ā€˜,ā€™, got ā€˜:ā€™

Hang on. Post the whole config where you use the template. I might have an idea what the problem is.

Great stuffā€¦Thanks

My intention is to have it on a card, but just testing it in the Developer Tools > Template menu currently.

Oh ok.Thatā€™s not the problem then. Was thinking you were doing something like this:

value_template: '{{ states.device_tracker | selectattr('ap_mac', 'eq', '00:00:00:00:00:00') | map(attribute='entity_id') | list | count }}'

Instead of this:

value_template: "{{ states.device_tracker | selectattr('ap_mac', 'eq', '00:00:00:00:00:00') | map(attribute='entity_id') | list | count }}"

One last try:

{{ states.device_tracker | selectattr('ap_mac' == '00:00:00:00:00:00') | map(attribute='entity_id') | list | count }}
{{ states.device_tracker | selectattr('ap_mac', == '00:00:00:00:00:00') | map(attribute='entity_id') | list | join(", ")  }}
{{ states.device_tracker | selectattr('ap_mac', == '00:00:00:00:00:00') | map(attribute='name') | list | join(", ")  }}

Donā€™t forget to replace the mac addresses, with the the AP addresses.

Still getting ā€˜0ā€™ returned :woozy_face:

Yeah on closer inspection my section template is not working for entity attributes, only state (itā€™s actually returning all my trackers in the example above).

Hopefully someone else may be able to help. I donā€™t like @ing people but if I see someone I know can help is online Iā€™ll point them this way.

@tom_l was close. To get to an ā€œattributeā€ of a state object in the selectattr() function you need to use 'attributes.ATTR'. (See State Objects.) So:

 {{ states.device_tracker | selectattr('attributes.ap_mac', 'eq', '00:00:00:00:00:00') | list | count }}
{{ states.device_tracker | selectattr('attributes.ap_mac', 'eq', '00:00:00:00:00:00') | map(attribute='entity_id') | list | join(', ')  }}
{{ states.device_tracker | selectattr('attributes.ap_mac', 'eq', '00:00:00:00:00:00') | map(attribute='name') | list | join(', ')  }}
1 Like

Awesome, thats done it! Thanks both!

If I want to add this to a card?

Thanks Phil.

Auto entities would be a good fit for this.

I am able to count to the LAN connected devices, i.e. home state but still not able to find the way how to list connected IPs (ip from device_tracker attributes )
Could anybody here advice pls?

Edit:
I did it :slight_smile:
{{ states.device_tracker | selectattr('attributes.ip') | map(attribute='attributes.ip')| list | join('\n') }}