So I was having the same issue trying to send an ssh command to restart another rasp pi I have. Here is what I found.
I think the answer is to issue the ssh key within the home assistant environment and also adjust the command to run as home assistant environment.
My Setup: Raspbian Buster and I have my home assistant running in it’s own environment. I am sorry if I use any terms incorrectly, I am new to Linux and don’t understand everything. Just wanted to share what I did to get mine to work.
Step one: Setting up SSH Key (I did this both on the homeassistant pi and in the environment)
ssh-keygen -t rsa
Then I left the RSA ID blank and pressed enter, left password blank and pressed enter, left confirm password blank and pressed enter.
I then copied the key to the machine that I watned to run the command on. Pi below is the username of the other machine.
ssh-copy-id [email protected]
Verify that the ssh connection works without prompt for password. You should now have an ssh connection to the other pc
ssh [email protected]
type exit to exit the ssh connection.
Now I do the same step above but under the home assistant user / environement
sudo -u homeassistant -H -s
ssh-keygen -t rsa
Enter, Enter, Enter past the ID name, Password, confirm password
Now copy the key over to the other machine you want to issue commands on. I also copied the key over to the PI where my home assistant is running. Not sure if I needed to but since it is running in an evironment I did any ways. The 10.0.0.128 is where homeassistant is installed and running.
ssh-copy-id [email protected]
ssh-copy-id [email protected]
exit
Now you should be signed in as the pi user and not the home assistant user
SSH Key’s are finished.
Add line to Configuration.yaml /home/homeassistant/.homeassistant
cd /home/homeassistant/.homeassistant
sudo nano configuration.yaml
Then add in the following line. I added it in at the bottom.
shell_command: !include shell_command.yaml
Save the Config
Now we need to create the shell_command.yaml file
sudo nano shell_command.yaml
I added the follow in the shell_command file
reboot_test: sudo -u homeassistant -H -s ssh [email protected] sudo reboot
Now we need to create the HA script that issues the command
sudo nano script.yaml
I added in the following
reboot_test:
alias: Reboot Test
sequence:
- Service: shell_command.reboot_test
Tested and it now works.
I think the key is to run the ssh command as the homeassistant user or in the homeassistant environment. Not sure what term is correct.