Monitoring your Unifi AP

Hi @revin. I’ve updated the code in the first post.

Try replacing

numclients = health[0]['num_user']

with

numclients = len(clients)

And see if it is solved.

@thundergreen Sure it is, but I am not that experienced in python to build it myself.

@juan11perez After having read the docs of the module’s developer, I do not think you can read 3 APs with the same script. So it is better to make 3 different scripts.

Thank you for your help

will this automaticall download the library? u use this library if i well understand from unificontrol import UnifiClient

any helo here? Getting this error:

bash-5.0# python3 unifi.py 
Traceback (most recent call last):
  File "unifi.py", line 18, in <module>
    stat = client.stat_sysinfo()
  File "/usr/local/lib/python3.8/site-packages/unificontrol/metaprogram.py", line 125, in wrapper
    return instance(client, *a, **kw)
  File "/usr/local/lib/python3.8/site-packages/unificontrol/metaprogram.py", line 103, in __call__
    return client._execute(url, self._method, rest_dict, need_login=self._need_login)
  File "/usr/local/lib/python3.8/site-packages/unificontrol/unifi.py", line 110, in _execute
    response = resp.json()
  File "/usr/local/lib/python3.8/site-packages/requests/models.py", line 900, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/simplejson/__init__.py", line 525, in loads
    return _default_decoder.decode(s)
  File "/usr/local/lib/python3.8/site-packages/simplejson/decoder.py", line 370, in decode
    obj, end = self.raw_decode(s)
  File "/usr/local/lib/python3.8/site-packages/simplejson/decoder.py", line 400, in raw_decode
    return self.scan_once(s, idx=_w(s, idx).end())
simplejson.errors.JSONDecodeError: Expecting value: line 2 column 1 (char 1)

my config:

host = '192.168.1.1'
username = 'hassio'
password = 'HassIo123'
site = 'default'  
port = '443'
mac = 'E0:63:DA:CE:08:C1' ## the mac address of your AP device

Hi @thundergreen ,

I downloaded manually the module, copied to the same script directory and added this lines to the script:

import sys
sys.path.append('/config/scripts/modules/')
1 Like

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