Hi, I have this sensor:
command_line:
- switch:
command_on: 'curl -X POST "https://api.connectlife.io/api/v1/appliance" -H "accept: application/json" -H "Authorization: Bearer {{states("sensor.token_hisense_a")+states("sensor.token_hisense_b")}}" -H "Content-Type: application/json" -d "[{\"id\": \"xxx-xxx\",\"properties\": {\"Power\": \"1\"}}]"'
command_off: 'curl -X POST "https://api.connectlife.io/api/v1/appliance" -H "accept: application/json" -H "Authorization: Bearer {{states("sensor.token_hisense_a")+states("sensor.token_hisense_b")}}" -H "Content-Type: application/json" -d "[{\"id\": \"xxx-xxx\",\"properties\": {\"Power\": \"0\"}}]"'
command_state: 'curl -X GET "https://api.connectlife.io/api/v1/appliance/xxx-xxx" -H "accept: application/json" -H "Authorization: Bearer {{states("sensor.token_hisense_a")+states("sensor.token_hisense_b")}}"'
value_template: '{{ value_json[0].properties.Power == "1" }}'
name: Climatizzatore Ufficio 1
scan_interval: 60
But the commands don’t evaluate the {{states("sensor.token_hisense_a")+states("sensor.token_hisense_b")}}
How to do?
Thanks
Troon
(Troon)
January 15, 2024, 11:45am
2
(removed, next time I’ll read the docs myself first!)
Both solutions don’t work
koying
(Chris B)
January 15, 2024, 12:12pm
5
Templates are not supported in command_line
switch (at least, not there)
Why not using a REST switch?
gjohansson
(G Johansson)
January 15, 2024, 12:57pm
6
Like already mentioned command_line
does not support templates in the commands so either find alterantives like above with RESTful or make a feature request so we can add the support.
Good idea. Anyway it is not possible using the states on the parameters:
This one works:
switch:
- platform: rest
name: Climatizzatore Ufficio 1
resource: "https://api.connectlife.io/api/v1/appliance"
state_resource: "https://api.connectlife.io/api/v1/appliance/xxx"
body_on: '[{"id": "xxx","properties": {"Power": "1"}}]'
body_off: '[{"id": "xxx","properties": {"Power": "0"}}]'
is_on_template: '{{ value_json[0].properties.Power == "1" }}'
method: POST
headers:
Content-Type: application/json
accept: application/json
Authorization: "Bearer xxxxxx"
verify_ssl: false
This one NOT:
switch:
- platform: rest
name: Climatizzatore Ufficio 1
resource: "https://api.connectlife.io/api/v1/appliance"
state_resource: "https://api.connectlife.io/api/v1/appliance/xxx"
body_on: '[{"id": "xxx","properties": {"Power": "1"}}]'
body_off: '[{"id": "xxx","properties": {"Power": "0"}}]'
is_on_template: '{{ value_json[0].properties.Power == "1" }}'
method: POST
headers:
Content-Type: application/json
accept: application/json
Authorization: "Bearer {{states('sensor.token_hisense_a')+states('sensor.token_hisense_b')}}"
verify_ssl: false
gjohansson
(G Johansson)
January 15, 2024, 1:53pm
8
Seems kinda strange to have auth tokens as a state of a sensor?
But as you’ve noticed not everything works with templates so I propose to go for feature requests with what makes sense.
Meaning likely yes for command_line
but perhaps not so much for an auth header in rest
It is a 357 character token. I need to split it in 2 parts. I do not have idea how and where to store it dynamically
koying
(Chris B)
January 15, 2024, 2:46pm
10
Does {{states('sensor.token_hisense_a')+states('sensor.token_hisense_b')}}
produce what you expect in the template debugger?
Yes, it gives the correct string
gjohansson
(G Johansson)
January 15, 2024, 2:56pm
12
I don’t think it matters as there is nothing that I’m aware of that can consume it dynamically. As you already found out neither command_line
nor rest
can use it dynamically (template) right now.
Troon
(Troon)
January 15, 2024, 3:38pm
14
This example in the Rest documentation suggests otherwise:
2 Likes
gjohansson
(G Johansson)
January 15, 2024, 3:55pm
15
You’re absolutely right and I did look in the code which should support using templates in headers so if this isn’t working an issue should be made to fix it
A solution:
switch:
- platform: rest
name: Climatizzatore Ufficio 1
resource: "https://api.connectlife.io/api/v1/appliance"
state_resource: "https://api.connectlife.io/api/v1/appliance/xxx"
body_on: >
[{"id": "xxx","properties": {"Power": "{{states('sensor.power')}}"}}]
body_off: '[{"id": "xxx","properties": {"Power": "0"}}]'
is_on_template: '{{ value_json[0].properties.Power == "1" }}'
method: POST
headers:
Content-Type: application/json
accept: application/json
Authorization: >
Bearer {{states('sensor.token_hisense_a')+states('sensor.token_hisense_b')}}
verify_ssl: false
It works!
koying
(Chris B)
January 16, 2024, 10:28am
17
Looks exactly the same as you “did not work” above (besides the YAML continuations)
Unfortunately there is something wrong:
If I create this switch:
switch:
- platform: rest
name: Climatizzatore Ufficio 1
resource: https://api.connectlife.io/api/v1/appliance
state_resource: https://api.connectlife.io/api/v1/appliance/xxx
body_on: >
[{"id": "xxx","properties": {"Power": "1" }}]
body_off: >
[{"id": "xxx","properties": {"Power": "0"}}]
is_on_template: '{{ value_json[0].properties.Power == "1" }}'
method: POST
headers:
Content-Type: application/json
Authorization: '{{states("sensor.token_hisense_a")+states("sensor.token_hisense_b")}}'
verify_ssl: false
and this binary_sensor:
binary_sensor:
- platform: rest
resource: https://api.connectlife.io/api/v1/appliance/xxx
method: GET
name: Clima1BS
value_template: '{{ value_json[0].properties.Power == "1" }}'
headers:
Content-Type: application/json
Authorization: '{{states("sensor.token_hisense_a")+states("sensor.token_hisense_b")}}'
verify_ssl: false
The binary sensor works with no problem using
Authorization: '{{states("sensor.token_hisense_a")+states("sensor.token_hisense_b")}}'
The switch doesn’t work. It is shown as unavailble on HA. If I use
Authorization: Bearer xxxxxxxxx
it works.
Anyone knows how to fix it?
koying
(Chris B)
January 18, 2024, 9:10am
20
paolomorabito:
If I create this switch:
You miss “Bearer”, here. You miss it in the binary sensor as well, so not sure why it worked there, unless you fumbled somewhere
Anyway, I went to great length to test this with a setup as close as yours as possible, and it works for me.
Bearer
is included in states("sensor.token_hisense_a")
Anyway I found a solution. I created a template switch and used shell_command
to turn on/off. In the shell_command
I had no problem to include the states