Trying to get script working to shutdown remote pi

I have a pi4 running Volumio music player. Before I turn off the power switch (smart plug), I want to send a shutdown command to the pi to be kind to the SD card.

I’ve already set up my shared public/private key pair between HA and Volumio Pi and that works. I don’t need to enter any credentials to log into the pi from HA anymore.

I have added the following to my configuration.yaml file as a shell command.

#Shell Commands
shell_command:
  shutdown_volumio: ssh -l [email protected] "sudo shutdown now"

I then created a script I was hoping to fire from an Action in an automation.

alias: Shutdown Volumio
sequence:
  - service: shell_command.shutdown_volumio
    data: {}

The script appears to run when I test it directly from the script but nothing happens. The pi doesn’t shut down. I have rebooted both the pi and HA numerous times. It doesn’t look like there are any errors in the traces of the script that I can see.

  • Have I done anything obviously wrong?

I did something similar for a remote pi.

My config looks like this

shell_command:
  reboot_zero2pi: ssh -i /config/.ssh/id_rsa -o StrictHostKeyChecking=no -o UserKnownHostsFile=/config/.ssh/known_hosts [email protected] 'sudo reboot'
  shutdown_zero2pi: ssh -i /config/.ssh/id_rsa -o StrictHostKeyChecking=no -o UserKnownHostsFile=/config/.ssh/known_hosts [email protected] 'sudo poweroff'

Did you setup/test inside the HA container?

1 Like

You bloody genius. I was logged into my docker host machine and running all the ssh commands via VS code. I didn’t even think that it was the HA container that needed the keys setting up and needed access to the remote pi.
Thank you so much. I really had been going round and round in circles. I must have watched this guys youtube video 20 times which was really starting to grate on my nerves cos there was annoying background music too.

1 Like

As a slight aside, I also figured out my shell command was wrong it should ready

#Shell Commands
shell_command:
  shutdown_volumio: ssh -l volumio 192.168.1.77 'sudo shutdown -h now'

the -l indicates that you are providing the user then host with a space between. without the -l you can use user@host. Just in case anyone comes along to read this afterwards. -h means halt all services before shutdown. Not strictly necessary but best practice from what I’ve read.