Unifi: see number of clients connected to AP

I’ve got a couple of AP’s and I want to turn them off when no clients are connected.

Especially my wall AP’s (UAP-IW-HD, U6-IW) are only in use when we’re working at home, so that’s only on certain days between 8.00 and 17.00.

I’ve got enough other sensors (power measurement, motion sensors) to detect if they should be turned on again.

I use template sensors to count devices connected to APs, you’ll need one for each AP and change the MAC to suit. This assumes you are using the Unifi Network integration

    - name: "Devices Connected to Bedroom Access Point"
      unique_id: 9525940b-5ec8-4598-890c-99138bef5ffb
      state_class: measurement
      state: >-
        {% set connected_devices = states.device_tracker
        | rejectattr('state', 'in', ['unavailable','unknown'])
        | selectattr('attributes.ap_mac', 'eq','xx:xx:xx:xx:xx:xx') 
        | list
        | count %}
        {{ connected_devices }}
1 Like

Voted. Here’s my command-line sensor to do the same job, not relying on device_tracker entities in HA:

- sensor:
    name: "Music Room AP client count"
    command: "ssh -i SSH_KEY USER@AP_IP mca-dump"
    unique_id: 2df86a53-7881-4d48-a4d3-ab71f85be7e4
    unit_of_measurement: 'clients'
    value_template: >
      {% set ns = namespace(stac=[]) %}
      {% set uplink = value_json.get('uplink_bssid', 'no') %}
      {% for sta_table in value_json['vap_table']|selectattr('name','contains','ath')|map(attribute='sta_table') %}
        {% set ns.stac = ns.stac + sta_table|map(attribute='mac')|reject('eq',uplink)|list %}
      {% endfor %}
      {{ ns.stac|count }}

Works even with wireless links, reporting the same as the UI frontend:

4 Likes