Remote control Raspberry Pi with shell_command and SSH

Hi everyone! I’m struggeling with remote controlling a raspberry pi from home assistant with ssh.

shell_command:
7inchpi_brightness: ssh 7inchpi.xxx.xxx sudo sh -c ‘echo “{{ brightness }}” > /sys/class/backlight/rpi_backlight/brightness’

  1. I run ssh-keygen on Home Assistant (connected as root with ssh), passphrase empty
  2. I add the generated public key to the remote pi (home/pi/.ssh/authorized_keys

What am I missing? Even for ‘ssh [email protected] sudo reboot’ I get an Error “255” - whatever that means. Any guru out there who see’s my problem?

here is one of my commands i use from my shell cmds
speak: /usr/bin/ssh -i /home/hass/.ssh/id_rsa [email protected] ‘/home/hass/speak.sh “”{{phrase}}""’

I have a PI in the kitchen that is connected to a speaker and it makes announcements as needed.

first step in troubleshooting would be to make sure you can execute the command from the CLI and verify it works without issue.

Thank you very much! I didn’t realize, that I have to send the private key as parameter. I really appreciate that.

Best regards

Well, it works in the CLI, but not as service - still get the Error 255. Have you done additional work on the kitchen side?

There’s a community guide for SSH:

Your problems probably stem from the fact that the shell_command runs inside the Home Assistant container while you are testing your commands outside of it.

@ondras12345 not everyone runs HA in a container or supervised. I run mine on debian in a virtual environment.

@Presswerk65 if it works via cli then you are half way there. I would next confirm what user is starting/running home assistant. If you are indeed running in a container or supervised, you may have some other sort of issue. I can supervise my own system, so I don’t have any experience with that setup. my other thoughts are to check permissions on your .ssh directory and key files.
I run HA and node-red as root on my machine so it has permissions to access and run shell scripts in my home assistant directory (/home/hass/) two more things I would suggest, (depending on your setup) look at the HA logs or tail -f /var/log/syslog while you fire off the cmd from HA and see what it says while you are doing that.

The problem was file permissions, now everything runs - thank you all for your advises!

1 Like