Monitoring your Unifi AP

Can you please prcie exactly what u did? I fear i dit not understand well :frowning: Dos it work for you? I am running

6.0.43.0 << network controller on UDM 1.8.5 firmware

@thundergreen,

at least for me, it didn’t download the module automatically. I’m running the first version of the script.

So these are the steps I followed:

  1. copied the python script to /config/scripts/ several times as I have 3 APs (UniFi_ap_1.py, UniFi_ap_2.py and unifi_ap_3.py
  2. I downloaded the module (pip3 install unificontrol) on another server and copied the files to /config/scripts/module/

  1. add these lines to each python script:
import sys
sys.path.append('/config/scripts/modules/')

aaa all right … nice way to solve depencies problem but I get those connection or json errors. :frowning:

You will have to pip3 install unificontrol the module first.
As for the other part, I am running home assistant core on windows, so i cannot really help if you’re running the hassOs on raspberry pi etc. as I have zero experience with that. Sorry.

Also, I’ve updated the script in the first post, so check if this solves any problems. Make sure to read the whole script and perhaps comment out anything that might not work for you.

@revin Hey, have you tried with my suggested edit numclients = len(clients) to see if the clients are reported correctly?

I only have one AP so I cannot really test your situation.
After reading the developer’s docs, I assume that this change will only return the clients per AP.

Hi @valvex

I’ve changed the code for the new one. I made no change except my config (user, server…) and it gives me this error:

hass@hass:~/1/unifi$ python3 unifi_ap_1.py
Traceback (most recent call last):
  File "unifi_ap_1.py", line 46, in <module>
    wifi0clients = sum(1 for _ in re.finditer(r'\b%s\b' % re.escape('wifi0'), str(clients)))
NameError: name 're' is not defined


edit:

Now it works. I had to add to the script “import re” and also modify print (f) to print (final). The output is:


hass@hass:~/1/unifi$ python3 unifi_ap_1.py
{"Clients": 41, "Guests": 0, "Clients_wifi0": 11, "Clients_wifi1": 30, "Score": 98, "CPU": "2.7", "RAM": "18.6", "Uptime": "0d 5h 34m", "Score_wifi0": 98, "Score_wifi1": 99, "Activity": "1.7 Mbps", "Update": false}

I’ll check the numbers later and compare with unified controller

@revin Yes,sorry I’ve updated the script many times and forgot to make these edits.

@valvex, no problem. My level of python is ridiculous but I can do some work on it

I’ve updated the the code with your last version

As you could see, remains giving total users instead by AP.

Also, the number of users by SSID doesn’t gives the correct number. you can compare with another sensor that I’m using (first one)

I’ll made some further testing with the code tomorrow

@revin My knowledge of python is also very basic.
In this case I just read the info provided on the endpoints, and then try to extract any useful information.

What you can do is print the results of the endpoints and find out what info you want to extract based on your system

# for example #
devs = client.list_devices(mac)
clients = client.list_clients()
print (clients)
print (devs)
# etc #

Also, have a look at the module’s documentation where all the available endpoints and commands are listed!

HI @valvex,

this module is quite powerful and it seems easy to use.

Unfortunately, I do not have much time during the workdays, but this weekend I will do some tests to get the the number of clients by AP

1 Like

@thundergreen there is a special branch version of unificontrol on github if you are using UDM with latest firmware.

`

pip install git+https://github.com/nickovs/unificontrol.git@udm-support#egg=unificontrol

`

@valvex i need help in getting a list of clients in the controller who have Tx/Rx 0 bps based on time ?

could you help?

Hi,
There is a method to call for statistics with 5 minute granularity (see the docs here)
Maybe you can specify there the tx, rx attributes.
However, I do not personally use it so I can’t really help any further.

Trying to make this work on a HASSOS installation, could you let me know what sensor configuration you are using? I’ve tried to use command line but its not working!

Thanks!

Hi @Stringyb92,

this is my sensor configuration:

  - platform: command_line
    command: 'python /config/scripts/unifi_ap_1.py'
    name: unifi ap 1
    value_template: '{{ value_json.Clients }}'
    unit_of_measurement: Clients
    scan_interval: 60
    json_attributes:
      - Guests
      - Clients_wifi0
      - Clients_wifi1
      - Score  
      - CPU
      - RAM
      - Uptime
      - Score_wifi0
      - Score_wifi1
      - Activity
      - Update

Unfortunately, I didn’t have time to make changes on the code, so I’m running the code from @valvex

Thanks for the help, wanted to make sure i was on the right lines.

Shame i cannot get the script to work due to SSL errors, will try and do some further digging

Can you paste the error log?

Its failing on the certificate, however reading the notes about the SSL this should have been resolved within the python code

Traceback (most recent call last):
  File "unifi1.py", line 21, in <module>
    stat = client.stat_sysinfo()
  File "/config/python_scripts/modules/unificontrol/metaprogram.py", line 125, in wrapper
    return instance(client, *a, **kw)
  File "/config/python_scripts/modules/unificontrol/metaprogram.py", line 103, in __call__
    return client._execute(url, self._method, rest_dict, need_login=self._need_login)
  File "/config/python_scripts/modules/unificontrol/unifi.py", line 96, in _execute
    resp = ses.send(ses.prepare_request(request))
  File "/usr/local/lib/python3.7/dist-packages/requests/sessions.py", line 655, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/requests/adapters.py", line 514, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='192.168.1.249', port=443): Max retries exceeded with url: /api/s/default/stat/sysinfo (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1056)')))

Error log tells that SSL certificate is expired. Can you confirm in your browser?

Certificate is expired, but i was hoping to amend the Python so even if it has expired it doesnt flag up an error! Any help would be appreciated!