Bandwidth monitor from unifi controller

Is it possible to add bandwidth information from my controller on my hass.io frontend?

I tried to do it with snmp but I cannot figure it out exactly. Is snmp the only option or are there other ways to display bandwidth monitoring on the frontend?

1 Like

Does anyone know hot to do this?

I’m looking for the exact same thing also.

Trying to set some automations based on a client’s bandwidth i.e streaming etc

I got this working by enabling SNMP and adding these sensors. internet_in_mbps and internet_out_mbps you can put in lovelace

- platform: snmp
  name: "USG WAN in"
  host: 192.168.1.1
  baseoid: 1.3.6.1.2.1.31.1.1.1.6.2
  community: "public"
  version: "2c"
  scan_interval: 10

- platform: snmp
  name: "USG WAN out"
  host: 192.168.1.1
  baseoid: 1.3.6.1.2.1.31.1.1.1.10.2
  community: "public"
  version: "2c"
  scan_interval: 10

- platform: statistics
  name: "USG WAN in Stats"
  entity_id: sensor.usg_wan_in
  sampling_size: 4
  max_age:
    hours: 24

- platform: statistics
  name: "USG WAN out Stats"
  entity_id: sensor.usg_wan_out
  sampling_size: 4
  max_age:
    hours: 24

- platform: template
  sensors:
    internet_in_mbps:
      value_template: "{{ (state_attr('sensor.usg_wan_in_stats','change_rate')|float*8*(state_attr('sensor.usg_wan_in_stats', 'sampling_size')-1)/1000000)|round(2) }}"
      unit_of_measurement: "MBps"
      entity_id: sensor.usg_wan_in_stats
    internet_out_mbps:
      value_template: "{{ (state_attr('sensor.usg_wan_out_stats','change_rate')|float*8*(state_attr('sensor.usg_wan_out_stats', 'sampling_size')-1)/1000000)|round(2) }}"
      unit_of_measurement: "MBps"
      entity_id: sensor.usg_wan_out_stats

@ingvarg did you have to update this for newer versions of home assistant? i’m seeing some warnings/errors

2021-06-13 23:36:12 WARNING (MainThread) [homeassistant.components.template.sensor] The 'entity_id' option near /config/sensors.yaml:33 is deprecated, please remove it from your configuration
2021-06-13 23:36:12 WARNING (MainThread) [homeassistant.components.template.sensor] The 'entity_id' option near /config/sensors.yaml:37 is deprecated, please remove it from your configuration
2021-06-13 23:36:19 ERROR (MainThread) [homeassistant.components.snmp.sensor] Please check the details in the configuration file
2021-06-13 23:36:19 ERROR (MainThread) [homeassistant.components.snmp.sensor] Please check the details in the configuration file

Read the error message and refer to the documentation.

The error says that on line 33 and line 37 you are using entity_id, but that is deprecated (which means it’s not recommended, although isn’t a fatal “all stop” error yet).

I’d imagine lines 33 and 37 are the entity_id declarations in the two template sensors.

The template sensor documentation is here (set to jump to the legacy format that you’re using):

and you’ll see there’s no mention of entity_id, which used to be allowed but no longer is. The sensor will work out what it needs to watch from the contents of value_template, which refer to the stats sensor you need.

Remove those two lines, and everything should work without any warnings.

Hi Guys!

anyone still has this working?
i am unable to figure out why it is not working.
i am also seeing some error messages that it might not work from the next version

2021-12-28 14:23:21 WARNING (MainThread) [homeassistant.helpers.template] Template warning: 'float' got invalid input 'None' when rendering template '{{ (state_attr('sensor.usg_wan_in_stats','change_rate')|float*8*(state_attr('sensor.usg_wan_in_stats', 'sampling_size')-1)/1000000)|round(2) }}' but no default was specified. Currently 'float' will return '0', however this template will fail to render in Home Assistant core 2022.1

using the following config:

  - platform: snmp
    name: "USG WAN in"
    host: 192.168.100.1
    baseoid: 1.3.6.1.2.1.31.1.1.1.6.2
    community: "public"
    version: "2c"
    scan_interval: 10

  - platform: statistics
    name: "USG WAN in Stats"
    entity_id: sensor.usg_wan_in
    sampling_size: 4
    max_age:
      hours: 24

  - platform: template
    sensors:
      internet_in_mbps:
        value_template: "{{ (state_attr('sensor.usg_wan_in_stats','change_rate')|float*8*(state_attr('sensor.usg_wan_in_stats', 'sampling_size')-1)/1000000)|round(2) }}"
        unit_of_measurement: "MBps"

am i wrong to say that “change_rate” is empty and it will not work?

i have a Unifi USG-3P → Firmware 4.4.56

Here’s what I had to do to iron out the issues with the original yaml:

  - platform: statistics
    name: "USG WAN in Stats"
    entity_id: sensor.usg_wan_in
    state_characteristic: change_second
    max_age:
      seconds: 30
  - platform: statistics
    name: "USG WAN out Stats"
    entity_id: sensor.usg_wan_out
    state_characteristic: change_second
    max_age:
      seconds: 30
  - platform: template
    sensors:
      internet_in_mbps:
        value_template: >
          {{ states('sensor.usg_wan_in_stats')|float*8/1024/1024|round(2) }}
        unit_of_measurement: "Mbps"
      internet_out_mbps:
        value_template: >
          {{ states('sensor.usg_wan_out_stats')|float*8/1024/1024|round(2) }}
        unit_of_measurement: "Mbps"

Might not be best practice or the most elegant approach, but it works for me and is (fairly) accurate.

1 Like

This works, but throws this warning:
Template warning: 'float' got invalid input 'unavailable' when rendering template [redacted] but no default was specified.

Changing it to float(0) … seems to do the trick:
{{ states('sensor.usg_wan_in_stats')|float(0)*8/1024/1024|round(2) }}

2 Likes

Hi hi, a 2022 Update.
Since a fair few things have been updated the whole template now looks like the following

sensor:
  - platform: snmp
    name: "USG WAN in"
    host: 10.1.0.1
    baseoid: 1.3.6.1.2.1.31.1.1.1.6.2
    community: "public"
    version: "2c"
    scan_interval: 10

  - platform: snmp
    name: "USG WAN out"
    host: 10.1.0.1
    baseoid: 1.3.6.1.2.1.31.1.1.1.10.2
    community: "public"
    version: "2c"
    scan_interval: 10

  - platform: statistics
    name: "USG WAN in Stats"
    entity_id: sensor.usg_wan_in
    sampling_size: 4
    state_characteristic: change_second
    max_age:
      hours: 24

  - platform: statistics
    name: "USG WAN out Stats"
    entity_id: sensor.usg_wan_out
    sampling_size: 4
    state_characteristic: change_second
    max_age:
      hours: 24
template:
  - sensor:
      - name: "Internet Download"
        state: "{{ (states('sensor.usg_wan_in_stats') |float /100000) | round(2) }}"
        unit_of_measurement: "MBps"
      - name: "Internet Upload"
        state: "{{ (states('sensor.usg_wan_out_stats') |float /100000) | round(2) }}"
        unit_of_measurement: "MBps"

image
Works wonderfully

2 Likes

Hey, just put this code to work and it works great! Had to shift some stuff around to the sensor.yaml but other than that it’s a great find, thanks!

I’ve just added the latest code above, but am getting the wrong calculated MBps (comparing Unifi and Home assistant, plus the fact I know my home service can’t do the speeds shown in HA)


image

(Code is exactly the same as this post above, minus the IP address!)

Never mind, I managed to fix it by applying previous float calculations to get the MBps number accurate.
Here’s the full code for others…


#Unifi USG
sensor:
  - platform: snmp
    name: "USG WAN in"
    host: 192.168.1.1
    baseoid: 1.3.6.1.2.1.31.1.1.1.6.2
    community: "public"
    version: "2c"
    scan_interval: 10

  - platform: snmp
    name: "USG WAN out"
    host: 192.168.1.1
    baseoid: 1.3.6.1.2.1.31.1.1.1.10.2
    community: "public"
    version: "2c"
    scan_interval: 10

  - platform: statistics
    name: "USG WAN in Stats"
    entity_id: sensor.usg_wan_in
    sampling_size: 4
    state_characteristic: change_second
    max_age:
      hours: 24

  - platform: statistics
    name: "USG WAN out Stats"
    entity_id: sensor.usg_wan_out
    sampling_size: 4
    state_characteristic: change_second
    max_age:
      hours: 24

template:
  - sensor:
      - name: "Internet Download"
        state: "{{ states('sensor.usg_wan_in_stats')|float(0)*8/1024/1024|round(2) }}"
        unit_of_measurement: "MBps"
      - name: "Internet Upload"
        state: "{{ states('sensor.usg_wan_out_stats')|float(0)*8/1024/1024|round(2) }}"
        unit_of_measurement: "MBps"

I am trying to set up this monitor for my USG Pro (with Gen 1 Cloud Key controller). I’ve used the following code for my sensors but I’m only seeing 0.0 within HA.

Anyone with advice on what isn’t working. Within my controller software, SNMP v2 is enabled.

 - platform: snmp
  name: "USG WAN in"
  host: 10.1.1.1
  baseoid: 1.3.6.1.2.1.31.1.1.1.6.2
  community: "public"
  version: "2c"
  scan_interval: 10

- platform: snmp
  name: "USG WAN out"
  host: 10.1.1.1
  baseoid: 1.3.6.1.2.1.31.1.1.1.10.2
  community: "public"
  version: "2c"
  scan_interval: 10

did you enable SNMP on the device?

I also heard that USG Pro does not have SNMP installed by default. you may have to log into the router or switch and install it manually. I have the USG and the above code works for me (with SNMP enabled in the controller).

This link explains how to install SNMP in the UDM Pro

If you’re using different hardware there’s a good chance you’re using a different interface. on the UDM PRO, adaptor 2 is “dummy” which explains why you got 0. per the link in @javellin’s post, the UDM PRO uses ifindex 4 which means baseoids needs to be updated to 1.3.6.1.2.1.31.1.1.1.6.24 and 1.3.6.1.2.1.31.1.1.1.10.24 respectively

I tried this code, and it works fine, but it seems to be disregarding the round(2) part. Any changes in recent HA versions that could cause this?

bilde

Assuming you’re referring to this:

        value_template: >
          {{ states('sensor.usg_wan_out_stats')|float*8/1024/1024|round(2) }}

…no, that would never have rounded to 2 decimal places. The round is only applied to the final 1024. Add brackets to show what you want rounded (and a default for the float):

        value_template: >
          {{ (states('sensor.usg_wan_out_stats')|float(0)*8/1024/1024)|round(2) }}

…and similarly for the other template.

1 Like

I must be doing something wrong in HA, my internet download is showing zero for my USG. Sensor WAN in and WAN out are showing data.

Show the code you’re using that is giving the zero result, properly formatted. Please paste this into Developer Tools / Template and paste the result back here:

1. {{ states('sensor.usg_wan_in') }}
2. {{ states('sensor.usg_wan_out') }}
3. {{ states('sensor.usg_wan_in_stats') }}
4. {{ states('sensor.usg_wan_out_stats') }}