The default Unifi AP integration is great for the presence detection part of your network.
I wanted to have a Unifi dashboard, with more info such as CPU, RAM, number of connected clients etc.
I recently found out this great python module with which you can pretty much access any info on your AP.
I wrote a python script that pulls some information using the above mentioned module:
from unificontrol import UnifiClient
from datetime import timedelta
import json
#### fill in your unifi controller credentials ####
host = ''
username = ''
password = ''
site = ''
port = ''
mac = '' ## the mac address of your AP device
#### endpoints ####
client = UnifiClient(host=host,username=username,password=password,site=site,port=port)
stat = client.stat_sysinfo()
devs = client.list_devices(mac)
clients = client.list_clients()
guests = client.list_guests()
### supposed that you use both 2.4 and 5 ghz frequencies there should be wifi0 and wifi1, else edit accordingly
numclients = len(clients)
numguests = len(guests)
wifi0clients = []
wifi1clients = []
score = int(devs[0]['satisfaction'])
update = str(stat[0]['update_available'])
cpu = float(devs[0]['system-stats']['cpu'])
ram = float(devs[0]['system-stats']['mem'])
wifi0score = int(devs[0]['radio_table_stats'][0]['satisfaction'])
wifi1score = int(devs[0]['radio_table_stats'][1]['satisfaction'])
activity = float(round(devs[0]['uplink']['rx_bytes-r']/125000 + devs[0]['uplink']['tx_bytes-r']/125000,1))
seconds = int(devs[0]['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'
for i in [i for i,x in enumerate(clients) if (mac in str(x) and 'wifi0' in str(x))]:
try:
wifi0clients.append(clients[i]['name'])
except:
wifi0clients.append(clients[i]['mac'])
for i in [i for i,x in enumerate(clients) if (mac in str(x) and 'wifi1' in str(x))]:
try:
wifi1clients.append(clients[i]['name'])
except:
wifi1clients.append(clients[i]['mac'])
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)
Afterwards, add a new command_line sensor on your configuration.yaml with the following:
- platform: command_line
command: 'C:\Users\Administrator\AppData\Roaming\.homeassistant\python\unifi.py'
name: unifi_ap
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
Remember to edit the command
part to point it to the location where you saved the script.
Finally, you can make various template sensors to extract info such as CPU, RAM etc
For example,
- 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’s the final product
Of course the unificontrol module has many more possible sensors you can extract, so be sure to check the docs and big thanks to the developer.