Monitoring your Unifi 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!

i need help

device_tracker:
  - platform: unifi_direct
    host: YOUR_AP_IP_ADDRESS
    username: YOUR_USERNAME
    password: YOUR_PASSWORD

what to put under username and password, dont know
and i have 2 unifi AP
help

You’re referring to the unifi integration.
Here we are talking about a custom python script. Have a look at the link.

hello…I cant figure it out what issue I have with this script…I’m running supervised install on Debian 10…the script itself runs fine if i execute is from command line…its working for root and for my user…so no issue there…

im calling the script this way in my configuration.yaml

sensor:

- platform: command_line
  name: unifi_ap
  command: 'python3 /config/python_scripts/unify.py'
.
.
.

The file path itself is correct I only created python scrips directory under my config directory. But for some reason the script doesn’t want to be executed.

Here is the Error output:

'Logger: homeassistant.components.command_line
Source: components/command_line/init.py:41
Integration: command_line (documentation, issues)
First occurred: 12:13:17 PM (1 occurrences)
Last logged: 12:13:17 PM

Command failed: python3 /config/python_scripts/unify.py’

If someone is using this on linux install or hassos…and give me some pointer where to look for problem I appreciate it…

Update:

script gets executed…since I see login from HA on my Unifi controller…so that part is ok…also aditional info on the error:

2021-04-17 12:57:50 WARNING (SyncWorker_4) [homeassistant.components.command_line.sensor] Empty reply found when expecting JSON data

so it looks that my config is wrong to parse the data :frowning:

here it is:

sensor:

- platform: command_line
  name: unifi_ap
  command: 'python3 /config/python_scripts/unify.py'
  value_template: '{{ value_json.Clients }}'
  unit_of_measurement: Clients
  scan_interval: 60
  json_attributes:
      - Clients
      - Clients_Dimi3_WLAN
      - Score
      - CPU
      - RAM
      - Uptime
      - Score_Dimi3_WLAN
      - Activity
      - Update
      
- platform: template
  sensors:  
    unifi_ap_activity:
        value_template: >
            {{ states.sensor.unifi_ap.attributes.Activity }}
        unit_of_measurement: 'Mbps'
        friendly_name_template: Unifi AP Activity      
    unifi_ap_ram:
        value_template: >
            {{ states.sensor.unifi_ap.attributes.RAM }}
        unit_of_measurement: '%'
        friendly_name_template: Unifi AP RAM    
    unifi_ap_cpu:
        value_template: >
            {{ states.sensor.unifi_ap.attributes.CPU }}
        unit_of_measurement: '%'
        friendly_name_template: Unifi AP CPU

and here is the optupt of the script in command line:

{"Clients": 9, "Clients_Dimi3_WLAN": 9, "Score": 93, "CPU": "1.6", "RAM": "41.9", "Uptime": "9d 0h 27m", "Score_Dimi3_WLAN": 92, "Activity": "0.0 Mbps", "Update": false}

Maybe the script takes longer to print the data, so the command line sensor does not wait that long.

Try adding a command_timeout parameter in your sensor config and see if it works.

- platform: command_line
  name: unifi_ap
  command: 'python3 /config/python_scripts/unify.py'
  command_timeout: 60
  etc .......

Edit:

Also, try to make everything a string in the final json.

For example,

final = json.dumps({"Clients":str(numclients),"Guests":str(numguests)... etc

ok…here is my complete config now:

final = json.dumps({"Clients":str(numclients),"Clients_Dimi3_WLAN":str(wifi0clients) ,"Score":str(score),"CPU":str(cpu),"RAM":str(ram),"Uptime":uptime,"Score_Dimi3_WLAN":str(wifi0score) ,\
                "Activity":str(activity)+' Mbps',"Update":str(update)})

and sensor config:

- platform: command_line
  name: unifi_ap
  command: 'python /config/scripts/unifi.py'
  command_timeout: 60
  value_template: '{{ value_json.Clients }}'
  unit_of_measurement: Clients
  scan_interval: 120
  json_attributes:
      - Clients_Dimi3_WLAN
      - Score  
      - CPU
      - RAM
      - Uptime
      - Score_Dimi3_WLAN
      - Activity
      - Update
- platform: template
  sensors:  
    unifi_ap_activity:
        value_template: >
            {{ states.sensor.unifi_ap.attributes.Activity }}
        unit_of_measurement: 'Mbps'
        friendly_name_template: Unifi AP Activity      
    unifi_ap_ram:
        value_template: >
            {{ states.sensor.unifi_ap.attributes.RAM }}
        unit_of_measurement: '%'
        friendly_name_template: Unifi AP RAM    
    unifi_ap_cpu:
        value_template: >
            {{ states.sensor.unifi_ap.attributes.CPU }}
        unit_of_measurement: '%'
        friendly_name_template: Unifi AP CPU

command itself in shell takes 2 sec to come up with the values…after the changes…still no luck bringing up sensors…

HA errors;

2021-04-17 16:21:13 ERROR (SyncWorker_1) [homeassistant.components.command_line] Command failed: python /config/scripts/unifi.py

2021-04-17 16:21:13 WARNING (SyncWorker_1) [homeassistant.components.command_line.sensor] Empty reply found when expecting JSON data

2021-04-17 16:21:16 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'mappingproxy object' has no attribute 'Activity' when rendering '{{ states.sensor.unifi_ap.attributes.Activity }}'

2021-04-17 16:21:16 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'mappingproxy object' has no attribute 'Activity' when rendering '{{ states.sensor.unifi_ap.attributes.Activity }}'

2021-04-17 16:21:16 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'mappingproxy object' has no attribute 'RAM' when rendering '{{ states.sensor.unifi_ap.attributes.RAM }}'

2021-04-17 16:21:16 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'mappingproxy object' has no attribute 'RAM' when rendering '{{ states.sensor.unifi_ap.attributes.RAM }}'

2021-04-17 16:21:16 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'mappingproxy object' has no attribute 'CPU' when rendering '{{ states.sensor.unifi_ap.attributes.CPU }}'

2021-04-17 16:21:16 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'mappingproxy object' has no attribute 'CPU' when rendering '{{ states.sensor.unifi_ap.attributes.CPU }}'

2021-04-17 16:23:13 ERROR (SyncWorker_4) [homeassistant.components.command_line] Command failed: python /config/scripts/unifi.py

2021-04-17 16:23:13 WARNING (SyncWorker_4) [homeassistant.components.command_line.sensor] Empty reply found when expecting JSON data

2021-04-17 16:25:13 ERROR (SyncWorker_5) [homeassistant.components.command_line] Command failed: python /config/scripts/unifi.py

2021-04-17 16:25:13 WARNING (SyncWorker_5) [homeassistant.components.command_line.sensor] Empty reply found when expecting JSON data

2021-04-17 16:27:13 ERROR (SyncWorker_7) [homeassistant.components.command_line] Command failed: python /config/scripts/unifi.py

2021-04-17 16:27:13 WARNING (SyncWorker_7) [homeassistant.components.command_line.sensor] Empty reply found when expecting JSON data

2021-04-17 16:29:13 ERROR (SyncWorker_2) [homeassistant.components.command_line] Command failed: python /config/scripts/unifi.py

2021-04-17 16:29:13 WARNING (SyncWorker_2) [homeassistant.components.command_line.sensor] Empty reply found when expecting JSON data

and script output from cli

{"Clients": "7", "Clients_Dimi3_WLAN": "7", "Score": "88", "CPU": "1.5", "RAM": "41.3", "Uptime": "9d 4h 3m", "Score_Dimi3_WLAN": "87", "Activity": "0.1 Mbps", "Update": "False"}