Unifi device tracker filling up with hundreds of unknown mac-addresses. Dont track new clients?

Hi shbatm this is amazing! I’ve been having so many issues with random macs showing up and the Tier 3 at UniFi was not very helpful. I’m running the latest version for everything right now but unfortunately I’m not very familiar on how to implement your script. Would you be kind to share like a step-by-step tutorial so I can add the script to the Cloud controller and automated? Let me know if you can help.

Thank you,

Steven

@cyberjson: Using the script should be pretty straightforward if you have a device running Linux with Python 3 available:

  1. Save the script somewhere (home directory is fine).
  2. Edit the username, password, cloud_key_ip, controller_port and site_name to match your controller info (first several lines at the top of the file).
  3. Test the script with:
    python3 /path/to/script/purge_clients.py
    
  4. Set up cron to run the script daily:
    crontab -e
    # Add this to the bottom of the file:
    @daily /usr/bin/python3 /path/to/script/purge_clients.py
    # Save!
    

Thank you you’re amazing! I guess I can use my Raspberry Pi :slight_smile: Is there a way to run it directly in the Unifi CloudKey?

Not sure; I don’t have a cloud key. It should run on anything that has network access to the controller/key so a RPi would work fine.

Awesome thanks again!

I did this but I got a syntax error line 13:

#!/usr/bin/env python3
import requests
from urllib.parse import urljoin

username = “username”
password = r"password"

cloud_key_ip = “192.168.1.2”
controller_port = 8443
site_name = “Controller”

base_url = “https://{192.168.1.2}:{8443}”.format(
cloud_key_ip=cloud_key_ip, controller_port=controller_port

Error is: Traceback (most recent call last):
File “/home/pi/Desktop/purge_clients.py”, line 13, in
cloud_key_ip=cloud_key_ip, controller_port=controller_port
IndexError: tuple index out of range

Don’t update the base_url line. It’ll get the values from everything else.

Thanks again for your suggestions. I got:

Successfully logged in

Retrieve client list

0 clients identified for purge

purge complete

It has anything to do with using a CloudKey gen2+?

I had to modify your script because I am running on the UDM-Base (’/api/auth/login’ for login, ‘/proxy/network’ added in front of the other API calls). I am able to retrieve macs, but when I call the

payload = {"cmd": "forget-sta", "macs": macs}
url = urljoin(base_url, "/proxy/network/api/s/{site_name}/cmd/stamgr".format(site_name=site_name))
resp = sess.post(url, json=payload)

I get a 404 response status “Not Found”. Any ideas on how to fix that?

Did you ever get this going? I get the same response.

@cyberjson @ManFromKC

I made this yesterday: https://github.com/wbradmoore/unifi-cleanup

It is working with controller version 6.0.45 on my Cloud Key gen2, but it should work on other devices.

For reference, these are the types of records it deletes:

I’ve installed the tool and requirements in a venv using:

pip install -r requirements.txt

After passing the arguments as needed (replaced with my own address, username and password)

python3 unifi-cleanup.py --port 443 192.168.1.1 <username> <password>

it first failed with:

Traceback (most recent call last):
  File "/Users/mieszko/GitHub/unifi-cleanup/unifi-cleanup.py", line 2, in <module>
    import asyncio,async_timeout
ModuleNotFoundError: No module named 'async_timeout'

After installing the missing dependency (pip3 install async_timeout) it fails with:

Starting aioUniFi
Traceback (most recent call last):
  File "/Users/mieszko/GitHub/unifi-cleanup/unifi-cleanup.py", line 106, in <module>
    asyncio.run(
    ~~~~~~~~~~~^
        main(
        ^^^^^
    ...<5 lines>...
        )
        ^
    )
    ^
  File "/opt/homebrew/Cellar/[email protected]/3.13.5/Frameworks/Python.framework/Versions/3.13/lib/python3.13/asyncio/runners.py", line 195, in run
    return runner.run(main)
           ~~~~~~~~~~^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.13.5/Frameworks/Python.framework/Versions/3.13/lib/python3.13/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.13.5/Frameworks/Python.framework/Versions/3.13/lib/python3.13/asyncio/base_events.py", line 725, in run_until_complete
    return future.result()
           ~~~~~~~~~~~~~^^
  File "/Users/mieszko/GitHub/unifi-cleanup/unifi-cleanup.py", line 59, in main
    controller = await unifi_controller(
                 ^^^^^^^^^^^^^^^^^^^^^^^
    ...<8 lines>...
    )
    ^
  File "/Users/mieszko/GitHub/unifi-cleanup/unifi-cleanup.py", line 14, in unifi_controller
    controller = aiounifi.Controller(
        host,
    ...<6 lines>...
        callback=callback,
    )
TypeError: Controller.__init__() got an unexpected keyword argument 'username'
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x106bbb380>

Is there anything I can do @wbradmoore ?

sorry @spitf1r3 , looks like aiounifi has diverged too much, and it’s more than just a quick fix. i believe the home assistant unifi integration has some sort of option to not include these entities now, though? i recall seeing some option to “filter phantom (or something) clients” the last time i added the unifi integration, and i don’t see these any more

Well for now I’ve removed the integration (with 17000 entities!) and re-added it which fixed the issue (I was conservative with enabling the options now). For some reason I’ve seen 3000 (!) devices with some random MAC addresses (most of them starting with 00:00:00:00:00:00) and no IPs/zero traffic. I have no idea where they came from. I was hoping I could get rid of them in Unifi’s UI too.

Did you find how to get it to only add the devices you select and not any other that connects to your network?

I’ve just cleared my integration and are re-adding it. As I had over 2k entities.