kim123
August 10, 2021, 8:59am
1
I am using rest platform to collect some data from my electric vehicle charger, via an api. I need to refresh my token every 24h, so i’ve created a own sensor to collect the token.
But i cant get it to work with the rest platform.
- platform: rest
resource: https://api.xxxxxxxxxxxxx/
name: ElectricVehicleCharger
scan_interval: 60
value_template: '1' #Avoid 255 err msg
headers:
authorization: !secret ElectricVehicleCharger_header_token
#authorization: "{{ states_attr('sensor.ElectricVehicleCharger_token', 'accessToken' }}"
I would like to use the comment out line, but it just throws me an error. How could I solve this? thanks
"{{ state_attr('sensor.electricvehiclecharger_token', 'accesstoken' }}"
|
|
^
should be state_attr, not states_attr
Recheck the entity_id
as well as the state attribute
to make sure it is identical to the source you are taking the data from. (Check from Developer Tools —> States (tab) —> Search your entity)
1 Like
kim123
August 10, 2021, 9:51am
3
That was a little bummer… it should be ok now, but still gets unautorized. Checked it out in templateeditor in developer tools, and for what I can see everything looks good.
- platform: rest
resource: https://api.XXX
method: POST
name: ElectricVehicleCharger_token
headers:
Content-Type: application/json
payload: '{ "grantType" : "password" , "username" : "+XXX" , "password" : "XXXX" }'
value_template: 'OK' # dummy value, avoid 255 err msg
json_attributes:
- accessToken
- refreshToken
scan_interval: 1800
- platform: rest
resource: https://api.easee.cloud/api/chargers/XXXXX/state
name: ElectricVehicleCharger
scan_interval: 60
value_template: 'OK' # dummy value, avoid 255 err msg
headers:
#authorization: !secret xxxx
authorization: Bearer {{ state_attr('sensor.ElectricVehicleCharger_token', 'accessToken') }}
json_attributes:
- smartCharging
- cableLocked
- chargerOpMode
- totalPower
- sessionEnergy
- energyPerHour
koying
(Chris B)
August 10, 2021, 10:24am
4
Unfortunately, headers:
do not accept templates…
There are at least 3 feature requests for this.
In the “meantime”, the workaround is to use a command_line sensor.
See, e.g:
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: cor…
kim123
August 10, 2021, 4:12pm
5
Perfect. That was the trick to get it working. Thank you.
koying
(Chris B)
August 10, 2021, 7:20pm
6
FWIW, I created a PR that implements template support for headers
home-assistant:dev
← koying:addrestheaderstemplate
opened 07:08PM - 10 Aug 21 UTC
kim123
August 10, 2021, 7:34pm
7
Thats perfect. I hope it will be merged into core asap