Unifi AP's as presence detection for automation

Hi,

I’ve got a bunch of Unifi AP’s scattered and I want to use these to determine if someone is presence in a zone of my home. If a person is seen, for instance in the garden, I wish to turn on the lights automatically.

To set this up, I already tried to use the unifi_ap (UniFi AP - Home Assistant) approach. But for some reason I only see my AP as connected device instead of my other devices. Does any know now if this is possible?

I’ve just looked at the json returned from an Appdaemon app I use and it does return the MAC of the AP (ap_mac) a client is connected to, maybe it could be added to the attributes in the device_tracker in the unifi integration.

That way you could check if a device is connected to an access point and automate from that.

Be aware though that devices don’t always connect to the nearest AP and switching between APs is not very quick

This is the returned JSON for a client

  {
    'site_id': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    'ap_mac': 'xx:xx:xx:xx:xx:xx',
    'assoc_time': 1691693844,
    'latest_assoc_time': 1692868612,
    'oui': 'Espressif Inc.',
    'user_id': 'xxxxxxxxxxxxxxxxxxxxxxxx',
    'last_ip': '192.168.40.103',
    'first_seen': 1637689931,
    'last_seen': 1692869617,
    'is_guest': False,
    'fingerprint_engine_version': '',
    'disconnect_timestamp': 1688045857,
    'dev_id_override': 2058,
    'is_wired': False,
    'dev_family': 16879,
    'mac': 'xx:xx:xx:xx:xx:xx',
    'score': 87,
    'hostname': 'front-garden-lightbulb-b2',
    'device_name': 'Embedded OS',
    'dev_cat': 16879,
    'local_dns_record_enabled': False,
    'use_fixedip': False,
    'local_dns_record': '',
    'usergroup_id': '',
    'fingerprint_override': True,
    'last_connection_network_name': 'network NoT',
    'fingerprint_source': 0,
    'dev_id': 36994,
    'noted': True,
    'last_connection_network_id': 'xxxxxxxxxxxxxxxxxxxxxx',
    'network_id': 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
    'fixed_ap_enabled': False,
    'name': 'front-garden-lightbulb-b2',
    '_id': 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
    'fixed_ip': '192.168.40.103',
    'dev_vendor': 0,
    'fw_version': '',
    'wlanconf_id': 'xxxxxxxxxxxxxxxxxxxxxxxxxxx',
    '_uptime_by_uap': 685655,
    '_last_seen_by_uap': 1692869617,
    '_is_guest_by_uap': False,
    'channel': 1,
    'radio': 'ng',
    'radio_name': 'wifi0',
    'essid': 'network NoT',
    'bssid': 'xx:xx:xx:xx:xx:xx',
    'powersave_enabled': False,
    'is_11r': False,
    'user_group_id_computed': 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
    'anomalies': -1,
    'anon_client_id': 'xxxxxxxxxxxxxxxxxxxxxxx',
    'ccq': xxx,
    'dhcpend_time': 2050,
    'idletime': 11,
    'ip': '192.168.40.103',
    'noise': -113,
    'nss': 1,
    'rx_rate': 6000,
    'rssi': 48,
    'satisfaction': 100,
    'satisfaction_now': 100,
    'satisfaction_real': 100,
    'satisfaction_reason': 0,
    'signal': -48,
    'tx_mcs': 7,
    'tx_power': 22,
    'tx_rate': 72222,
    'tx_retry_burst_count': 162,
    'hostname_source': 'ubios',
    'radio_proto': 'ng',
    'channel_width': 20,
    'satisfaction_avg': {
      'total': 87096141,
      'count': 893152
    },
    'uptime': 1175773,
    'tx_bytes': 3058331,
    'rx_bytes': 6785034,
    'tx_packets': 55923,
    'rx_packets': 96091,
    'tx_retries': 3470,
    'wifi_tx_attempts': 59390,
    'authorized': True,
    'qos_policy_applied': True,
    '_uptime_by_usw': 2325473,
    '_last_seen_by_usw': 1692869653,
    '_is_guest_by_usw': False,
    'sw_mac': 'xx:xx:xx:xx:xx:xx',
    'wired_rate_mbps': 1000,
    'sw_depth': 1,
    'network': 'network NoT',
    '_uptime_by_ugw': 2325473,
    '_last_seen_by_ugw': 1692869653,
    '_is_guest_by_ugw': False,
    'gw_mac': '68:d7:9a:62:aa:93',
    'gw_vlan': 40,
    '_last_reachable_by_gw': 1692869634,
    'bytes-r': 0.0,
    'tx_bytes-r': 0.0,
    'rx_bytes-r': 0.0,
    'roam_count': 3,
    'vlan': None
  }
1 Like

Thanks!

What app are you running to get this information?

Problem is I need to figure out what device it is and translate it (with hostname, but iPhone does not have any).

Eventually one of the usecaes: I’m in the garden for a longer time and sunset is already started. I want to enable the lights of my canopy.

Look at disabled entities. I think non-APs are disabled by default.

I use Unifi integration and this template to determine where a sonos speaker is.

      - condition: template
        value_template: '{{ state_attr(''device_tracker.sonoszp_2'', ''ap_mac'') == ''e0:63:da:e9:f5:fa'' }}
1 Like

Haha, no idea how I missed that, I looked and couldn’t see it.

I use appdaemon, but as said above, it exists in the device tracker already

Hmm… can you explain a bit better?

Currently: I run ‘Unifi Network’, ‘Protection’ and now ‘Unifi Direct’ also. When going to Config > Entities , where do I have to look?

This sounds a bit like ‘reverse engineering’. So… you make a list of devices you want to track and check if their current ap_mac is the Mac address of a given AP? This means I need to check against multiple ap_macs. Or… I create a list of devices based on who have ap_mac = AP Mac address. Sounds do-able.

For some reason only a few devices (airco, vacuum cleaner) have ap_mac in their attributes. My mobile devices are missing.

The entities for “Unifi Network” is the right place to look, but you might have a filter that only shows enabled ones. Look at the upper right corner.

I’ve just looked at getting the name for the Access Points into a sensor from the MAC addresses and using jinja tools macros:

Macro. Set this up once filling in the MACS and Rooms/Access Point names

{% macro unifi_ap(entity_id) %}
{% if state_attr(entity_id, 'ap_mac') == 'xx:xx:xx:xx:xx:xx' %}
    Living Room
{% elif state_attr(entity_id, 'ap_mac') == 'xx:xx:xx:xx:xx:xx' %}
    Downstairs
{% elif state_attr(entity_id, 'ap_mac') == 'xx:xx:xx:xx:xx:xx' %}
    Bedroom
{% elif state_attr(entity_id, 'ap_mac') == 'xx:xx:xx:xx:xx:xx' %}
    Garage
{% else %}
    Unknown
{% endif %}
{% endmacro %}

Template Sensor. This shows what access point the device is connected to. You can have as many as you need of these for each device you want to track

    - name: "Phone Access Point"
      state: >
        {% from 'tools.jinja' import unifi_ap %}
        {{ unifi_ap('device_tracker.phone') }}

Macro docs in case you need it:

1 Like

hi,
if I understand you correctly you want to track a device (e.g. smartphone of a person) based on the access point it is connected.
I have 2 APs, one upstairs and one downstairs.
If my device switches from the AP MAC upstairs to the AP MAC downstairs the lights upstairs turn off automatically.

alias: "turn off lights when downstairs"
description: turn off lights when downstairs
trigger:
  - platform: state
    entity_id:
      - device_tracker.pixel_6_unifi
    attribute: ap_mac
    from: 74:ac:b9:6c:e8:60
    to: 74:ac:b9:6c:e7:ee
condition: []
action:
  - if:
      - condition: device
        type: is_on
        device_id: 50828300e444c66c006638c69da9db03
        entity_id: 8b56fd481e7e432a8f4d8ab3100b4708
        domain: light
    then:
      - type: turn_off
        device_id: 50828300e444c66c006638c69da9db03
        entity_id: 8b56fd481e7e432a8f4d8ab3100b4708
        domain: light
mode: single

I just added the AP MACs hardcoded in the automation, didn’t bother to create a template since I only have 2 APs.
Just make sure to enable the unifi device trackers for the devices you want to track (settings → integrations → unify network → entities → enable the device trackers), otherwise you can’t select them as triggers.

Hope this helps.

1 Like

Awesome, thanks for sharing!

Thanks, but my problem is my apple devices (iOS & MacOS) won’t show up in the list. My guess apple is making it hard to track the devices. I already disabled private WiFI so my BSSID should stay the same (and easier to track).

This has nothing to do with apple. As mentioned previously, by default, the Unifi integration disables the entities for individual devices. You just need to find them and enable.

I found a lot of private devices (device_tracker.unifi_XX_XX_XX_XX_XX_XX_default, private modus?) and my tablet. I enabled it but it’s not showing in the Unifi > Devices list. Even after rebooting Home Assistant.

Make sure your device isn’t using randomised MAC addresses (it’s a setting). Turn it off or it will never work for tracking :slight_smile:

They are entities, not devices

HIs Tablet is a device - in HA it is an entity I give you that, but I was saying to turn off the random mac address on the physical tablet DEVICE.

I was answering Robin’s message, not yours:
Screen Shot 2023-08-30 at 1.46.46 PM

I don’t know if random macs are relevant here, but I do know that they won’t find it in the devices, where they were looking, but rather in entities.