API call to Unifi UDM

I’m new to Home Assistant and exploring its capabilities. I’m trying to authenticate against the UniFi UDM using Home Assistant’s REST commands and query the results. I’ve successfully done this using Python, but now I’m interested in achieving the same using the REST integration in Home Assistant OS (HAOS).

Here’s what I have so far: I’ve set up the authentication part, and it seems to be working. On the Overview dashboard, I see the “unifi auth” entity, and clicking on it shows the deviceToken in its time slot.

The next step is to query another URL to retrieve the JSON results, and I want to do this natively in HAOS using the REST integration, rather than Python. I’m unsure how to proceed with using the authenticated token to make this additional request.

Any guidance on how to authenticate and then use that authentication to query the next URL and gather the JSON results?

# Authentication with first API
sensor:
  - platform: rest
    name: unifi auth
    resource: https://X.X.X.1:443/api/auth/login
    method: POST
    payload: !secret unifi_payload
    headers:
      Content-Type: "application/json"
    verify_ssl: false
    value_template: "{{ value_json.deviceToken }}"
  # Query second API using authentication from first
  - platform: rest
    name: unifi health
    resource: "https://X.X.X.1:443/proxy/network/api/s/default/stat/health/"
    method: GET
    headers:
      Authorization: 'Bearer {{ states("sensor.unifi_auth") }}'
    value_template: "{{ value_json.result }}"

Also, when it comes to debugging this, I set homeassistant.core: debug, I still don’t see much more information. Is there a debug for API? Any clues on how to degub this would be great also.

Any help is appreciated.

Couldn’t you implement this in the UniFi integration if you’ve done it in python?

Yes, but I am using the HAOS that doesn’t allow me to “import requests” in the script. I guess python is locked down within the OS. I did a manual pip install and it worked, reboot the OS its gone, I get it is a container. I just wanted to see if I could do the same via the API method.

Do a search on my name for past posts. I have a video that I made that details how to do all this using python, where to store the scripts, how to call hem, all of it, for my UDM pro. Works great, even if several of the features have subsequently been included into the main integration now. But my code is all there and the rest of the explanations that you can leverage to do whatever you need.

I meant a proper implementation upstream that would benefit more users