Unifi sensor custom component

Hi all,

I just wrote a custom component for monitoring my unifi controller. The state of the sensor shows the number of devices connected. It also shows the number of devices per essid and per AP as attributes. Like this:

unifi_sensor

This is my first attempt to write a custom component and I’m sure there’s a lot of room to improve. You can grab it at my github.

9 Likes

Nice job @clyra looks interesting, and an easy way to keep tabs of how many clients are connected without having to worry about accessing the unifi webpage. I will have a play around with it when I get a chance :+1:

Hi @clyra

Thanks for sharing.

Tried to deploy but I get an error:

2018-09-10 20:06:06 ERROR (MainThread) [homeassistant.components.sensor] unifi: Error on device update!
Traceback (most recent call last):
File “/usr/src/app/homeassistant/helpers/entity_platform.py”, line 251, in _async_add_entity
await entity.async_device_update(warning=False)
File “/usr/src/app/homeassistant/helpers/entity.py”, line 353, in async_device_update
yield from self.hass.async_add_job(self.update)
File “/usr/local/lib/python3.6/concurrent/futures/thread.py”, line 56, in run
result = self.fn(*self.args, **self.kwargs)
File “/config/custom_components/sensor/unifi.py”, line 107, in update
self._data.update()
File “/config/custom_components/sensor/unifi.py”, line 250, in update
self.update_ap_list(ap)
File “/config/custom_components/sensor/unifi.py”, line 224, in update_ap_list
self.ap_list[newmac] = "AP" + device_info[0][‘name’]
KeyError: ‘name’

My config is:

I’m on 0.77.3

Thanks!!

Hi @revin,

I thought that this could happen… did you named your access points? Or they are using the default mac address as the name?

Thanks for quick replying

I only have one and is using the mac address.

image

Do I have to rename?

Thanks!

Hi,

If you rename it should work, or just wait until tomorrow and I will fix my code :slight_smile: .

I’ll wait until you release a new version. Then we’ll confirm that it works to avoid future issues.

thanks!

Hi,

Just sent a new version for github. May you try it, please?

It works!

image

would be possible to obtain another values that are available from the unifi AP?

image

Thanks!!

Hi,

hm… I think it is possible. What you want?

Hi all,

The custom entity-attribute-card is a good match to the unifi sensor. You may use it by adding something like this to ui-lovelace.yaml:

- type: entities
       title: Unifi
       entities:
        - sensor.unifi
        - type: custom:entity-attributes-card
          entity: sensor.unifi
          filter:
            include:
             - key: sensor.unifi.*

of course, you need to install it…

1 Like

Hi,
I’m pretty new to Home Assistant, and this is my first attempt at using a custom component, so I might be missing something obvious. I have it placed under custom_components/sensor and it’s found but it errors out on updating. I thought maybe it was ssl and tried adding “verify_ssl: false” to my configuration to no avail. I also tried it using the username & password in my secrets file and also directly in my config. Here is what is logged:

2018-09-12 16:24:41 ERROR (MainThread) [homeassistant.components.sensor] unifi: Error on device update!
File “/home/denny/.homeassistant/custom_components/sensor/unifi.py”, line 107, in update
File “/home/denny/.homeassistant/custom_components/sensor/unifi.py”, line 241, in update

And here is my config:

- platform: unifi
  username: Hass
  password: HassPass
  url: https://192.168.0.7:8443
  verify_ssl: false

I’m also using the Unifi device tracker, would that make a difference?

Thanks,

Denny

Hi @dennyreiter, the setup you’re describing is working fine for me; I also have the Unifi device tracker component running:

- platform: unifi
  name: Unifi AP Details
  username: !secret unifi_user
  password: !secret unifi_pass
  url: https://192.168.7.16:8443

Hi @dennyreiter,

Did you put the config under the right yaml? I mean, under “sensor:”? I assume you can correctly log on https://192.168.0.7:8443 using your browser with the provided password and access the unifi controller interface, it that right?

Hi @clyra,

great hint there with the the entity-attributes-card :+1:

I got it working the way you describe it, but I cannot get rid of the last wo entries:
image

I’ve tried this as well as including only specific entries:

      - type: custom:entity-attributes-card
        title: Unifi Status
        heading_name: Name
        heading_state: Value
        filter:
          include:
            - key: sensor.unifi.*
          exclude:
            - sensor.unifi.unit_of_measurement
            - sensor.unifi.friendly_name

Any hints?

1 Like

Yes. I have a separate yaml file for sensors, but it’s in with the rest of them.

I found a little more info by not using grep:

2018-09-12 19:25:36 WARNING (MainThread) [homeassistant.loader] You are using a custom component for sensor.unifi which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you do experience issues with Home Assistant.
2018-09-12 19:25:38 ERROR (MainThread) [homeassistant.components.sensor] unifi: Error on device update!
Traceback (most recent call last):
File “/home/denny/homeassistant/lib/python3.6/site-packages/homeassistant/helpers/entity_platform.py”, line 251, in _async_add_entity
await entity.async_device_update(warning=False)
File “/home/denny/homeassistant/lib/python3.6/site-packages/homeassistant/helpers/entity.py”, line 353, in async_device_update
yield from self.hass.async_add_job(self.update)
File “/usr/lib/python3.6/concurrent/futures/thread.py”, line 56, in run
result = self.fn(*self.args, **self.kwargs)
File “/home/denny/.homeassistant/custom_components/sensor/unifi.py”, line 107, in update
self._data.update()
File “/home/denny/.homeassistant/custom_components/sensor/unifi.py”, line 241, in update
if device[‘essid’] in devices_per_essid.keys():
KeyError: ‘essid’

Hi,

Entity-attributes-card accepts either include or exclude but not both. Try this:

 filter:
   include:
     - sensor.unifi.ap*
     - sensor.unifi.739*
     - sensor.unifi.gerstmeyers

Thanks for the hint - got it working now, but it doesn’t display the attribute unless I add a * at the end of the string :confused:

Hi @dennyreiter,

It seems that there are few (i hope) special cases that I was not expecting, like having a device which is not associated to any essid/network. May you check in your “clients” page on unifi interface if this is the case? I should put a few tests in the code to prevent that from happening.