hybrix
(Gonzalo)
June 13, 2021, 9:04am
1
Hi,
I’m trying to include entity ids values as part of headers in a rest sensor.
- platform: rest
resource: https://openapi.tuyaeu.com/v1.0/devices/xxxxxxxxxxx/status
name: termo_power_rest
method: GET
scan_interval: 5
headers:
sign_method: HMAC-SHA256
client_id: sensor.client_id
t: sensor.tuya_t
mode: cors
Content-Type: application/json
sign: sensor.tuya_sign
access_token: sensor.tuya_accesstoken
I tried templating like this the values without success:
{{ states('sensor.tuya_sign') }}
Can anybody help figuring how to make it work?
Thanks in advance!
tom_l
June 13, 2021, 9:27am
2
headers
does not accept templates:
1 Like
hybrix
(Gonzalo)
June 13, 2021, 10:36am
3
Ciao, I was afraid of receiving that answer… Any workaround?
Thanks
koying
(Chris B)
June 13, 2021, 10:56am
4
Using sensors for key-id / tokens / … seems rather strange.
Any reason to not use the secrets mechanism of HA? Storing secrets - Home Assistant (home-assistant.io)
hybrix
(Gonzalo)
June 13, 2021, 12:09pm
5
Because they are dinamic. Token, sign and T change every hour.
tom_l
June 13, 2021, 12:35pm
7
Not for a sensor. Fort that they will need a command line sensor:
The docs for this need updating. The command does accept a template.
Here’s an example command I used to use that uses curl to supply the headers:
kodi_input_back: 'curl -u "user:passwd" --header "Content-Type: application/json" --data "{\"jsonrpc\":\"2.0\",\"method\":\"Input.Back\"}" "http://ipaddress:8080/jsonrpc"'
2 Likes
koying
(Chris B)
June 13, 2021, 12:42pm
8
Arf, yes. I always make the confusion between the sensor and the service.
hybrix
(Gonzalo)
June 13, 2021, 1:01pm
9
I will check this doc later. Fyi My purpose is the following one:
I need to monitor power of a switch every 5secs. The request I was trying to run with the Rest sensor returns the power, but in order to make that request is necessary to have an updated token (expires in 1h), which is obtained from a prior request.
Thanks
hybrix
(Gonzalo)
June 13, 2021, 3:29pm
10
I tried this but I’m not sure why it’s not succeeding validation:
- platform: command_line
command: 'curl --request GET "https://openapi.tuyaeu.com/v1.0/devices/xxxxxxx/status" --header "sign_method: HMAC-SHA256" --header "client_id: "xxxxxxx"" --header "t:"{{ states('sensor.tuya_t') }}"" --header "mode: cors" --header "Content-Type: application/json" --header "sign:"{{ states('sensor.tuya_sign') }}"" --header "access_token: "{{ states('sensor.tuya_acesstoken') }}"'
name: termo_power_command_line
Thanks
koying
(Chris B)
June 13, 2021, 3:44pm
11
As a first, check your double quotes. They don’t seem right.
1 Like
hybrix
(Gonzalo)
June 14, 2021, 10:53am
12
I’m getting closer, but I’m not there yet. I get as answer this:
{"result":[{"code":"switch","value":true},{"code":"countdown_1","value":0},{"code":"cur_current","value":0},{"code":"cur_power","value":333},{"code":"cur_voltage","value":2319}],"success":true,"t":1623667742601}
I’m trying to get the value 333 of the ocurrence with code = cur_power. {“code”:“cur_power”,"value":333 }. I’ve tried to add this without success:
value_template: >
{% set items = value_json['result'] | selectattr('code','eq','cur_power') | list | first %}
{{ items['value'] }}
It’s erroring because it’s says there’s no attribute called result ¿?
koying
(Chris B)
June 14, 2021, 11:07am
13
Tested
{% set value_json = {"result":[{"code":"switch","value":true},{"code":"countdown_1","value":0},{"code":"cur_current","value":0},{"code":"cur_power","value":333},{"code":"cur_voltage","value":2319}],"success":true,"t":1623667742601} %}
{% set items = value_json['result'] | selectattr('code','eq','cur_power') | list | first %}
{{ items['value'] }}
in the template debugger and it works, so likely the actual output is not what you show
For debugging, maybe just put
{{ value_json | truncate(255, True, '') }}
as the template, to view the the actual output returned to HA.
EDIT: Added truncate to avoid the value being larger than 255 chars
2 Likes
hybrix
(Gonzalo)
June 14, 2021, 11:32am
14
I copypasted it:
Also I tried to show it in the templates developer tools:
I also tried to copy paste the value of MY SENSOR inside your set value_json and it works there, but for some reason doesn’t work in the command line sensor. Quite weird.
koying
(Chris B)
June 14, 2021, 12:44pm
15
hybrix:
Quite weird
Yeah, puzzling.
Tested basically the same with a random REST api and it works.
1 Like
Troon
(Troon)
June 14, 2021, 12:51pm
16
Paste you complete command_line
sensor as it is now. The prior version definitely had some quoting issues.
1 Like
hybrix
(Gonzalo)
June 14, 2021, 1:19pm
17
You both are right, there should be a typo, as I copy pasted again the code from the templates view and now it’s working. Thanks!!
For future reference, I paste below the complete solution:
- platform: command_line
command: >
curl
--request GET
"https://openapi.tuyaeu.com/v1.0/devices/46560787f4cfa2f8465b/status"
--header "sign_method: HMAC-SHA256"
--header "client_id: "xxxxxxxxxx""
--header "t:"{{ states('sensor.tuya_t') }}""
--header "mode: cors"
--header "Content-Type: application/json"
--header "sign:"{{ states('sensor.tuya_sign') }}""
--header "access_token: "{{ states('sensor.tuya_accesstoken') }}""
value_template: >
{% set items = value_json['result'] | selectattr('code','eq','cur_power') | list | first %}
{{ items['value']|multiply(0.1)|int }}
name: aire_despacho_power_cmd
scan_interval: 5
2 Likes
Troon
(Troon)
June 14, 2021, 1:40pm
18
I still think your quoting is wrong in places, these lines specifically need the double-quotes escaping:
--header "client_id: \"xxxxxxxxxx\""
--header "t:\"{{ states('sensor.tuya_t') }}\""
--header "sign:\"{{ states('sensor.tuya_sign') }}\""
--header "access_token: \"{{ states('sensor.tuya_accesstoken') }}\""
If it works without these changes, I don’t understand how…
hybrix
(Gonzalo)
June 14, 2021, 1:59pm
20
I actually copy pasted the format from another user in another post. It worked for him, so it worked for me
JebNZ
(Evan Beitz)
May 12, 2023, 9:33am
21
Hey man! I know this is a long time ago but I was wondering where you got the values for states(‘sensor.tuya_t’) and the others?
Gautch
May 17, 2023, 1:59am
22
Hey everyone
I had a similar issue and explained the resolution process here
Hi everyone,
I am currently facing an issue while trying to retrieve my data through the API using Rest.
The process involves two steps. First, I use my API key to obtain the bearer token, and then I use that bearer token to access the desired data. So far, the initial step is functioning correctly.
For my second request, I am able to utilize my secret file for the header section, but not a sensor value.
My secret file
api_key: "ABC123"
api_key_bearer_static: "Bearer DEF456"
This works
re…