Passing sensor state on a command_line sensor

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

(removed, next time I’ll read the docs myself first!)

Both solutions don’t work

Templates are not supported in command_line switch (at least, not there)

Why not using a REST switch?

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

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

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

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.

What a pity…

This example in the Rest documentation suggests otherwise:

2 Likes

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 :+1:

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!

Looks exactly the same as you “did not work” above (besides the YAML continuations) :wink:

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?

You miss “Bearer”, here. You miss it in the binary sensor as well, so not sure why it worked there, unless you fumbled somewhere :wink:

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 :man_shrugging: