How to send different http commands from within automation

Hi , We built a simple esp32 http based dashboard that except http://IP/get?input1=infodata so the infodata can be text and it works fine from browser and cmd line e.g curl http://IP/get?input1=“HiThere”

shell_command:
  send_alert816: "/usr/bin/curl -X GET http://IP/get?input1=CheckMe"
  clear_alert816: "/usr/bin/curl -X GET http://IP/get?input1="

I am trying to send dynamic infodata , i added a few shell command and they work but can not figure
out how to pass the infodata separate from the command without hard coding the command in the configuration.yaml , not sure that’s curl related or HA but any hits will be appreciated . I am assuming there is a simple way to send different http commands from within automation . Not also sure if REST API is any different then shell commands

Thanks

shell_command:
  send_alert816: /usr/bin/curl -X GET http://IP/get?input1={{input}}

Use it like this

action:
  - service: shell_command.send_alert816
    data:
      input: 'something to send here'

That worked thank you

8x16TM1640_ha

version 2
Uses MQTT now and support sound with text to speech , works great with HA

Hi, I’m trying to replicate this, but have been unsuccessful. I added this to my configuration.yaml:

shell_command:
  curl_test: >-
    /usr/bin/curl -X POST "http://10.0.4.90/json/state" -d input1={{input}}

and then tried to enter the data into an action like this:

action:
  - service: shell_command.curl_test
    data:
      input: >-
        ''{"on":true,"bri":5,"seg":{"id":0,"i":[0,7,'00d9ff']}}' -H "Content-Type: application/json"'

and nothing is working. Tried several different permutations.

Try this:

shell_command:
  curl_test: >
    /usr/bin/curl -X POST "http://10.0.4.90/json/state" -d input1={{input|from_json}}

See: https://www.home-assistant.io/docs/configuration/templating/#tofrom-json

Ok, did:

shell_command:
  curl_test: >
    /usr/bin/curl -X POST "http://10.0.4.90/json/state" -d input1={{input|from_json}}

and then

service: shell_command.curl_test
data:
  input: '{"on":true,"bri":5,"seg":{"id":0,"i":[0,7,'00d9ff']}}' -H "Content-Type: application/json"

in developer tools. No error, but nothing happened.

What exactly is the format of the message you are trying to send once {{input}} has been substituted?

What does the API expect?

You probably need to drop this from {{input}]. -H "Content-Type: application/json"' and include it in the shell command instead.

Like this?

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

I’m trying to initiate a curl command using an HA automation. It works perfectly fine from the command line. Here’s an example:

curl -X POST "http://10.0.4.90/json/state" -d '{"on":true,"bri":5,"seg":{"id":0,"i":[0,18,'FF5910']}}' -H "Content-Type: application/json"
shell_command:
  curl_test: >
    /usr/bin/curl -X POST "http://10.0.4.90/json/state" -d {{input|from_json}} -H "Content-Type: application/json"

Copy and pasted the modified code into configuraton.yaml and restarted.

Loaded automation and tried to run this:

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

Nothing.

I ssh’d into HA and ran this from the command line manually:

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

and it worked fine.

See Mike’s advice here:

That’s a bit over my head. I’ll have to research how to do that.

thank you.

Add this to your configuration.yaml file:

logger:
  default: info
  logs:
    homeassistant.components.shell_command: debug

You will then see a lot more information about how your command behaves in System → Logs.

Logger: homeassistant.components.shell_command
Source: helpers/template.py:425
Integration: Shell Command (documentation, issues)
First occurred: 12:41:11 AM (2 occurrences)
Last logged: 12:42:57 AM

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)
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 423, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1950, in _render_with_context
    return template.render(**kwargs)
  File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1854, in from_json
    return json_loads(value)
orjson.JSONDecodeError: unexpected character: line 1 column 43 (char 42)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/shell_command/__init__.py", line 51, in async_service_handler
    rendered_args = args_compiled.async_render(
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 425, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: JSONDecodeError: unexpected character: line 1 column 43 (char 42)

Because this is broken up into two different sections I don’t know if this refers to the line in configuration.yaml or in Developer Tools.

EDIT: Fixed the “column 43” error, was missing input1=

I deliberately left that out because you did not use it in the working command here:

I figured input1= was a variable for the shell_command so it wouldn’t make sense to have it in a raw command.

No, there is nothing like that in the documentation.

What you send is what you send.