#Passing a Sensor Value to a CURL Command in Configuration.yaml

8 messages · Page 1 of 1 (latest)

restive hill
#

I have a created a sensor in configuration.yaml that passed my information to TP Link to get my token to interact with devices. The token is stored as "TP Link Token".

I understand I need to use some variation of {{ states(sensor.tp_link_token) }} to pass token to the CURL command. I am messing something up with the formatting of the command. Below is what I have, and I need to replace TOKEN with the sensor value. When I manually set TOKEN to the value it works perfectly in HA, but after trying about a dozen times to get the syntax right, I haven't been able to pull the value in from sensor. The examples I found wrap it in quotes, but that did not seem to work for me, as it would pass literally "{{ states(sensor.tp_link_token) }}" or "{{ states['sensor.tp_link_token'] }}" as the token rather than the value.

command_line:
    - switch:
        name: Remote Switch
        command_on: curl --request POST https://use1-wap.tplinkcloud.com/?token=TOKEN --data '{"method":"passthrough","params":{"deviceId":"DEVICE_ID","requestData":"{\"system\":{\"set_relay_state\":{\"state\":1}}}"}}' --header "Content-Type:application/json"
        command_off: curl --request POST https://use1-wap.tplinkcloud.com/?token=TOKEN --data '{"method":"passthrough","params":{"deviceId":"DEVICE_ID","requestData":"{\"system\":{\"set_relay_state\":{\"state\":0}}}"}}' --header "Content-Type:application/json"
        command_state: curl -s --request POST https://wap.tplinkcloud.com/?token=TOKEN --data '{"method":"passthrough","params":{"deviceId":"DEVICE_ID","requestData":"{\"system\":{\"get_sysinfo\":null},\"emeter\":{\"get_realtime\":null}}"}}' --header "Content-Type:application/json"
        value_template: "{{ (value_json.result.responseData.split('relay_state\":')[1][0]) == '1' }}"

hybrid muralBOT
#

To format your text as code, enter three backticks on the first line, press Enter for a new line, paste your code, press Enter again for another new line, and lastly three more backticks.
```yaml
example: here
```
Don't forget you can edit your post rather than repeatedly posting the same thing.

restive hill
#

Just bumping this. I have had no luck. I tried ChatGPT just to see if caught anything. It suggested this code, which doesn't work at all (I can't even see the Remote Switch Entity).

switch:
  - platform: command_line
    name: Remote Switch
    switches:
      remote_switch:
        command_on: >
          curl --request POST https://use1-wap.tplinkcloud.com/?token={{ states('sensor.tp_link_token') }} \
          --data '{"method":"passthrough","params":{"deviceId":"800696C702E279245B306558739D92E721076624","requestData":"{\"system\":{\"set_relay_state\":{\"state\":1}}}"}}' \
          --header "Content-Type:application/json"
        command_off: >
          curl --request POST https://use1-wap.tplinkcloud.com/?token={{ states('sensor.tp_link_token') }} \
          --data '{"method":"passthrough","params":{"deviceId":"800696C702E279245B306558739D92E721076624","requestData":"{\"system\":{\"set_relay_state\":{\"state\":0}}}"}}' \
          --header "Content-Type:application/json"
        command_state: >
          curl -s --request POST https://wap.tplinkcloud.com/?token={{ states('sensor.tp_link_token') }} \
          --data '{"method":"passthrough","params":{"deviceId":"800696C702E279245B306558739D92E721076624","requestData":"{\"system\":{\"get_sysinfo\":null},\"emeter\":{\"get_realtime\":null}}"}}' \
          --header "Content-Type:application/json"
        value_template: "{{ value_json.result.responseData.split('relay_state\":')[1][0] == '1' }}"

placid iris
#

ChatGPT regularly gives invalid and/or made up answers, of which this is a good example.

https://www.home-assistant.io/integrations/command_line/#switch: command_line switches don't support templates, which is why you observed the literal value being used as opposed the actual value of the template.

One option could be to use a template switch https://www.home-assistant.io/integrations/switch.template/ along w/ rest/shell commands (https://www.home-assistant.io/integrations/rest_command/ / https://www.home-assistant.io/integrations/shell_command/)

#

There is a native TP Link Smart Home integration though, too

restive hill
#

Thanks @placid iris I am using the native TP Link Smart Home Integration https://www.home-assistant.io/integrations/tplink

The issue is that only works locally for switches on the same network at HA. I have one switch that is not on the same network. I can easily control it with the API calls. I'm just trying to get that setup within HA.

The previous code worked, it would show the switch and the status, and I could toggle it. I just simply can't figure how to get the variable from the sensor to be loaded into the CURL command. If I put in the token by hand it works, but to automate it I just need to read the token from the sensor rather than hand edit the configuration.yaml every time the token expires.

placid iris
#

Yeah, and that's why you can't use the command_line switch for that situation. There's no way to use a template in the commands, so if they're static, it'll work fine.

restive hill
#

Ah, do you have any recommendations for another way? I'm happy to try something else if you know a starting point for me to look at next.