{
  "$schema": "./.schemas/page.schema.json",
  "title": "UniFi",
  "icon": "wifi.router",
  "sources": {
    "unifi": {
      "url": "https://api.ui.com",
      "secret": "unifi",
      "auth": {
        "type": "header",
        "name": "X-API-Key"
      },
      "every": "2m",
      "requests": {
        "sites": {
          "path": "/v1/sites?pageSize=500",
          "follow": {
            "items": "data",
            "param": "nextToken",
            "from": "nextToken",
            "max": 4
          }
        },
        "hosts": {
          "path": "/v1/hosts?pageSize=50",
          "follow": {
            "items": "data",
            "param": "nextToken",
            "from": "nextToken",
            "max": 4
          }
        },
        "devices": {
          "path": "/v1/devices?pageSize=500",
          "follow": {
            "items": "data",
            "param": "nextToken",
            "from": "nextToken",
            "max": 4
          }
        }
      },
      "derive": [
        {
          "name": "console_all",
          "value": {
            "each": "devices.data",
            "flatten": "devices",
            "fields": {
              "host": "parent.hostId",
              "status": "status",
              "console": "isConsole"
            }
          }
        },
        {
          "name": "console_devs",
          "value": {
            "each": "console_all",
            "where": {
              "path": "console",
              "equals": true
            },
            "fields": {
              "host": "host",
              "status": "status"
            }
          }
        },
        {
          "name": "site_list",
          "value": {
            "each": "sites.data",
            "lookup": {
              "from": "hosts.data",
              "by": "id",
              "on": "hostId",
              "as": "host"
            },
            "fields": {
              "host_id": "hostId",
              "id": "siteId",
              "name": {
                "if": {
                  "path": "meta.desc",
                  "in": [
                    "Default",
                    "default",
                    ""
                  ]
                },
                "then": {
                  "if": {
                    "all": [
                      {
                        "path": "host.reportedState.name",
                        "exists": true
                      },
                      {
                        "path": "host.reportedState.name",
                        "not": ""
                      }
                    ]
                  },
                  "then": {
                    "template": "{host.reportedState.name}"
                  },
                  "else": {
                    "template": "Default"
                  }
                },
                "else": {
                  "template": "{meta.desc}"
                }
              },
              "isp": "statistics.ispInfo.name",
              "wans": {
                "count": "statistics.wans",
                "entries": true,
                "default": 0
              },
              "up": {
                "count": "statistics.wans",
                "entries": true,
                "where": {
                  "path": "value.portUp",
                  "equals": true
                },
                "default": 0
              },
              "up_names": {
                "join": "key",
                "of": "statistics.wans",
                "entries": true,
                "where": {
                  "path": "value.portUp",
                  "equals": true
                },
                "separator": ", "
              },
              "down_names": {
                "join": "key",
                "of": "statistics.wans",
                "entries": true,
                "where": {
                  "path": "value.portUp",
                  "not": true
                },
                "separator": ", "
              },
              "gateway_off": {
                "path": "statistics.counts.offlineGatewayDevice",
                "default": 0
              },
              "connected": {
                "if": {
                  "path": "host.reportedState.state",
                  "equals": "connected"
                },
                "then": {
                  "template": "yes"
                },
                "else": {
                  "if": {
                    "path": "host.reportedState.state",
                    "equals": "disconnected"
                  },
                  "then": {
                    "template": "no"
                  },
                  "else": {
                    "template": "unknown"
                  }
                }
              },
              "since": "host.lastConnectionStateChange",
              "counts": "statistics.counts",
              "retry": "statistics.percentages.txRetry",
              "gateway": "statistics.gateway.shortname"
            }
          }
        },
        {
          "name": "site_words",
          "value": {
            "each": "site_list",
            "lookup": {
              "from": "console_devs",
              "by": "host",
              "on": "host_id",
              "as": "cdev"
            },
            "fields": {
              "host_id": "host_id",
              "id": "id",
              "name": "name",
              "isp": "isp",
              "wans": "wans",
              "up": "up",
              "up_names": "up_names",
              "down_names": "down_names",
              "gateway_off": "gateway_off",
              "connected": {
                "if": {
                  "path": "cdev.status",
                  "equals": "offline"
                },
                "then": {
                  "template": "no"
                },
                "else": "connected"
              },
              "since": "since",
              "counts": "counts",
              "retry": "retry",
              "gateway": "gateway",
              "not_connected": {
                "if": {
                  "all": [
                    {
                      "path": "down_names",
                      "exists": true
                    },
                    {
                      "path": "down_names",
                      "not": ""
                    }
                  ]
                },
                "then": {
                  "template": " · {down_names} not connected"
                },
                "else": {
                  "template": ""
                }
              }
            }
          }
        },
        {
          "name": "site_rows",
          "value": {
            "each": "site_words",
            "fields": {
              "host_id": "host_id",
              "id": "id",
              "name": "name",
              "isp": "isp",
              "wans": "wans",
              "up": "up",
              "up_names": "up_names",
              "down_names": "down_names",
              "gateway_off": "gateway_off",
              "connected": "connected",
              "since": "since",
              "counts": "counts",
              "retry": "retry",
              "gateway": "gateway",
              "state": {
                "if": {
                  "path": "connected",
                  "equals": "no"
                },
                "then": {
                  "template": "critical"
                },
                "else": {
                  "if": {
                    "path": "connected",
                    "equals": "unknown"
                  },
                  "then": {
                    "template": "unknown"
                  },
                  "else": {
                    "if": {
                      "path": "gateway_off",
                      "gt": 0
                    },
                    "then": {
                      "template": "critical"
                    },
                    "else": {
                      "if": {
                        "path": "wans",
                        "equals": 0
                      },
                      "then": {
                        "template": "unknown"
                      },
                      "else": {
                        "if": {
                          "path": "up",
                          "equals": 0
                        },
                        "then": {
                          "template": "critical"
                        },
                        "else": {
                          "template": "ok"
                        }
                      }
                    }
                  }
                }
              },
              "detail": {
                "if": {
                  "path": "connected",
                  "equals": "no"
                },
                "then": {
                  "if": {
                    "all": [
                      {
                        "path": "since",
                        "exists": true
                      },
                      {
                        "path": "since",
                        "not": ""
                      }
                    ]
                  },
                  "then": {
                    "template": "Not reporting to UniFi since {since|ago}"
                  },
                  "else": {
                    "template": "Not reporting to UniFi"
                  }
                },
                "else": {
                  "if": {
                    "path": "connected",
                    "equals": "unknown"
                  },
                  "then": {
                    "template": "UniFi has no report from its console"
                  },
                  "else": {
                    "if": {
                      "path": "gateway_off",
                      "gt": 0
                    },
                    "then": {
                      "template": "Gateway offline"
                    },
                    "else": {
                      "if": {
                        "path": "wans",
                        "equals": 0
                      },
                      "then": {
                        "template": "UniFi hasn't reported its WANs"
                      },
                      "else": {
                        "if": {
                          "path": "up",
                          "equals": 0
                        },
                        "then": {
                          "template": "No WAN connected{not_connected}"
                        },
                        "else": {
                          "if": {
                            "all": [
                              {
                                "path": "isp",
                                "exists": true
                              },
                              {
                                "path": "isp",
                                "not": ""
                              }
                            ]
                          },
                          "then": {
                            "template": "{isp} · {up_names} up{not_connected}"
                          },
                          "else": {
                            "template": "{up_names} up{not_connected}"
                          }
                        }
                      }
                    }
                  }
                }
              },
              "link": {
                "template": "legato://page/unifi-site?site={id|raw}&host={host_id|raw}"
              }
            }
          }
        },
        {
          "name": "n_sites",
          "value": {
            "count": "site_rows",
            "default": 0
          }
        },
        {
          "name": "n_sites_ok",
          "value": {
            "count": "site_rows",
            "where": {
              "path": "state",
              "equals": "ok"
            },
            "default": 0
          }
        },
        {
          "name": "one",
          "value": {
            "each": "site_rows",
            "limit": 1,
            "fields": {
              "id": "id",
              "isp": "isp",
              "up_names": "up_names",
              "link": "link",
              "state": "state",
              "detail": "detail"
            }
          }
        },
        {
          "name": "console_rows",
          "value": {
            "each": "hosts.data",
            "lookup": {
              "from": "console_devs",
              "by": "host",
              "on": "id",
              "as": "cdev"
            },
            "fields": {
              "id": "id",
              "name": {
                "if": {
                  "all": [
                    {
                      "path": "reportedState.name",
                      "exists": true
                    },
                    {
                      "path": "reportedState.name",
                      "not": ""
                    }
                  ]
                },
                "then": {
                  "template": "{reportedState.name|clip:24}"
                },
                "else": {
                  "if": {
                    "all": [
                      {
                        "path": "reportedState.hostname",
                        "exists": true
                      },
                      {
                        "path": "reportedState.hostname",
                        "not": ""
                      }
                    ]
                  },
                  "then": {
                    "template": "{reportedState.hostname|clip:24}"
                  },
                  "else": {
                    "template": "Console"
                  }
                }
              },
              "state": {
                "if": {
                  "path": "cdev.status",
                  "equals": "offline"
                },
                "then": {
                  "template": "critical"
                },
                "else": {
                  "if": {
                    "path": "reportedState.state",
                    "equals": "connected"
                  },
                  "then": {
                    "template": "ok"
                  },
                  "else": {
                    "if": {
                      "path": "reportedState.state",
                      "equals": "disconnected"
                    },
                    "then": {
                      "template": "critical"
                    },
                    "else": {
                      "template": "unknown"
                    }
                  }
                }
              },
              "detail": {
                "if": {
                  "path": "cdev.status",
                  "equals": "offline"
                },
                "then": {
                  "if": {
                    "all": [
                      {
                        "path": "lastConnectionStateChange",
                        "exists": true
                      },
                      {
                        "path": "lastConnectionStateChange",
                        "not": ""
                      }
                    ]
                  },
                  "then": {
                    "template": "Not reporting to UniFi since {lastConnectionStateChange|ago}"
                  },
                  "else": {
                    "template": "Not reporting to UniFi"
                  }
                },
                "else": {
                  "if": {
                    "path": "reportedState.state",
                    "equals": "connected"
                  },
                  "then": {
                    "if": {
                      "all": [
                        {
                          "path": "reportedState.version",
                          "exists": true
                        },
                        {
                          "path": "reportedState.version",
                          "not": ""
                        }
                      ]
                    },
                    "then": {
                      "template": "Connected · UniFi OS {reportedState.version}"
                    },
                    "else": {
                      "template": "Connected"
                    }
                  },
                  "else": {
                    "if": {
                      "path": "reportedState.state",
                      "equals": "disconnected"
                    },
                    "then": {
                      "if": {
                        "all": [
                          {
                            "path": "lastConnectionStateChange",
                            "exists": true
                          },
                          {
                            "path": "lastConnectionStateChange",
                            "not": ""
                          }
                        ]
                      },
                      "then": {
                        "template": "Not reporting to UniFi since {lastConnectionStateChange|ago}"
                      },
                      "else": {
                        "template": "Not reporting to UniFi"
                      }
                    },
                    "else": {
                      "if": {
                        "all": [
                          {
                            "path": "reportedState.state",
                            "exists": true
                          },
                          {
                            "path": "reportedState.state",
                            "not": ""
                          }
                        ]
                      },
                      "then": {
                        "template": "{reportedState.state|title}"
                      },
                      "else": {
                        "template": "UniFi has no report from it yet"
                      }
                    }
                  }
                }
              }
            }
          }
        },
        {
          "name": "n_consoles",
          "value": {
            "count": "console_rows",
            "default": 0
          }
        },
        {
          "name": "site_info_named",
          "value": {
            "each": "site_rows",
            "fields": {
              "base": {
                "template": "{name|clip:24}"
              },
              "tail": {
                "path": "id",
                "match": "(.{4})$",
                "default": {
                  "template": ""
                }
              },
              "state": "state",
              "detail": "detail",
              "link": "link"
            }
          }
        },
        {
          "name": "site_info_by_name",
          "value": {
            "each": "site_info_named",
            "group": "base",
            "fields": {
              "key": "key",
              "count": "count",
              "rows": "rows"
            }
          }
        },
        {
          "name": "site_info_rows",
          "value": {
            "each": "site_info_by_name",
            "flatten": "rows",
            "fields": {
              "name": {
                "if": {
                  "path": "parent.count",
                  "gt": 1
                },
                "then": {
                  "template": "{base|clip:19} {tail|raw}"
                },
                "else": {
                  "template": "{base}"
                }
              },
              "state": "state",
              "detail": {
                "template": "{detail|clip:80}"
              },
              "rank": {
                "path": "state",
                "values": {
                  "critical": 0,
                  "warning": 1,
                  "unknown": 2
                },
                "default": 3
              },
              "url": {
                "if": {
                  "path": "link",
                  "exists": true
                },
                "then": "link"
              }
            }
          }
        },
        {
          "name": "site_info_shown",
          "value": {
            "each": "site_info_rows",
            "sort": {
              "by": [
                "rank",
                "name"
              ]
            },
            "limit": 60,
            "fields": {
              "name": "name",
              "state": "state",
              "detail": "detail",
              "url": "url"
            }
          }
        },
        {
          "name": "console_info_named",
          "value": {
            "each": "console_rows",
            "fields": {
              "base": {
                "template": "{name|clip:24}"
              },
              "tail": {
                "path": "id",
                "match": "(.{4})$",
                "default": {
                  "template": ""
                }
              },
              "state": "state",
              "detail": "detail",
              "link": "link"
            }
          }
        },
        {
          "name": "console_info_by_name",
          "value": {
            "each": "console_info_named",
            "group": "base",
            "fields": {
              "key": "key",
              "count": "count",
              "rows": "rows"
            }
          }
        },
        {
          "name": "console_info_rows",
          "value": {
            "each": "console_info_by_name",
            "flatten": "rows",
            "fields": {
              "name": {
                "if": {
                  "path": "parent.count",
                  "gt": 1
                },
                "then": {
                  "template": "{base|clip:19} {tail|raw}"
                },
                "else": {
                  "template": "{base}"
                }
              },
              "state": "state",
              "detail": {
                "template": "{detail|clip:80}"
              },
              "rank": {
                "path": "state",
                "values": {
                  "critical": 0,
                  "warning": 1,
                  "unknown": 2
                },
                "default": 3
              },
              "url": {
                "if": {
                  "path": "link",
                  "exists": true
                },
                "then": "link"
              }
            }
          }
        },
        {
          "name": "console_info_shown",
          "value": {
            "each": "console_info_rows",
            "sort": {
              "by": [
                "rank",
                "name"
              ]
            },
            "limit": 60,
            "fields": {
              "name": "name",
              "state": "state",
              "detail": "detail",
              "url": "url"
            }
          }
        },
        {
          "name": "clients_wifi",
          "value": {
            "sum": "counts.wifiClient",
            "of": "site_rows",
            "default": 0
          }
        },
        {
          "name": "clients_wired",
          "value": {
            "sum": "counts.wiredClient",
            "of": "site_rows",
            "default": 0
          }
        },
        {
          "name": "clients",
          "value": {
            "add": [
              "clients_wifi",
              "clients_wired"
            ]
          }
        },
        {
          "name": "retry",
          "value": {
            "max": "retry",
            "of": "site_rows",
            "round": 0
          }
        },
        {
          "name": "dev_flat",
          "value": {
            "each": "devices.data",
            "flatten": "devices",
            "fields": {
              "name": "name",
              "model": "model",
              "status": "status",
              "firmwareStatus": "firmwareStatus",
              "version": "version",
              "productLine": "productLine",
              "parent": "parent",
              "mac_tail": {
                "path": "mac",
                "match": "(.{4})$",
                "default": {
                  "template": ""
                }
              }
            }
          }
        },
        {
          "name": "dev_named",
          "value": {
            "each": "dev_flat",
            "fields": {
              "base": {
                "if": {
                  "all": [
                    {
                      "path": "name",
                      "exists": true
                    },
                    {
                      "path": "name",
                      "not": ""
                    }
                  ]
                },
                "then": {
                  "template": "{name|clip:24}"
                },
                "else": {
                  "if": {
                    "all": [
                      {
                        "path": "model",
                        "exists": true
                      },
                      {
                        "path": "model",
                        "not": ""
                      }
                    ]
                  },
                  "then": {
                    "template": "{model|clip:18} {mac_tail|raw}"
                  },
                  "else": {
                    "template": "Device {mac_tail|raw}"
                  }
                }
              },
              "name": "name",
              "model": "model",
              "status": "status",
              "firmwareStatus": "firmwareStatus",
              "version": "version",
              "productLine": "productLine",
              "host": "parent.hostName",
              "mac_tail": "mac_tail"
            }
          }
        },
        {
          "name": "dev_by_name",
          "value": {
            "each": "dev_named",
            "group": "base",
            "fields": {
              "key": "key",
              "count": "count",
              "rows": "rows"
            }
          }
        },
        {
          "name": "dev_rows",
          "value": {
            "each": "dev_by_name",
            "flatten": "rows",
            "fields": {
              "name": {
                "if": {
                  "path": "parent.count",
                  "gt": 1
                },
                "then": {
                  "template": "{base|clip:19} {mac_tail|raw}"
                },
                "else": {
                  "template": "{base}"
                }
              },
              "state": {
                "path": "status",
                "values": {
                  "online": "ok",
                  "offline": "critical"
                },
                "default": "unknown"
              },
              "detail": {
                "if": {
                  "path": "status",
                  "equals": "offline"
                },
                "then": {
                  "template": "Offline · {model} on {host}"
                },
                "else": {
                  "if": {
                    "path": "status",
                    "equals": "online"
                  },
                  "then": {
                    "if": {
                      "path": "firmwareStatus",
                      "in": [
                        "updatePending",
                        "updateAvailable"
                      ]
                    },
                    "then": {
                      "template": "{model} · {version} · update ready"
                    },
                    "else": {
                      "template": "{model} · {version} · on {host}"
                    }
                  },
                  "else": {
                    "template": "{status|title} · {model}"
                  }
                }
              },
              "group": {
                "path": "productLine",
                "values": {
                  "network": "Network",
                  "protect": "Protect"
                },
                "default": "Other"
              },
              "order": {
                "path": "productLine",
                "values": {
                  "network": 0,
                  "protect": 1
                },
                "default": 2
              },
              "status": "status",
              "update": {
                "if": {
                  "path": "firmwareStatus",
                  "in": [
                    "updatePending",
                    "updateAvailable"
                  ]
                },
                "then": {
                  "template": "yes"
                },
                "else": {
                  "template": "no"
                }
              },
              "rank": {
                "path": "status",
                "values": {
                  "offline": 0,
                  "online": 2
                },
                "default": 1
              }
            }
          }
        },
        {
          "name": "dev_picked",
          "value": {
            "each": "dev_rows",
            "sort": {
              "by": [
                "rank",
                "order",
                "name"
              ]
            },
            "limit": 60,
            "fields": {
              "name": "name",
              "state": "state",
              "detail": {
                "template": "{detail|clip:80}"
              },
              "group": "group",
              "order": "order"
            }
          }
        },
        {
          "name": "dev_shown",
          "value": {
            "each": "dev_picked",
            "sort": {
              "by": [
                "order",
                "name"
              ]
            },
            "fields": {
              "name": "name",
              "state": "state",
              "detail": "detail",
              "group": "group"
            }
          }
        },
        {
          "name": "dev_total",
          "value": {
            "count": "dev_rows",
            "default": 0
          }
        },
        {
          "name": "dev_offline",
          "value": {
            "count": "dev_rows",
            "where": {
              "path": "status",
              "equals": "offline"
            },
            "default": 0
          }
        },
        {
          "name": "dev_updates",
          "value": {
            "count": "dev_rows",
            "where": {
              "path": "update",
              "equals": "yes"
            },
            "default": 0
          }
        },
        {
          "name": "dev_cut",
          "value": {
            "if": {
              "path": "dev_total",
              "gt": 60
            },
            "then": {
              "template": "yes"
            },
            "else": {
              "template": "no"
            }
          }
        }
      ]
    },
    "isp": {
      "url": "https://api.ui.com",
      "secret": "unifi",
      "auth": {
        "type": "header",
        "name": "X-API-Key"
      },
      "every": "5m",
      "requests": {
        "isp": "/v1/isp-metrics/5m?duration=24h"
      },
      "derive": [
        {
          "name": "lat_rows",
          "value": {
            "each": "isp.data[0].periods",
            "fields": {
              "t": "metricTime",
              "ms": {
                "max": "value.avgLatency",
                "of": "data",
                "entries": true,
                "where": {
                  "path": "value.uptime",
                  "gt": 0
                }
              }
            }
          }
        },
        {
          "name": "lat",
          "value": {
            "each": "lat_rows",
            "where": {
              "path": "ms",
              "exists": true
            },
            "fields": {
              "t": "t",
              "ms": "ms"
            }
          }
        },
        {
          "name": "lat_n",
          "value": {
            "count": "lat",
            "default": 0
          }
        },
        {
          "name": "isp_sites",
          "value": {
            "count": "isp.data",
            "default": 0
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "sites",
      "type": "fleet",
      "label": "Sites",
      "placement": "page_info",
      "items": [],
      "source": {
        "ref": "unifi",
        "when": {
          "path": "n_sites",
          "gt": 0
        },
        "map": {
          "label": {
            "if": {
              "path": "n_sites",
              "gt": 60
            },
            "then": {
              "template": "Sites, 60 of {n_sites}"
            },
            "else": {
              "template": "Sites"
            }
          },
          "items": {
            "each": "site_info_shown",
            "sort": "name",
            "fields": {
              "name": "name",
              "state": "state",
              "detail": "detail",
              "url": "url"
            }
          }
        }
      }
    },
    {
      "id": "consoles",
      "type": "fleet",
      "label": "Consoles",
      "placement": "page_info",
      "items": [],
      "source": {
        "ref": "unifi",
        "failed": {
          "state": "critical",
          "detail": "UniFi's Site Manager isn't answering",
          "after": 3
        },
        "empty": {
          "text": "UniFi lists no consoles for this key.",
          "when": {
            "path": "n_consoles",
            "equals": 0
          }
        },
        "map": {
          "label": {
            "if": {
              "path": "n_consoles",
              "gt": 60
            },
            "then": {
              "template": "Consoles, 60 of {n_consoles}"
            },
            "else": {
              "template": "Consoles"
            }
          },
          "items": {
            "each": "console_info_shown",
            "sort": "name",
            "fields": {
              "name": "name",
              "state": "state",
              "detail": "detail",
              "url": "url"
            }
          }
        }
      }
    },
    {
      "id": "cut",
      "type": "text",
      "tone": "info",
      "body": "UniFi has more sites, consoles or devices than this page reads. Counts here cover part of them, and unifi.ui.com has the rest.",
      "source": {
        "ref": "unifi",
        "when": {
          "any": [
            {
              "all": [
                {
                  "path": "sites.nextToken",
                  "exists": true
                },
                {
                  "path": "sites.nextToken",
                  "not": ""
                }
              ]
            },
            {
              "all": [
                {
                  "path": "hosts.nextToken",
                  "exists": true
                },
                {
                  "path": "hosts.nextToken",
                  "not": ""
                }
              ]
            },
            {
              "all": [
                {
                  "path": "devices.nextToken",
                  "exists": true
                },
                {
                  "path": "devices.nextToken",
                  "not": ""
                }
              ]
            }
          ]
        }
      }
    },
    {
      "id": "network",
      "type": "facts",
      "label": "Network",
      "items": [
        {
          "key": "Internet",
          "value": "—"
        },
        {
          "key": "Clients",
          "value": "—"
        },
        {
          "key": "Devices",
          "value": "—"
        },
        {
          "key": "Wi-Fi retries",
          "value": "—"
        }
      ],
      "source": {
        "ref": "unifi",
        "when": {
          "path": "n_sites",
          "gt": 0
        },
        "map": {
          "items[0].value": {
            "if": {
              "path": "n_sites",
              "equals": 1
            },
            "then": {
              "template": "{one[0].detail|clip:80}"
            },
            "else": {
              "if": {
                "path": "n_sites",
                "equals": 0
              },
              "then": {
                "template": "No sites"
              },
              "else": {
                "template": "{n_sites_ok} of {n_sites} sites connected"
              }
            }
          },
          "items[0].url": {
            "if": {
              "path": "n_sites",
              "equals": 1
            },
            "then": "one[0].link"
          },
          "items[1].value": {
            "if": {
              "path": "clients",
              "gt": 0
            },
            "then": {
              "template": "{clients} · {clients_wifi} Wi-Fi · {clients_wired} wired"
            },
            "else": {
              "template": "{clients}"
            }
          },
          "items[2].value": {
            "if": {
              "path": "dev_offline",
              "gt": 0
            },
            "then": {
              "if": {
                "path": "dev_updates",
                "gt": 0
              },
              "then": {
                "template": "{dev_total} · {dev_offline} offline · {dev_updates} updates ready"
              },
              "else": {
                "template": "{dev_total} · {dev_offline} offline"
              }
            },
            "else": {
              "if": {
                "path": "dev_updates",
                "equals": 1
              },
              "then": {
                "template": "{dev_total} · 1 update ready"
              },
              "else": {
                "if": {
                  "path": "dev_updates",
                  "gt": 1
                },
                "then": {
                  "template": "{dev_total} · {dev_updates} updates ready"
                },
                "else": {
                  "template": "{dev_total}"
                }
              }
            }
          },
          "items[3].value": {
            "if": {
              "path": "retry",
              "exists": true
            },
            "then": {
              "if": {
                "path": "n_sites",
                "gt": 1
              },
              "then": {
                "template": "{retry}% at worst"
              },
              "else": {
                "template": "{retry}%"
              }
            },
            "else": {
              "template": "—"
            }
          }
        }
      }
    },
    {
      "id": "latency",
      "type": "sparkline",
      "label": "Latency",
      "unit": "ms",
      "change": false,
      "points": [
        0,
        0
      ],
      "start_label": "24h ago",
      "end_label": "Now",
      "source": {
        "ref": "isp",
        "when": {
          "all": [
            {
              "path": "isp_sites",
              "equals": 1
            },
            {
              "path": "lat_n",
              "gte": 2
            }
          ]
        },
        "map": {
          "points": {
            "each": "lat",
            "value": "ms",
            "buckets": 144
          },
          "times": {
            "each": "lat",
            "template": "{t}",
            "buckets": 144
          }
        }
      }
    },
    {
      "id": "devices",
      "type": "fleet",
      "label": "Devices",
      "max_items": 24,
      "items": [],
      "source": {
        "ref": "unifi",
        "empty": {
          "text": "UniFi lists no devices for this key.",
          "when": {
            "path": "dev_total",
            "equals": 0
          }
        },
        "map": {
          "label": {
            "if": {
              "path": "dev_cut",
              "equals": "yes"
            },
            "then": {
              "template": "Devices, 60 of {dev_total}"
            },
            "else": {
              "template": "Devices"
            }
          },
          "items": {
            "each": "dev_shown",
            "fields": {
              "name": "name",
              "state": "state",
              "detail": "detail",
              "group": "group"
            }
          }
        }
      }
    }
  ]
}
