Monitoring your Unifi AP

Got a feeling there is something on your side that is causing the issue as you saying you have other errors related to python as well.

I set up a fresh installation in Hyper-V with plain HA + Hacs (incl. Unifi Gateway integration) + Terminal + File Editor. Nothing elseā€¦ same error:

Logger: homeassistant.components.command_line
Source: components/command_line/__init__.py:44
Integration: command_line (documentation, issues)
First occurred: 09:46:02 (4 occurrences)
Last logged: 09:52:02

Command failed: python3 /config/scripts/unifi_ap.py

This is the adapted script:

from pyunifi.controller import Controller
from datetime import timedelta
import json
import re

#### fill in your unifi controller credentials ####

host = '192.168.1.254'
username = 'xxx'
password = 'xxx'
version = 'UDMP-unifiOS'
#### version ####
## the base version of the controller API [v4|v5|unifiOS|UDMP-unifiOS] ##
## this would be for the version of the controller you are running ##
#################
site_id = 'default'
port = '443'
verify_ssl = False
target_mac = '74:ac:b9:46:36:37' ## the mac address of your AP device

# client = Controller(host, username, password, port, version, site_id=site_id, ssl_verify=verify_ssl)
client = Controller(host=host,username=username,password=password,site_id=site_id,port=port,ssl_verify=verify_ssl)
stat = client.get_sysinfo()
devs = client.get_device_stat(target_mac)
clients = client.get_clients()
numclients = devs['user-wlan-num_sta']
numguests = devs['guest-wlan-num_sta']
score = devs['satisfaction']
update = devs['upgradable']
cpu = devs['system-stats']['cpu']
ram = devs['system-stats']['mem']
activity = round(devs['uplink']['rx_bytes-r']/125000 + devs['uplink']['tx_bytes-r']/125000,1)
seconds = devs['uptime']
days = seconds // 86400
hours = (seconds - (days * 86400)) // 3600
minutes = (seconds - (days * 86400) - (hours * 3600)) // 60
uptime = str(days)+'d '+str(hours)+'h '+str(minutes)+'m'
wifi0clients = devs['radio_table_stats'][0]['user-num_sta']
wifi1clients = devs['radio_table_stats'][1]['user-num_sta']
wifi0score = devs['radio_table_stats'][0]['satisfaction']
wifi1score = devs['radio_table_stats'][1]['satisfaction']
final = json.dumps({"Clients":numclients,"Guests":numguests,"Clients_wifi0":wifi0clients ,"Clients_wifi1":wifi1clients ,"Score":score,"CPU":str(cpu),"RAM":str(ram),"Uptime":uptime,"Score_wifi0":wifi0score ,"Score_wifi1":wifi1score ,"Activity":str(activity)+' Mbps',"Update":update})

print (final)

You have and error in your script:

client=Controller(host=host,username=username,password=password,site_id=site_id,port=port,ssl_verify=verify_ssl)

Is incorrect and should be:

client = Controller(host, username, password, port, version, site_id=site_id, ssl_verify=verify_ssl)

Hi! Cool work :slight_smile:

The official Unifi integration seems to be adding quite a bit more data nowadays; has there been some effort to integrate these things into the upstream integration? Iā€™m very much interested in the power measurements on PoE myself.

I managed to try the script in terminal and get the following message:

āžœ  ~ python3 /config/scripts/unifi_ap.py
Traceback (most recent call last):
  File "/config/scripts/unifi_ap.py", line 1, in <module>
    from pyunifi.controller import Controller
ModuleNotFoundError: No module named 'pyunifi'

Do you have this integration installed on your home assistant ?

Can definitely try and add the power measurements on the sensors.

FYI:
I am still learning my way around things. As mentioned before this me just trying to getting things to work and taking bits and pieces all over. I started with python course, so maybe in few weeks I can clean this up and make better and easier integration into home assistant.

This is what I use to get the POE data from my switches (I use Appdaemon so will need adjusting):

        for x in range(len(devs['port_table'])):
            port_poe = devs['port_table'][x]['port_poe']
            if port_poe == True:
                port_name = devs['port_table'][x]['name']
                poe_power = round(float(devs['port_table'][x]['poe_power']), 1)
                poe_voltage = round(float(devs['port_table'][x]['poe_voltage']))
                self.log(str(key) + ' Port ' + str(x+1) + ' ' + str(port_name) + ': ' + str(poe_power) + 'W' + ' ' + str(poe_voltage) + 'V')
                self.set_state(entity + "_port" + str(x+1) + "_power", state = poe_power, attributes = {"friendly_name": port_name, "device_class": "power", "unit_of_measurement": "W", "model": model})
                self.set_state(entity + "_port" + str(x+1) + "_voltage", state = poe_voltage, attributes = {"friendly_name": port_name, "device_class": "voltage", "unit_of_measurement": "V", "model": model})
            else:
                self.log(str(key) + ' Port ' + str(x+1) + ": NOT POE")

Playing around with a similar idea of displaying port info as well.

Managed to get it to show if active or not, but the code is messyā€¦

here is what I have for now:

port1 = devs['port_table'][0]["up"]
port2 = devs['port_table'][1]['up']
port3 = devs['port_table'][2]['up']
port4 = devs['port_table'][3]['up']
port5 = devs['port_table'][4]['up']
port6 = devs['port_table'][5]['up']
1 Like

Yeah, I know that your script uses the integration.
I re-configured the integration and your script is working now. Thank you very much for sharing the script and your patience.

1 Like

@w1w0lf, that screenshot looks amazing :slight_smile: . I had a quick look at the Unifi integration code myself, but itā€™s quite a complex beast to get around after just a quick look. Usually in this kind of projects, the maintainers of the component are very happy to help you find your way, so I would suggest you get in touch with them if you want to do this!

@Holdestmade would you be willing to share you app daemon code ? I would love to use that rather then command line scriptws

Sure, its in post 57 above

1 Like

I thought I saw a link to the shell command defined int he original card, but now I cant find itā€¦

shell_command.reboot_ap_ac_lite

am I crazy or was that posted somewhere ?

nevermind, I found it here

Would it be possible to pull the actual network sent/recieved from the controller?
Have a usg3 and tried to use that MAC in pyunifi.

For those of you keen to run this in appdaemon, i took the script posted by @Holdestmade and made it more appdaemon friendly and configuralbe

I will add some more details specific to switches sometime soon.

1 Like

Hello,

Itā€™s a great job. Thank you for sharing it with us.
Iā€™ve just bought a Unifi Dream Router (OS: UDR v3.0.17).

I thought that your solution will also allow me to extra more info, but unfortunately I always got 404 error, when I try to connect via this script.

Iā€™ve checked the PHP library, theoretically it should work with UDR as well.

Any ideas what could be wrong?

Thank you in advance.
Zoli

@sh00t2kill,looking foreward to more details.
I have copied unify.py from your github to config/appdeamon/apps/unify.py
and pasted the setup
Config/appdeamon/apps/apps.yaml with my own credentials(controller is UDMP, so using port 443) and a couple of switches and apā€™s.
Donā€™t understa d what to do next:)

Assuming you have already restarted appdaemon and its not working, try the latest change.

You might be getting 401 errors as UDMP works slightly differenty than non UDMP controllers