Shell Command not working in Automation

I am trying to assure a Sony Bravia TV is using its speakers rather than audio system prior to starting an automation action. The REST example came from: https://github.com/home-assistant/core/issues/88532 which was used to create a shell command that was added to configuration.yaml as:

#Exposes shell_command,sonytv_speakers shell_command: sonytv_speakers: curl -X POST \ http://xxx.xx.xx.xxx/sony/audio \ -H 'cache-control:no-cache' \ -H 'x-auth-psk:xxxx' \ -d '{"method":"setSoundSettings", "id":5, "params":[{"settings":[{"value":"speaker", "target":"outputTerminal"}]}], "version":"1.1"}'

The command works when entered directly in the terminal and there are no errors in the configuration.yaml file. (“xxxxxxxx” used in lieu of actual IP and PSK.)

However when envoked as a service call in an automation, for example:

action: shell_command.sonytv_speakers data: {}

nothing happens and the log error is:

``Logger: homeassistant.components.shell_command
Source: /usr/src/homeassistant/homeassistant/components/shell_command/init.py:129
integration: Shell Command (documentation, issues)
First occurred: 5:30:04 PM (1 occurrences)
Last logged: 5:30:04 PM

Error running command: curl -X POST \ http://xxx.xx.xx.xxx/sony/audio \ -H 'cache-control:no-cache' \ -H 'x-auth-psk:xxxx' \ -d '{"method":"setSoundSettings", "id":5, "params":[{"settings":[{"value":"speaker", "target":"outputTerminal"}]}], "version":"1.1"}', return code: 3
NoneType: None``

I would appreciate suggestions to make the automation work as planned.

If the return code from curl was 3, supposedly that means the URL was malformed. Check that you’re properly escaping your command line in YAML maybe?

It is not clear what you are doing from your poorly formatted post. But…

You can’t define shell commands directly in your automation.

You need to save the shell command first using this:

Then call that shell command in your automation.

Please take note of this:

There’s a community guide that may help if this is your issue: SSH'ing from a command line sensor or shell command

Sometimes the version of the command is not what you’re expecting. I have often had to create a shell command just to find the version of the command (e.g., which version of curl you have installed) so I can make sure it supports what I’m doing. If you’ve got simple curl commands working but complex ones don’t, check the version!

Thank you for your response and sorry I was not clear about my goal. I did follow the Shell Command integration instructions by first placing the shell_command in configuration.yaml and then calling it in the automation.

Upon reading the command guide you provided I think it provides an explanation how my command can work in a terminal but not through HAOS.

1 Like