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
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"
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.
Getting this error now:
Error rendering command template: JSONDecodeError: Input must be bytes, bytearray, memoryview, or str: line 1 column 1 (char 0)
I got it to work. The whole command has to be listed in configuration.yaml for it to work. Is there any way to variable-ize the data so I only need one entry and can configure the data from an automation?
Yes that is exactly what this topic was about. Look above for the solution.
I’ve tried that without success.
Ok, figured it out. The {{input}}
needs single quotes around it AND the data entry also needs single quotes. Using {{input}}
was stripping the single quotes from the data entry for some reason, so I need it at both points.