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:
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
disable_warnings()
SECRETS_FILE = "/config/secrets.yaml"
def get_secret(secret):
try:
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")
exit()
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]
print(json.dumps({
"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"]
}))
EOF
command_timeout: 180
value_template: >
{{ value_json.version }}
json_attributes:
- cpu
- cpu_temp
- mem
- disk
# - internet
- uptime
- version
scan_interval: 900