Execute CURL Command from an Automation

I have a bunch of CURL commands I’d like to use in some automations. However, I can’t figure out how to get them to run from HA. Every topic I’ve seen on the forums is over 3 years old. Examples given on forum posts talk about adding the curl command to a switch, but current documentation say to make it a sensor. I tried using RESTful, too, but haven’t had any luck there, either.

- platform: rest
    resource: http://10.0.4.90/json/state
    method: POST
    payload: >-
      '{"on":true,"bri":5,"seg"...
- platform: command_line
  name: curl test
  command: >-
    'curl -X POST "http://10.0.4.90/json/state" -d '{"on":true,"bri":5,"seg"...

And this is another one :slight_smile:

If you want to keep your CURL commands maybe this Integration will help you:
https://www.home-assistant.io/integrations/shell_command/

I’ve tried to follow this, but keep getting errors like this:

Error rendering command template: JSONDecodeError: unexpected character: line 1 column 43 (char 42)
Error rendering command template: JSONDecodeError: unexpected character: line 1 column 1 (char 0)

I go to that character and it’s part of the curl command, so I don’t know what’s wrong. I can run the raw curl command from the CLI and it works fine.

without showing your Code, it is hard to guess…

I made examples in the OP, but here’s my newest iteration. I have this in my configuration.yaml:

shell_command:
  curl_test: > 
    curl -X POST "http://10.0.4.90/json/state" -d {{input|from_json}} -H "Content-Type: application/json"

and then I try and do this in Development Tools:

service: shell_command.curl_test
data:
  input: '{"on":true,"bri":5,"seg":{"id":0,"i":[0,28,[123,231,213]]}}'

and nothing happens.

This works perfectly fine from the command line within HA

➜  ~ curl -X POST "http://10.0.4.90/json/state" -d '{"on":true,"bri":5,"seg":{"id":0,"i":[0,28,[123,231,213]]}}' -H "Content-Type: application/json"
{"success":true}# 

Just a guess. Maybe “|from_json” is the problem. Did you try it without “|from_json” or try to put your input directly in the curl_test command?

I’ll try both. If I put the whole thing in the configuration.yaml entry, how would I call it?

without the data section

configuration.yaml entry:

shell_command:
  curl_test: > 
    curl -X POST "http://10.0.4.90/json/state" -d '{"on":true,"bri":5,"seg":{"id":0,"i":[0,28,[123,231,213]]}}' -H "Content-Type: application/json"

Developer Tools:


No error, but it doesn’t work. Same if I put it in an automation as an action and run it from there.

Wait, I got it. It works when the whole command is in configuration.yaml

Is there any way to variable-ize it so I only need one entry and can change up the data through an automation?

Glad to hear it works now!
Do you need whole json-statement to change or only parts of it?

Just the data, between the single quotes.

'{"on":true,"bri":5,"seg":{"id":0,"i":[0,28,[123,231,213]]}}'

OK, then we are back to your post #6 above.
if try this in Template editor:

{% set input= '{"on":true,"bri":5,"seg":{"id":0,"i":[0,28,[123,231,213]]}}' %}
{{ input }}    

I get this:

{"on":true,"bri":5,"seg":{"id":0,"i":[0,28,[123,231,213]]}}

which is witout single quotes.
Maybe this is the problem?
Try this to check:

shell_command:
  curl_test: > 
    curl -X POST "http://10.0.4.90/json/state" -d '{{input}}' -H "Content-Type: application/json"

Ok, and then to call it I do this?

shell_command.curl_test:
data:
  input: '{"on":true,"bri":5,"seg":{"id":0,"i":[0,28,[123,231,213]]}}'

That works! Ok, so I was losing the single quote because the input was stripping it, so I need it on the outside of both the curly brackets AND as part of the data entry in the automation…which I may make as input_booleans.

Hello. May I see how you have done it in config yaml. I can get this to work in terminal and need help.

curl -X POST https://shelly-85-eu.shelly.cloud/device/status -d “id=b8d61a855cf0&auth_key=MWRmZGUzdWlk89931ACF8A25FB279F2C51675379D12D2F66E28D49798920EXXB7BEC14AB5620D0C5DD30A590DDC8”

result:
{“isok”:true,“data”:{“online”:true,“device_status”:{“code”:“SNSW-001X16EU”,“mqtt”:{“connected”:true},“_updated”:" 2024-01-12 07:37:53",“v_eve:0”:{“ev”:“”,“ttl”:-1,“id”:0},“temperature:100”:{“id” :100,“tC”:21.62,“tF”:70.93,“errors”:},“ws”:{“connected”:false},“addons”:[“temperature:100”,“temperature:101 “],“ble”:,“sys”:{“mac”:“B8D61A855CF0”,“restart_required”:false,“time”:“10:00”,“unixtime”:1703494858,“uptime”:4 ,“ram_size”:245996,“ram_free”:140504,“fs_size”:458752,“fs_free”:147456,“cfg_rev”:20,“kvs_rev”:0,“schedule_rev”:0,“webhook_rev”:0,” available_updates”:,“reset_reason”:3},“id”:“b8d61a855cf0”,“wifi”:{“sta_ip”:“192.168.1.85”,“status”:“got ip”,“ssid”:" ORBI98",“rssi”:-55},“temperature:101”:{“id”:101,“tC”:23.31,“tF”:73.96,“errors”:},“serial”:1705045073.29, “input:0”:{“id”:0,“state”:false},“cloud”:{“connected”:true},“switch:0”:{“id”:0,“source”:" init",“output”:true,“temperature”:{“tC”:61.8,“tF”:143.2}}}}}%

I’m new to the forum, but would like to share my 2 cents:

I had a similar issue with my (less than €10) Sonoff smartswitches. In PC-Active 299 (april/may 2018) Adri Wischmann describes how these devices can be modified into a smartswitch with ESPEasy firmware.

After having tried several possibilities in HA to control these, I ended up with this solution:
In the configuration.yaml I added the lines:

shell_command:
  sonoff1_aan: curl http://your ip/control?cmd=gpio,12,1
  sonoff1_uit: curl http://your ip/control?cmd=gpio,12,0

In the automation you can use the above Shell commands for actions.
No integrations needed!