Monitoring your Unifi AP

@sh00t2kill
I’m trying to get this to run, but fail. Hope you can help out a AppDeamon and python newbie :face_with_peeking_eye:
I’m running HomeAssistant and AppDaemon on Docker which is installed on a Diskstation.
What I did:

1.) Add pyunifi as a python module:
I created a requirements.txt in the apps folder and added one line with pyunifi.
Thats it, not sure if this does the trick, but couldn’t find that I have do do anything else like copying a pyunifi file somewhere…

2.) copied the unifi.py from github to the apps folder

3.) pasted the setup into the apps.yaml
Changed to my credentials. Gateway mac is my diskstation. Added an AP and a switch.

AppDaemon is showing the following errors now…

SyntaxError: invalid character '·' (U+00B7)
^
<title>unifi-appdaemon/unifi.py at main · sh00t2kill/unifi-appdaemon · GitHub</title>
File "/conf/apps/unifi.py", line 105
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "<frozen importlib._bootstrap_external>", line 947, in source_to_code
File "<frozen importlib._bootstrap_external>", line 1017, in get_code
File "<frozen importlib._bootstrap_external>", line 879, in exec_module
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
return _bootstrap._gcd_import(name[level:], package, level)
File "/usr/local/lib/python3.10/importlib/__init__.py", line 126, in import_module
self.modules[module_name] = importlib.import_module(module_name)
File "/usr/local/lib/python3.10/site-packages/appdaemon/app_management.py", line 783, in read_app
self.read_app(file)
File "/usr/local/lib/python3.10/site-packages/appdaemon/app_management.py", line 768, in read_app
result = self.fn(*self.args, **self.kwargs)
File "/usr/local/lib/python3.10/concurrent/futures/thread.py", line 58, in run
response = future.result()
File "/usr/local/lib/python3.10/site-packages/appdaemon/utils.py", line 304, in run_in_executor
await utils.run_in_executor(self, self.read_app, mod["name"], mod["reload"])
File "/usr/local/lib/python3.10/site-packages/appdaemon/app_management.py", line 1000, in check_app_updates
Traceback (most recent call last):
During handling of the above exception, another exception occurred:
KeyError: 'unifi'
importlib.reload(self.modules[module_name])
File "/usr/local/lib/python3.10/site-packages/appdaemon/app_management.py", line 763, in read_app
2024-04-02 20:34:25.250155 WARNING Error: Traceback (most recent call last):
2024-04-02 20:34:25.180327 WARNING Error: ------------------------------------------------------------
2024-04-02 20:34:25.179918 WARNING Error: Unexpected error loading module: /conf/apps/unifi.py:
2024-04-02 20:34:25.179421 WARNING Error: ------------------------------------------------------------
2024-04-02 20:30:19.158130 WARNING unifi_monitor: ------------------------------------------------------------

Dont’t kow what I did wrong… hope sb can help

You need to add the required python library too appdaemon config. The addon configuration has python packages, simply add pyunifi to it.

If you dont have anything else in appdaemon, you can edit it in yaml and replace whats there with this:

system_packages: []
python_packages:
  - pyunifi
init_commands: []

In which yaml am I supposed to put this?
I did add it to the requirements.txt as described here:
https://appdaemon.readthedocs.io/en/latest/DOCKER_TUTORIAL.html#python-packages

I do have a docker installation of apdaemon and home assistant. So no addons in home assistant.

EDIT:
Ist running now but giving me errors again. But at least sth :slight_smile:

-removed error code-

Update:
Another step forward. The name of my switch flex mini hat blanks, which caused some of the errors.

main log looking like this now:

2024-04-03 19:39:17.441917 INFO unifi_monitor: Update Switches Started
2024-04-03 19:39:17.460458 INFO unifi_monitor: Switch Model: USMINI : 172.172.172.21
2024-04-03 19:39:17.468765 INFO unifi_monitor: SwitchFlexMini Port 1 Port 1 is on
2024-04-03 19:39:17.481253 INFO unifi_monitor: SwitchFlexMini Port 2 Port 2 is on
2024-04-03 19:39:17.492652 INFO unifi_monitor: SwitchFlexMini Port 3 Port 3 is on
2024-04-03 19:39:17.504548 INFO unifi_monitor: SwitchFlexMini Port 4 Port 4 is on
2024-04-03 19:39:17.516430 INFO unifi_monitor: SwitchFlexMini Port 5 Port 5 is on
2024-04-03 19:39:17.536772 INFO unifi_monitor: Update Health Started
2024-04-03 19:39:17.545368 WARNING AppDaemon: Logged an error to /conf/logs/error.log
2024-04-03 19:39:17.558758 INFO unifi_monitor: Updating WAN stats
2024-04-03 19:39:17.898569 WARNING AppDaemon: Logged an error to /conf/logs/error.log
2024-04-03 19:40:17.424946 INFO unifi_monitor: Update APs Started
2024-04-03 19:40:17.428500 WARNING AppDaemon: Logged an error to /conf/logs/error.log

But I do still get some errors:

2024-04-03 19:38:17.427087 WARNING unifi_monitor: ------------------------------------------------------------
2024-04-03 19:38:17.429412 WARNING unifi_monitor: Unexpected error in worker for App unifi_monitor:
2024-04-03 19:38:17.430499 WARNING unifi_monitor: Worker Ags: {'id': 'd86a23252b894c0ebf2d5858955664d9', 'name': 'unifi_monitor', 'objectid': '0f0d142cb5cb4c12944255c9a8d0b93e', 'type': 'scheduler', 'function': <bound method UnifiAPSW.update_aps of <unifi.UnifiAPSW object at 0x7fbe26e5f5e0>>, 'pin_app': True, 'pin_thread': 1, 'kwargs': {'__thread_id': 'thread-1'}}
2024-04-03 19:38:17.431121 WARNING unifi_monitor: ------------------------------------------------------------
2024-04-03 19:38:17.433450 WARNING unifi_monitor: Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/appdaemon/threading.py", line 1022, in worker
    funcref(self.AD.sched.sanitize_timer_kwargs(app, args["kwargs"]))
  File "/conf/apps/unifi.py", line 77, in update_aps
    for ap in self.args['aps']:
TypeError: 'NoneType' object is not iterable

2024-04-03 19:38:17.434134 WARNING unifi_monitor: ------------------------------------------------------------
2024-04-03 19:38:17.604157 WARNING unifi_monitor: ------------------------------------------------------------
2024-04-03 19:38:17.604698 WARNING unifi_monitor: Unexpected error in worker for App unifi_monitor:
2024-04-03 19:38:17.605190 WARNING unifi_monitor: Worker Ags: {'id': 'd8e5f202acfc4e45a6025be44f1e1c08', 'name': 'unifi_monitor', 'objectid': '0f0d142cb5cb4c12944255c9a8d0b93e', 'type': 'scheduler', 'function': <bound method UnifiAPSW.update_health of <unifi.UnifiAPSW object at 0x7fbe26e5f5e0>>, 'pin_app': True, 'pin_thread': 1, 'kwargs': {'__thread_id': 'thread-1'}}
2024-04-03 19:38:17.605622 WARNING unifi_monitor: ------------------------------------------------------------
2024-04-03 19:38:17.606201 WARNING unifi_monitor: Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/appdaemon/threading.py", line 1022, in worker
    funcref(self.AD.sched.sanitize_timer_kwargs(app, args["kwargs"]))
  File "/conf/apps/unifi.py", line 141, in update_health
    isp_upload = int(health[2]['xput_up'])
KeyError: 'xput_up'

2024-04-03 19:38:17.606621 WARNING unifi_monitor: ------------------------------------------------------------
2024-04-03 19:38:17.957155 WARNING unifi_monitor: ------------------------------------------------------------
2024-04-03 19:38:17.957672 WARNING unifi_monitor: Unexpected error in worker for App unifi_monitor:
2024-04-03 19:38:17.958120 WARNING unifi_monitor: Worker Ags: {'id': '0f2b97f7be90432aa2331fe70aca4448', 'name': 'unifi_monitor', 'objectid': '0f0d142cb5cb4c12944255c9a8d0b93e', 'type': 'scheduler', 'function': <bound method UnifiAPSW.update_wan of <unifi.UnifiAPSW object at 0x7fbe26e5f5e0>>, 'pin_app': True, 'pin_thread': 1, 'kwargs': {'__thread_id': 'thread-1'}}
2024-04-03 19:38:17.958553 WARNING unifi_monitor: ------------------------------------------------------------
2024-04-03 19:38:17.960086 WARNING unifi_monitor: Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/pyunifi/controller.py", line 31, in wrapper
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/pyunifi/controller.py", line 139, in _read
    return self._jsondec(response.text)
  File "/usr/local/lib/python3.10/site-packages/pyunifi/controller.py", line 120, in _jsondec
    raise APIError(obj["meta"]["msg"])
pyunifi.controller.APIError: api.err.UnknownDevice

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/pyunifi/controller.py", line 36, in wrapper
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/pyunifi/controller.py", line 139, in _read
    return self._jsondec(response.text)
  File "/usr/local/lib/python3.10/site-packages/pyunifi/controller.py", line 120, in _jsondec
    raise APIError(obj["meta"]["msg"])
pyunifi.controller.APIError: api.err.UnknownDevice

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/appdaemon/threading.py", line 1022, in worker
    funcref(self.AD.sched.sanitize_timer_kwargs(app, args["kwargs"]))
  File "/conf/apps/unifi.py", line 52, in update_wan
    gw = self.client.get_device_stat(self.args["gateway_mac"])
  File "/usr/local/lib/python3.10/site-packages/pyunifi/controller.py", line 326, in get_device_stat
    return self._api_read("stat/device/" + target_mac, params)[0]
  File "/usr/local/lib/python3.10/site-packages/pyunifi/controller.py", line 142, in _api_read
    return self._read(self._api_url() + url, params)
  File "/usr/local/lib/python3.10/site-packages/pyunifi/controller.py", line 38, in wrapper
    raise APIError(err)
pyunifi.controller.APIError: api.err.UnknownDevice

Apologies, i totally missed that you are running appdaemon in a container. My bad.
Anyway, looks like you got the config working.

Re the errors, this is happening because you dont have a “gateway” as such. I only just noticed you mentioned you set the diskstation is the gateway.
In this context, gateway refers to a unifi router, such as a USG or UDMP. It pulls heath/WAN stats from it.
I have made a small change so that it doesnt try to run those functions if the gateway_mac isnt set. Remove that config entry and update the script.
At the same time I made a change so if either the switches or ap config isnt set, it doesnt try and run those either.

It also looks as though potentially the AP config isnt right. It should be in the same format as switches, only with the key of aps instead of switches. Can you please share the app config if that is not the case?

Great, thanks a lot! :muscle: Just tested :slight_smile:

I removed the gateway and added my AP.
I did remove the AP before cause I thought it caues trouble.

Now everything seems to run smoothly. :partying_face: :star_struck:
Still need to check in Home Assistant though, but thats then the next step…

Main Log look like this:

2024-04-04 22:30:48.604134 INFO AppDaemon: Reloading Module: /conf/apps/unifi.py
2024-04-04 22:30:48.609942 INFO AppDaemon: Loading app unifi_monitor using class UnifiAPSW from module unifi
2024-04-04 22:30:48.613914 INFO AppDaemon: Calling initialize() for unifi_monitor
2024-04-04 22:30:49.107072 INFO unifi_monitor: Unifi AP and Switches Started
2024-04-04 22:30:49.108421 INFO unifi_monitor: Logging in to Unifi Controller
2024-04-04 22:30:49.127531 INFO unifi_monitor: Update APs Started
2024-04-04 22:30:49.139080 INFO unifi_monitor: No gateway configured -- skipping
2024-04-04 22:30:49.221606 INFO unifi_monitor: sensor.unifi_U7Pro_ap
2024-04-04 22:30:49.240924 INFO unifi_monitor: Update Switches Started
2024-04-04 22:30:49.249500 INFO unifi_monitor: Switch Model: USMINI : 172.172.172.21
2024-04-04 22:30:49.256817 INFO unifi_monitor: SwitchFlexMini Port 1 Port 1 is on
2024-04-04 22:30:49.267959 INFO unifi_monitor: SwitchFlexMini Port 2 Port 2 is on
2024-04-04 22:30:49.279217 INFO unifi_monitor: SwitchFlexMini Port 3 Port 3 is on
2024-04-04 22:30:49.290400 INFO unifi_monitor: SwitchFlexMini Port 4 Port 4 is on
2024-04-04 22:30:49.301634 INFO unifi_monitor: SwitchFlexMini Port 5 Port 5 is on
2024-04-04 22:31:49.121974 INFO unifi_monitor: Update APs Started
2024-04-04 22:31:49.211407 INFO unifi_monitor: sensor.unifi_U7Pro_ap
2024-04-04 22:31:49.232643 INFO unifi_monitor: Update Switches Started
2024-04-04 22:31:49.240860 INFO unifi_monitor: Switch Model: USMINI : 172.172.172.21
2024-04-04 22:31:49.249585 INFO unifi_monitor: SwitchFlexMini Port 1 Port 1 is on
2024-04-04 22:31:49.260951 INFO unifi_monitor: SwitchFlexMini Port 2 Port 2 is on
2024-04-04 22:31:49.272145 INFO unifi_monitor: SwitchFlexMini Port 3 Port 3 is on
2024-04-04 22:31:49.283384 INFO unifi_monitor: SwitchFlexMini Port 4 Port 4 is on
2024-04-04 22:31:49.294504 INFO unifi_monitor: SwitchFlexMini Port 5 Port 5 is on

No errors.

I still get those namespace warnings when I start the app the first time, but I guess thats as it should be.

2024-04-04 22:18:59.014586 WARNING unifi_monitor: unifi_monitor: Entity sensor.unifi_U7Pro_ap_ip not found in namespace default
2024-04-04 22:18:59.016155 INFO AppDaemon: unifi_monitor: Entity sensor.unifi_U7Pro_ap_ip created in namespace: default
2024-04-04 22:18:59.035946 WARNING unifi_monitor: unifi_monitor: Entity sensor.unifi_U7Pro_ap_clients not found in namespace default
2024-04-04 22:18:59.038833 INFO AppDaemon: unifi_monitor: Entity sensor.unifi_U7Pro_ap_clients created in namespace: default
2024-04-04 22:18:59.048384 WARNING unifi_monitor: unifi_monitor: Entity sensor.unifi_U7Pro_ap_guests not found in namespace default
2024-04-04 22:18:59.049635 INFO AppDaemon: unifi_monitor: Entity sensor.unifi_U7Pro_ap_guests created in namespace: default

Yep!
All looks like its working as its meant to.

Can anyone point me in the right direction i have everything working, but for whatever reason the Activity Mbps is not getting reported into the sensor. I see the values in the attributes but not in the sensor. Same thing happens with the AP Score.


Hello!
Is this post alive?

Im trying to configure this
GitHub - w1tw0lf/Unifi-Device-info: Unifi AP Device info via API for home assistant

in my home assistant OS.
and its imposible.
My unifi controller is the home assistant addon, and the unifigateway works.

But the py script not return information i have this 2 errors and i dont know how to solve

Command failed (with return code 1): python /config/scripts/unifi_ap_buhardilla.py
Empty reply found when expecting JSON data

any idea?

EDIT:
for home assistant OS and unifi addon. the version is V5
solved!