Vasai
(Vasily)
November 10, 2021, 7:40pm
1
Good day!
I want to make a request to a specific API when a trigger fires, I understand how to do this kind of automation.
But in order to request this API, I first need to login and get the token in JSON format:
https://192.168.1.10:8080/login?password=12345
{
"success": 1,
"sid": "Dui79RfO"
}
I need to use the resulting “sid” value for subsequent requests:
https://192.168.1.10:8080/settings/channels/name?sid=Dui79RfO
Please tell me how can I save the value received in the request for future use?
I am making requests using “RESTful Command”, it might be better to use templates for this.
Use two restful sensors one for logging in an storing the token and one for requesting the data
Vasai
(Vasily)
November 12, 2021, 9:16pm
3
Thank you very much for the advice, I didn’t know about the possibility of creating “rest” sensors.
I was able to get the token and write it:
- platform: rest
resource: https://192.168.1.10:8080/login?password=12345
verify_ssl: false
name: get token
value_template: "{{ value_json.sid }}"
But I can’t imagine how to substitute it in the request.
Tried specifying directly in the query string:
- platform: rest
resource: https://192.168.1.10:8080/settings/channels/name?sid=sensor.get_token
verify_ssl: false
name: device name
value_template: "{{ value_json.value }}"
Also tried to specify in headers:
- platform: rest
resource: https://192.168.1.10:8080/settings/channels/name
verify_ssl: false
name: device name
value_template: "{{ value_json.value }}"
headers:
sid: "{{ states('sensor.get_token') }}"
But in return I get the state “unknown”.
Please tell me what am I doing wrong?
Sorry for the late reply:
You would need to use resource_template
instead of resource
. See below:
- platform: rest
resource_template: "https://192.168.1.10:8080/settings/channels/name?sid={{states('sensor.get_token')}}"
verify_ssl: false
name: device name
value_template: "{{ value_json.value }}"