UniFi Network Integration - Confirm WAN Internet is UP/Down?

I just installed the UniFi Network Integration and trying to see if there are entities that I can leverage in a template to confirm if WAN1 and/or WAN2 have internet or not. I was hoping to leverage a entity/sensor/template based on info available within Unifi so that I do not need to manage an additional ping test and/or need to create a lot of reporting data by monitoring Rx/Tx, WAN Latency, etc.

Am I missing an easy sensor/entity I should be leveraging. Or what’s the cleanest way someone has been able to trigger based on internet is up or down. If nothing exists I’m kinda of surprised as this seems to be something someone would surely want.

Do not have UniFi devices (I’m on Draytek ecosystem), so I cannot judge if such information is available via integration. In my case, due to lack of any integration, I check status of WAN using SNMP sensor. I’ve seen that UniFi also supports SNMP, so this might be alternative for you.

Unfortunately, I thought of this too and it only reports WAN physical status of up and down. None of the internet up/down passes through without having to leveragr transmit, etc. Adding complexitty and further metrics I need to monitor.

As I thought of this, in fact in case of Draytek routers status of internet connection is made from ping sensor configured on router itself and only exposed via SNMP. So your initial idea of creation of ping sensor to something on WAN side might be quite reasonable…

It’s been long time i got online status reported, but i think i have it through UPNP/IDG integration. That would be a more universal solution.

Yes ping is the likely solution. However I wanted to minimize duplicating pinging that’s already occurring and minimize reporting by leveraging existing items where possible. I know the ping traffic and data is negligible. However my ocd and good practice using what’s available tends to prevail whenever I build something.

I also liked the idea of internet up down check living on the closest to internet device for reporting (router/udmse) vs relaying on something further down the line to reduce internal changes flapping it.

I’ll likely end up using uptime-kuma in a docker and go overboard down another hole of monitor all my core devices outside of just internet up and down.

Thats also what I did, I used tracert to figure out the first device outside my home network that is pingable and configured it as sensor on router. BUT it has also some drawback; accessibiulity of device on ISP side only tells if their network in vicinity is up buit they might have outage elsewhere, down the the line, that makes Internet unavailable. Well, too many cases to track all of them :slight_smile:

If you add these 2 sensors, there is a lot of data available, including WAN status. You’ll need an API key from the controller. I use these and use template sensors to generate whatever I need

  - platform: rest
    name: UniFi WAN Raw
    unique_id: unifi_wan_raw
    resource: https://192.168.1.1/proxy/network/api/s/default/stat/health
    headers:
      X-API-Key: xxxxxxxxxxxxxxxxxxxxxxx
    verify_ssl: false
    value_template: "OK"
    json_attributes:
      - data

  - platform: rest
    name: UniFi Gateway Devices
    unique_id: unifi_gateway_devices
    resource: https://192.168.1.1/proxy/network/api/s/default/stat/device
    headers:
      X-API-Key: xxxxxxxxxxxxxxxxxxxxxxx
    verify_ssl: false
    value_template: "OK"
    json_attributes:
      - data
1 Like

This looks like exactly what I need. I’d love to see one of your template sensors if you’re confirming WAN up/down.

WAN Upload:

{% set d = state_attr('sensor.unifi_wan_raw','data') | default([], true) %}
{% set wan = (d | selectattr('subsystem','equalto','wan') | list | first) %}
{% set tx = wan['tx_bytes-r'] if wan is defined and 'tx_bytes-r' in wan else 0 %}
{{ ((tx | float(0)) * 8 / 1000000) | round(2) }}

WAN Download:

{% set d = state_attr('sensor.unifi_wan_raw','data') | default([], true) %}
{% set wan = (d | selectattr('subsystem','equalto','wan') | list | first) %}
{% set rx = wan['rx_bytes-r'] if wan is defined and 'rx_bytes-r' in wan else 0 %}
{{ ((rx | float(0)) * 8 / 1000000) | round(2) }}

WAN1:

{% set d = state_attr('sensor.unifi_wan_raw','data') | default([], true) %}
{% set wan = (d | selectattr('subsystem','equalto','wan') | list | first) %}
{% set s = wan.uptime_stats if wan is defined and 'uptime_stats' in wan else dict() %}
{% set w1 = s.WAN if 'WAN' in s else dict() %}
{{ (wan.status == 'ok') and (w1.get('availability', 0) | float(0)) > 0 }}

WAN2:

{% set d = state_attr('sensor.unifi_wan_raw','data') | default([], true) %}
{% set wan = (d | selectattr('subsystem','equalto','wan') | list | first) %}
{% set s = wan.uptime_stats if wan is defined and 'uptime_stats' in wan else dict() %}
{% set w2 = s.WAN2 if 'WAN2' in s else dict() %}
{{ (wan.status == 'ok') and (w2.get('availability', 0) | float(0)) > 0 }}

WAN Up:

{% set devs = state_attr('sensor.unifi_gateway_devices','data') | default([], true) %}
{% set gw = (devs | selectattr('type','equalto','udm') | list | first)
            or (devs | selectattr('type','equalto','ugw') | list | first) %}
{{ gw is defined and gw.uplink is defined and gw.uplink.up }}

Active WAN Name:

{% set devs = state_attr('sensor.unifi_gateway_devices','data') | default([], true) %}
{% set gw = (devs | selectattr('type','equalto','udm') | list | first)
            or (devs | selectattr('type','equalto','ugw') | list | first) %}
{{ gw.uplink.name if gw is defined and gw.uplink is defined else 'unknown' }}

Thank you so much. This was so helpful and pointed me in the right direction of what I wanted to do. I went even a simpler route.

I see the uptime_stats for each WAN publishes a uptime with a value or a downtime with a value, so keying off that was enough to confirm UP/Down!

There is so much other good data here in the API I totally forgot about, as well as their Site Manager API that I will be able to leverage.

Where would I find the API Key? I’m on a UDM (Dream Machine) with all updates current. I don’t recall ever seeing an API Key anywhere in the interface. Thanks!

For the local network API:
Setting>Control Plane>Integrations>Create API Key

For the Site Manager API
Unifi.ui.com > Right side bar > select API

@Holdestmade - your WAN Up. Will this reflect as up no matter if WAN1 or WAN2 is UP?
I’m not at a point where I can take my WANs down to test until next week sometime when I get home so also curious is the up state purely IP based or does it take into any considered ability for traffic to pass?

Not sure, I’ve never actually had a wan offline to test

Quickly put together a custom component for the WAN info and speedtest info, feel free to use it.

3 Likes

This looks very interesting and im wondering if it can help my dilemma.
My UDM, is connected solely on a 4G sim and my isp throttles the internet when i reach 600gb over a month period, in my case it resets on the 11th. Is there an exposed total data entity that will help?

I’ll have a look tomorrow

Thank you much appreciated.

I can’t see any endpoint that shows totals, but couldn’t you use the download sensor and use an integration sensor and a utility meter (with a 10 day offset) for this ?
EDIT - just tried this and I now have WAN upload and download totals for the month