Unifi sensor custom component

Hi @jimbob1001

Not a too easy task because the sensor shows what the unifi controller reports. But there are two solutions: 1) make your binary_sensor smarter and check if the network exist or not (I’m sure that this can be done using templates); 2) What do you think if the sensor has a configuration option where you list your “guest” or “fixed” network (maybe even list more than one)?

If (1) is enough search the forum because I guess someone asked this before and found a solution. If (2) is better I can do it but not before the end of march or I can accept a patch or PR :-).

Ah okay, in that case I think I will see about changing the template to tolerate the fault as I think it can be done as you suggest, just need to read the docs again!
Many thanks

EDIT: I think I have fixed the error - just need a guest to come over now to test it works!!

- platform: template
  sensors:
    guests:
      friendly_name: 'Guests'
      device_class: 'presence'
      value_template: >
        {{ states.sensor.unifi.attributes["GUEST"] is defined and 
        states.sensor.unifi.attributes["GUEST"] > 0 }}

I can’t seem to get this component to work. I get the following error when I try to validate my configuration in the settings under Configuration: Integration my_unifi not found when trying to verify its sensor platform.

Here’s my configuration in my sensor.yaml:

- platform: my_unifi
  name: UniFi Controller
  region: <site id from controller url>
  username: !secret unifi_username
  password: !secret unifi_password
  url: https://192.168.1.80:8443
  ssl_verify: false

I’ve copied the my_unifi folder to my custom_components folder and it seems it’s not recognizing the custom component at all. I’m not running my hass instance using an SSL certificate, but I’ve tired with and without the ssl_verify line above and neither way works.

I have the built in UniFi Component and Device Tracker component working without issue, so I know that Home Assistant can see my UniFi network. I’m stumped here. Would love to find away to get this working.

Hi,

I guess that there’s a missing file. Try this, create a empty _ init _.py file in my_unifi folder. I will update github asap.

That worked! Thank you!

great. I just updated the github and added this file.

Hello !
Thanks for your work.
I get this log, what should I do ?

‘’’
Logger: homeassistant.components.sensor
Source: custom_components/my_unifi/sensor.py:170
Integration: Senzor (documentation, issues)
First occurred: 10:09:23 (1 occurrences)
Last logged: 10:09:23

my_unifi: Error on device update!

Traceback (most recent call last): File “/usr/local/lib/python3.7/site-packages/requests/models.py”, line 380, in prepare_url scheme, auth, host, port, path, query, fragment = parse_url(url) File “/usr/local/lib/python3.7/site-packages/urllib3/util/url.py”, line 392, in parse_url return six.raise_from(LocationParseError(source_url), None) File “”, line 3, in raise_from urllib3.exceptions.LocationParseError: Failed to parse: https://192.168.88.175:8443 /api/login During handling of the above exception, another exception occurred: Traceback (most recent call last): File “/usr/src/homeassistant/homeassistant/helpers/entity_platform.py”, line 324, in _async_add_entity await entity.async_device_update(warning=False) File “/usr/src/homeassistant/homeassistant/helpers/entity.py”, line 476, in async_device_update await self.hass.async_add_executor_job(self.update) File “/usr/local/lib/python3.7/concurrent/futures/thread.py”, line 57, in run result = self.fn(*self.args, **self.kwargs) File “/config/custom_components/my_unifi/sensor.py”, line 107, in update self._data.update() File “/config/custom_components/my_unifi/sensor.py”, line 231, in update self.login() File “/config/custom_components/my_unifi/sensor.py”, line 170, in login self._current_status_code = self._session.post("{}/api/login".format(self._baseurl), data=json.dumps(self._login_data), verify=self._verify_ssl).status_code File “/usr/local/lib/python3.7/site-packages/requests/sessions.py”, line 578, in post return self.request(‘POST’, url, data=data, json=json, **kwargs) File “/usr/local/lib/python3.7/site-packages/requests/sessions.py”, line 516, in request prep = self.prepare_request(req) File “/usr/local/lib/python3.7/site-packages/requests/sessions.py”, line 459, in prepare_request hooks=merge_hooks(request.hooks, self.hooks), File “/usr/local/lib/python3.7/site-packages/requests/models.py”, line 314, in prepare self.prepare_url(url, params) File “/usr/local/lib/python3.7/site-packages/requests/models.py”, line 382, in prepare_url raise InvalidURL(*e.args) requests.exceptions.InvalidURL: Failed to parse: https://192.168.88.175:8443 /api/login
‘’’

I guess you forgot to put quotes around url value. Also check if there’s no spaces left on the value.

Hi there, I am running HA 0.115.2 and your custom component doesn’t update the connected SSID sensor anymore. Maybe it is not compatible with the latest version as HA evolves quite rapidly? I heven’t see any recent updates on github yet. Is there any chance you can look into it why the sensor does not get updated?

This custom component is actively used in my HA configuration to detect guest presence and so to disable certain automation. It has worked without any issues until I upgraded HA recently. Without it my guest detection would break. I hope you can find time to make it compatible with 0.115.2. Thank you so much for your time and effort in this.

Hi @janus,

Hm… i think i didnt get it. I’m running 0.115.3 and I can see the number of devices connected to my guest SSID changing. Or are you creating new SSIDs?

Hi @clyra thank you so much for your fast reaction. I found out the glitches are from the Unifi Controller software version 6.0.23. It somehow doesn’t update orphaned disconnected clients. Disconnected clients from my guest wifi for example would pop op sometime long after they have been disconnected. Or the controller doesn’t update the list of connected clients per SSID when new client joins the wifi network. Very strange because this never happened before.

Since you are also using UNIFI, may I ask what version are you running? And did you have similar issue before? Thank you!

Hi @janus,

My controller is running the 5.13.32 version. I heard that 6.0.x has nasty bug. Can’t remember exactly what but had something to do with loops, so i just hold the upgrade. Should be fixed by now, but as you already found, there’s more bugs :-(.

Hi @clyra, I managed to fix the “bug”. I think it is dued to the transition to version 6 that the mongodb has issues with large databases. So I grabbed my latest Unifi backup and copied it to my desktop. Then I completely purged Unifi from my pi and reinstalled it. When I browsed to the controller web interface, I restored the backup and now everything works as expected again.

I am sorry to bother you before doing my research. Hope you didn’t mind. I am still satisfied with your component and it works flawlessly!
Thank you!

no problem, you’re welcome. Glad to hear it still works :-).

1 Like

I have problem setting component…

I have 6.0.23 local UniFi controller.
Also HA is up-to-date…

Component (folder) was copied under correct folder… but after HA restart I do not get any sensor…

I double check login data, controller site name… so… what could be the problem?

Where exactly I do find SITE ID under UniFi Controller…

Look at the URL from your dashboard:

https://192.168.0.xx:8443/manage/site/default/dashboard

“default” in this case, yours may be different.

Great… Thank you for info… it’s working…
I was using site name, which is not default, but on “http” link is the same, so default…

Thank you so much for a really useful component for Home Assistant @clyra :smiley:

I recently upgraded my Cloud Key Gen2+ to the new firmware 2.0.26 (UniFi OS) and a lot has changed - Specifically it no longer uses port 8443 for the UniFi controller but instead uses port 443 with different path for each application (/network or /protect).

Would it be an easy fix for you to make this component compatible with the new setup? I came across a comment on Reddit where someone explained the differences with the new OS. In case there’s any issues with the link this is the important part:

I have found the API access URL. It’s similar to the previous controller URL, but prefixed with “/proxy/network/“. The port is 443.

For example https://udm_pro_ip:443/proxy/network/api/s/default/stat/device will get you a Json text response with all the device configuration details in it.

Note you do have to login first., so it’s easier to use a python script, with a requests session to login and then run whatever API you want.

To login you post your login credentials to “/proxy/network/API/login” as Json like this:

json_request = { ‘username’: self.username, ‘password’: self.password, ‘strict’: True }

hi @ZuluWhiskey,

As I dont use the cloud key/new firmware I cannot test it, but based on your description, a few easy changes should do the trick. The first one is just changing the “url” on sensor configuration, so it should look like “https://<device_ip>:443/proxy/network” (no trailing slash).

Also, if your description is correct, they changed the login a little bit, as the old url is “/api/login” and not “/API/login”, so you have to edit this on the “def login(self):” function.

May you try the first one, and if it didnt work, try the second one too?