Hello everyone,
New to the forum, but happy user of HA for more than a year now.
Would anyone have a complete example on how to “GET” data that requires a “Bearer token” in the Authorization of the request ? I can’t find a way to map a curl commands workflow to HA sensors.
One of my appliances has a REST API (and I don’t want to use the vendor’s app to read these values, I have a nice dashboard). These two curl commands work fine from the CLI :
# Authentication
curl -X 'POST' 'https://myvendor.com/path-to-token' -H 'Content-Type: application/json' -d '{"userName":"", “password":""}'
# Sensors
curl -X 'GET' 'https://myvendor.com/path/to/the/list/of/sensors?id=<myuserid>' -H 'accept: */*' -H 'Authorization: Bearer <token>'
In configuration.yaml, I tried rest, rest_command, restful sensors … to no avail. I’m lost, totally … Been through docs, Google, Github examples (even asked ChatGPT for a good laugh), but I couldn’t find anything that looks like what I’m trying to achieve (and some “successful” forum entries made me think of the “Rest of the owl” subreddit …).
My token is always empty, so I can’t even start reading values
Latest attempt:
rest:
- resource: "https://https://myvendor.com/path-to-token"
method: POST
payload: '{"userName": "N is uppercase", "password": "hardcoded, YOLO"}'
headers:
Content-Type: "application/json"
sensor:
- name: "token"
value_template: "{{ value_json.data.token }}"
- name: "refresh_token"
value_template: "{{ value_json.data.tokenExpiringdata }}"
- sensor:
- name: "Bearer"
state: "{{ states('sensor.token') }}"
- name: "Refresh"
state: "{{ states('sensor.refresh_token') }}"
Restarted HA, even rebooted, but the sensor values are empty So I can’t even try the second step either
I also tried with restful commands, still no joy. I tried hardcoding name and password in the config file, no luck either.
Would anyone have a similar working config I can get inspired from ?