UniFi Security Gateway

Very cool, would love to test this out. Just moved from pfSense to a USG and tying it into HA would be nice…

I can not actually help.Except for the fact that for me it works just installing from HACS and using a readonly user

platform: unifigateway
host: !secret unifi_host
username: !secret unifi_username
password: !secret unifi_password
monitored_conditions:
- www
- lan
- wan
- wlan
- alerts
- firmware

I use some template sensor to get specific attributes for the graphs in lovelace.

1 Like

Can you share the template sensors?
I have the Integration working but don’t have the charting working yet.

platform: template
sensors:
  unifi_gateway_wan_cpu:
    friendly_name: 'CPU'
    value_template: "{{ states.sensor.unifi_gateway_wan.attributes['gw_system-stats']['cpu'] }}"
platform: template
sensors:
  unifi_gateway_wan_mem:
    friendly_name: 'Memory'
    value_template: "{{ states.sensor.unifi_gateway_wan.attributes['gw_system-stats']['mem'] }}"
platform: template
sensors:
  unifi_gateway_wlan_num_guest:
    friendly_name: 'Guests'
    value_template: '{{ states.sensor.unifi_gateway_wlan.attributes.num_guest }}'
platform: template
sensors:
  unifi_gateway_www_xput_down:
    friendly_name: 'USG Speedtest Download'
    unit_of_measurement: Mbps
    value_template: "{{ states.sensor.unifi_gateway_www.attributes.xput_down }}"
platform: template
sensors:
  unifi_gateway_www_xput_up:
    friendly_name: 'USG Speedtest Upload'
    unit_of_measurement: Mbps
    value_template: "{{ states.sensor.unifi_gateway_www.attributes.xput_up }}"
platform: template
sensors:
  unifi_gateway_www_speedtest_ping:
    friendly_name: 'USG Speedtest Ping'
    unit_of_measurement: ms
    value_template: "{{ states.sensor.unifi_gateway_www.attributes.speedtest_ping }}"

I should rewrite them to use the functions states() and state_attr() but they work as they are.

1 Like

@kaosmagix Dude! Thank you very much!!

Thanks for the help Cristian.

I’ve done pretty much exactly the same as you but unfortunately no luck here as yet.

One question for you - do you have the secrets in quotes in your secrets.yaml file? Or without quotes?

And just to confirm - the host it should point to is the IP of the Unifi Controller? In my case i’m using a Debian VM with the controller installed there. That’s supported right?

It looks like it’s possible to run the Unifi Controller on the same Pi as Hassio and perhaps that is why i’m struggling.

In my case i’m running in a seperate VM which means the pyunifi libraries I think I need in Hassio are missing. Is anyone able to give me pointers on how install the missing libraries please, or at least verify that they are installed?

Secret username/password/host values are all defined without quotes. the host is IP only. I run Unifi Controller in a separate VM besides hassio (docker) as otherwise it clashes with the Mosquito add-on. However I used to use the add-on before (when I did not use Mosquito yet) and I basically only changed the IP-address. I did a complete rebuild of everything recently and could get it to work without any issue by just adding the Unify integration (not in yaml) and the HACS custom component.

Edit: I use hassio 0.99.3 (not the latest as that crashes the 2.0 iOS beta companion app each minute)and Unify Controller 5.10.25

Edit2: My config is similar my Controller is also running on another VM and machine as Hassio

Thanks for confirming. In that case my config should be ok.

To test I installed the Unifi Controller add-on in HA but didn’t configure it. I figured doing that should cause the required libraries to automatically be installed but having done so there are still 4 sensors which show UNKNOWN in the state view.

I’ve got the HACS custom component installed but I hadn’t enabled the integration.

I’ve done that now but unfortunately it’s not made a difference as yet.

The following metrics are being populated:

sensor.unifi_gateway_3_bytes_received
sensor.unifi_gateway_3_bytes_sent
sensor.unifi_gateway_3_kbyte_sec_received
sensor.unifi_gateway_3_kbyte_sec_sent
sensor.unifi_gateway_3_packets_received
sensor.unifi_gateway_3_packets_sec_received
sensor.unifi_gateway_3_packets_sec_sent
sensor.unifi_gateway_3_packets_sent
sensor.unifi_gateway_alerts
sensor.unifi_gateway_firmware_upgradable

But these all shown as ‘UNKNOWN’:

sensor.unifi_gateway_lan
sensor.unifi_gateway_vpn
sensor.unifi_gateway_wan
sensor.unifi_gateway_wlan
sensor.unifi_gateway_www

I’ve tried various things since my last post but all to no avail. No matter what I do those four sensors always return UNKNOWN.

If anyone is able to offer any suggestions then i’d really love to hear them.

@kaosmagix Are you running your devices in the default site? Or did you create a new one? I’m wondering if my devices being in a new site that I defined is the issue?

Scratch that. I got to the bottom of it.

I don’t know how but I missed that the site_id property could be specified. I had opened the python script and noticed the parameter section in there and whilst it’s not mentioned in the initial post on this thread it is mentioned in the repo.

I’ve added the site ID and now it works fine. Phew!!

Thanks a lot for creating this custom sensor.
I have (with help from @kaosmagix above) successfully got it displaying USG CPU and RAM, plus the most recent speedtest Upload/Download speeds as gauges (rounded to whole Mbps numbers using | int ).

I am having trouble trying to get the current WWW or WAN up/down throughput displayed using the tx_bytes-r and rx_bytes-r attributes.

When I try to create a sensor template using the rx_bytes-r variable, it always errors, and seems to be related to the -r on the end.

  - platform: template
    sensors:
      unifi_gateway_www_rx_bytes-r:
        friendly_name: 'Current Download'
        unit_of_measurement: bps
        value_template: "{{ states.sensor.unifi_gateway_www.attributes.rx_bytes-r }}"

Gives an error of:

Error rendering template: UndefinedError: ‘mappingproxy object’ has no attribute ‘rx_bytes’

  - platform: template
    sensors:
      unifi_gateway_www_rx_bytes_r:
        friendly_name: 'Current Download'
        unit_of_measurement: bps
        value_template: "{{ states.sensor.unifi_gateway_www.attributes.rx_bytes_r }}"

Does not give an error, but the result is “”

HI I had a similar issue but tried this in my template and it works for me

  - platform: template
    sensors:
      unifi_gateway_wan_current_download:
        friendly_name: 'Current Download'
        value_template: "{{ states.sensor.unifi_gateway_wan.attributes['rx_bytes-r'] }}"
        unit_of_measurement: "bps"

Thanks mate, that worked.

I am also testing displaying the sensor in Kbps and Mbps (by adding a 2nd /1024) to see which looks best for my usage.

  - platform: template
    sensors:
      unifi_gateway_wan_download:
        friendly_name: 'WAN Download'
        unit_of_measurement: Kbps
        value_template: "{{ (states.sensor.unifi_gateway_wan.attributes['rx_bytes-r'] / 1024) | int }}"

I also added a combined upload/download history line graph below the gauges:

            - type: history-graph
              hours_to_show: 3
              entities:
                - sensor.unifi_gateway_wan_download
                - sensor.unifi_gateway_wan_upload

Just checking something… the rx_bytes-r variable is Bytes per Second not Bits per Second ?

So by Div by 1024, I am displaying the current WAN KB/sec download speed…

I thought the base number was Bits per Second and was going to Div / 8 to get Bytes per Second, then /1024 to get KB/sec…

I went a completely different way with this… I am using the speed test values… this has been working well for me as the Rx and Tx seem cumulative and not the reflective values I was looking for. Additionally the numbers in the sensor for Rx and Tx do not make any sense nor do they correlate directly to the router at all. if you SSH to the gateway and run “show interfaces counters” you would see the Rx and Tx values…

  - platform: template
    sensors:
      unifi_gateway_www_xput_down:
        friendly_name: 'USG Speedtest Download'
        unit_of_measurement: Mbps
        value_template: "{{ states.sensor.unifi_gateway_www.attributes.xput_down }}"
        icon_template: mdi:speedometer

  - platform: template
    sensors:
      unifi_gateway_www_xput_up:
        friendly_name: 'USG Speedtest Upload'
        unit_of_measurement: Mbps
        value_template: "{{ states.sensor.unifi_gateway_www.attributes.xput_up }}"
        icon_template: mdi:speedometer

Does anyone know why since UniFi Controller v5.12.22 speed tests have been limited to 12 hourly?

It makes the sensors a lot less useful.

The variable says “bytes per second” so I’m taking that at face value as Bytes not Bits. I div by 1024 to get KB/Sec.

@cdrom1028 I too thought it was a cumulative value, but if you keep an eye on it, it seems to go up and down and actually be a realtime WAN value.

@klogg thats more a question for the UniFi forums. Im on v5.12.35 and can set speedtests to once every 24 hours, which is what I use.

Please someone can help me please,
i get following error:
Error while setting up platform unifigateway
Traceback (most recent call last):
File “/usr/local/lib/python3.7/site-packages/urllib3/connection.py”, line 157, in _new_conn
(self._dns_host, self.port), self.timeout, **extra_kw
File “/usr/local/lib/python3.7/site-packages/urllib3/util/connection.py”, line 61, in create_connection
for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
File “/usr/local/lib/python3.7/socket.py”, line 752, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -3] Try again

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py”, line 672, in urlopen
chunked=chunked,
File “/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py”, line 376, in _make_request
self._validate_conn(conn)
File “/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py”, line 994, in _validate_conn
conn.connect()
File “/usr/local/lib/python3.7/site-packages/urllib3/connection.py”, line 334, in connect
conn = self._new_conn()
File “/usr/local/lib/python3.7/site-packages/urllib3/connection.py”, line 169, in _new_conn
self, “Failed to establish a new connection: %s” % e
urllib3.exceptions.NewConnectionError: <urllib3.connection.VerifiedHTTPSConnection object at 0x72380690>: Failed to establish a new connection: [Errno -3] Try again

My config looks like
‘’’
sensor:

  • platform: unifigateway
    host: !secret unifi_host
    username: !secret unifi_username
    password: !secret unifi_password
    port: !secret unifi_port
    site_id: !secret unifi_site
    ‘’’

files are stored under custom_components>unifigateway>
then 2x py-files and and json file and an extra map pycache
whats wrong

Looks like an issue with the host ip/port not being correct, or possible the site_id.