Rest sensor: Templating in header values

I’m trying to include entity ids values as part of headers in a rest sensor.

  - platform: rest
    name: termo_power_rest
    method: GET
    scan_interval: 5
      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!

headers does not accept templates:

1 Like

Ciao, I was afraid of receiving that answer… Any workaround?

Using sensors for key-id / tokens / … seems rather strange.
Any reason to not use the secrets mechanism of HA? Storing secrets - Home Assistant (

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.

I tried this but I’m not sure why it’s not succeeding validation:

  - platform: command_line
    command: 'curl --request GET "" --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


As a first, check your double quotes. They don’t seem right.

1 Like

I’m getting closer, but I’m not there yet. I get as answer this:


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 ¿?


{% 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.

1 Like

Paste you complete command_line sensor as it is now. The prior version definitely had some quoting issues.

1 Like

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: >
      --request GET
      --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 :slight_smile:

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