Ciao, I was afraid of receiving that answer… Any workaround?
Thanks
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)
Because they are dinamic. Token, sign and T change every hour.
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"'
Arf, yes. I always make the confusion between the sensor and the service.
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
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
As a first, check your double quotes. They don’t seem right.
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 ¿?
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
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.
Yeah, puzzling.
Tested basically the same with a random REST api and it works.
Paste you complete command_line
sensor as it is now. The prior version definitely had some quoting issues.
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
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…
I actually copy pasted the format from another user in another post. It worked for him, so it worked for me
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?
Hey everyone
I had a similar issue and explained the resolution process here
Hey! Ik it is an old post but I was wondering if you could tell me how you got the sign and access token for tuya into hone assistant? I would appreciate it
Hi,
Can you tell me how I can configure / script : 1. sensor.tuya_t, 2. sensor.tuya_sign and 3. sensor.tuya_accesstoken in home assistant?
I’m familiar with the Tuya IoT Platform site.
Thanks in advance. Frans