Adding Shell commands

I am new to HA. How do I add a Shell command that can be called as part of an automation? Do I merely add

shell_command:
   test_script: some shell command

to the configruation.yaml file. If so, it is not working, so I must be missing a step.

Nope that’s it. If it’s not working it’s probably the command itself. Well should be indented 2 spaces not 3. Post the command if it’s still not working.

I changed it to 2 indented spaces and restarted HA. I look at Deveoper Tools->Services but do not see anything added. I presume I should see Shell _command or test_script but I don’t.

OK. I got it to show up. Is it not possible to have the floowing as a shell command:

test: ssh ipaddress command

It’s not going to work or work for long. There is a certain way to set up commands, that’s why I asked you to post the command. Anyway follow this guide.

I added the following to the configuration.yaml file:

shell_command:
   test_script: ssh ipaddress /usr/local/bin/play /Users/ihf/Desktop/test.mp3

This works in so far as showing up as a service. However, if I try to use it in an action, I get:

Logger: homeassistant.components.shell_command
Source: /usr/src/homeassistant/homeassistant/components/shell_command/__init__.py:114
Integration: Shell Command (documentation, issues)
First occurred: 12:25:02 PM (4 occurrences)
Last logged: 12:29:15 PM

Error running command: `ssh ipaddress /usr/local/bin/play /Users/ihf/Desktop/test.mp3`, return code: 255
NoneType: None

If I run this command from the SSH&Terminal addin under HA it works fine. the known_hosts file is in /root/.ssh (perhaps that is the problem?)

P.S. I added the parameter -o UserKnownHostsFile=/root/.ssh/known_hosts and it works fine from terminal in HA but not as an action.

The problem is likely related to authentication:

Command executed: ssh -o UserKnownHostsFile=/root/.ssh/known_hosts -i /root/.ssh/id_rsa user@ip /usr/local/bin/play /Users/ihf/Desktop/test.mp3: 255
Warning: Identity file /root/.ssh/id_rsa not accessible: No such file or directory.
user@ip: Permission denied (publickey,keyboard-interactive).

the id_rsa file is in /root/.ssh so not sure what that error means.

As the linked guide says, this doesn’t mean anything. The SSH&Terminal addon is a different docker container with a different file system and a different /root folder. Being able to run your command from the terminal this addon provides doesn’t tell you any information about whether it will work when run as a shell command from HA. Please read the guide Mikefila linked, it covers how to get set up here in great detail.

1 Like