How can I use curl from an automation?

I’m struggling to get my automation trigger to call a curl command. I’ve been trying and searching for hours, but I’m not getting it…

This works in my terminal:

curl --data ‘{“SystemON”:“on”}’ http://192.168.1.192/SystemON

but I can’t get it working through HA.

I have the following in my configuration file:

shell_command:
- AC_on: ‘curl -X POST -d {{ data }} {{ url }}’

and the following in my automations file:

action:

and I’m getting this config validation error:

Invalid config for [shell_command]: expected a dictionary for dictionary value @ data[‘shell_command’]. Got [OrderedDict([(‘AC_on’, ‘curl -X POST -d {{ data }} {{ url }}’)])]. (See /config/configuration.yaml, line 122). Please check the docs at https://home-assistant.io/components/shell_command/

I’ve tried not having the data template and just putting the url and data in the curl command, but some of the trouble may be from the curl command needing ’ and " quotes in it and it didn’t help. I’ve tried escaping them and this and that with no luck. Honestly, I’m using trial and error, but don’t even know if I’m on the right track because this is my first automation that isn’t created using the UI automation wizard.

Any help to set this up would be appreciated.

Thanks.

Please first format the lines from your configuration and automations files so we can read them properly. The easiest way to do that is to select the lines then hit the </> button. This is very important because YAML is very sensitive to indentation, and also without proper formatting certain characters (like quotes and dashes) get changed, as you can see by looking at your post above. Once we’re able to see your YAML code correctly we’ll be able to help.

Having said that, best I can tell is that you need to change your shell_command to:

shell_command:
  ac_on: "curl -X POST -d '{{ data }}' {{ url }}"

and the corresponding automation action should be:

action:
  - service: shell_command.ac_on
    data:
      url: 'http://192.168.1.192/SystemON'
      data: '{"SystemON":"on"}'

UPDATE: Wow, just saw this again. Must not have know what I was doing back then! :blush: Updated the data_template to data, since data_template isn’t required here.

4 Likes

Thanks, and sorry, I didn’t see how to format it, but new I could.

I’ve updated my code as you suggested. I no longer have the error in the config validation, but the curl command wasn’t doing what it does in the terminal… until I restarted Home Assistant!

Thanks for the help, its really appreciated.

1 Like

Sorry for bumping an old thread, but having some difficulty with this exact thing. I’m relatively new to Homeassistant but have had success so far, but I’m really stuck here. I have a projector that can use http requests for remote functions. The requests look something like this.

http://deviceip:port/controller?action=keyevent&keycode=menu

What I currently have is:

In my configuration yaml

shell_command:
  tv_remote: "curl -k '{{ data }}' {{ url }}"

and in the service data

data:
  url: 'http://deviceip:port/controller'
  data: '{"action":"keyevent"}{"keycode":"volumedown"}'

I can’t get it to run, properly and I’ve tried a bunch of variations including not using the data and url variables and just having it hardcoded in the configuration yaml. I’m really at a loss, because when I just copy the url into firefox it runs all of the commands just fine.

Not entirely sure, but should the data be:

data: '{"action":"keyevent", "keycode":"volumedown"}'

1 Like