Unifi sensor

HIim trying to make some sesnor for the unifi controller, and im having problems with the authentication and rest sensors,
i get the cookie but thats it, i saw a solution similar from this topic.

can @Vsider or @Mattias_Persson share your sensors for this? it would very appreciated.
I am trying to use the rest platform to be able to use attribute_templates:


I switched to python https://github.com/matt8707/hass-config/blob/0c7f0d0b210e46729d8d9e58a9005f7646f9b51a/packages/router_unifi.yaml
as you can see in the script you should create these secrets


1 Like

Hi thanks for your response, in your code , i see you get the “data” part of the json response and is exactly what im looking for, but i need to put the complete response on an attribute (because of the 255 character limit), not a programmer here and i cant figure it out could you show me how to put the full json in the attribute instead of specific parts?

  - platform: command_line
    name: udm_unifios
    command: |-
      python3 << EOF
      import json, yaml, requests
      from datetime import datetime
      from urllib3 import disable_warnings

      SECRETS_FILE = "/config/secrets.yaml"
      def get_secret(secret):
              with open(SECRETS_FILE, "r", encoding="utf8") as file:
                  secrets = yaml.full_load(file)
                  for key, value in secrets.items():
                      if key == secret:
                          return value
          except FileNotFoundError:
              print("secrets.yaml not found")

      IP = get_secret("unifi_ip")
      PORT = get_secret("unifi_port")
      USER = get_secret("unifi_user")
      PASS = get_secret("unifi_pass")
      URL = f"https://{IP}:{PORT}"

      login = requests.request("POST", f"{URL}/api/auth/login", \
          headers={"Content-Type": "application/json"}, \
          data=json.dumps({"username": USER, "password": PASS}), verify=False)
      response = requests.request("GET", f"{URL}/proxy/network/api/s/default/stat/device/", \
          cookies=login.cookies, verify=False)
      data = response.json()["data"][0]

          "cpu": data["system-stats"]["cpu"],
          "cpu_temp": round(data["temperatures"][1]["value"], 1),
          "mem": data["system-stats"]["mem"],
          "disk": round(data["storage"][1]["used"] / data["storage"][1]["size"] * 100, 1),
          # "internet": data["internet"],
          "uptime": datetime.fromtimestamp(data["startup_timestamp"]).isoformat(),
          "version": data["displayable_version"]
    command_timeout: 180
    value_template: >
      {{ value_json.version }}
      - cpu
      - cpu_temp
      - mem
      - disk
      # - internet
      - uptime
      - version
    scan_interval: 900

thats for your help!

you can print the whole response to your command line

Thanks, but i went an other way, just with simple sensor toe get all the data then will use templates to separate the information i need.

here is the sensor

  - platform: command_line
    name: unifi_stats
    command: 'curl -H ''Content-Type: application/json'' -d ''{"username": "Your_User", "password": "Your_password"}'' -ksc - https://your_url:8443/api/login -o /dev/null | curl -ksb - https://your_url:8443/api/stat/sites'
    value_template: 'OK'
      - data
    scan_interval: 900

@Mattias_Persson Thanks for your help and guidence!