How to shutdown raspberry from HA (Raspbian)

hi!

Someone knows how to switch off the raspberry from Home Assistant witch a button on Home or some else?

create a shell_command with sudo shutdown now as a command and then create a script that will call this shell_command.
you will need to ensure user that HA runs under has rights to run this command without a password.
You can use the NOPASSWD directive in your /etc/sudoers file.

If your user is called user and your host is called host you could add these lines to /etc/sudoers :

user host = (root) NOPASSWD: /sbin/shutdown
user host = (root) NOPASSWD: /sbin/reboot

This will allow the user user to run the desired commands on host without entering a password. All other sudo ed commands will still require a password.

1 Like

Hmmm not sure i understand clearly.
Username is xxxx and host is 192.168.1.29.
What to add in sudoers file?

Thanks!!! That it’ll work!! :smiley:

1 Like

Translated to raspbian with Home Asisstant that’s:

  1. First run “sudo visudo” for edit the /etc/sudoers file.
  2. If our user for home assistant is homeassistant write under root ALL=(ALL:ALL) ALL:
    homeassistant ALL=(root) NOPASSWD: /sbin/shutdown
    homeassistant ALL=(root) NOPASSWD: /sbin/reboot:

That means that for the user homeassisant in ALL hosts, with the root permissions and without prompt password it can run that commands.

Hello all,

Thanks for the great tip regarding how to setup a script to shutdown a RPi from HA. Unfortunately it doesn’t seem to be working for me. I’ve updated the /etc/sudoers file and I can confirm that from the command line I can run sudo for the homeassistant user and I’m not prompted for a password. I’ve also setup the shell_command with sudo shutdown now and a script that calls this. But every time I run this script, I get the following error:

Logger: homeassistant.components.shell_command
Source: /usr/src/homeassistant/homeassistant/components/shell_command/__init__.py:115 
Integration: Shell Command (documentation, issues) 
First occurred: 07:17:39 (1 occurrences) 
Last logged: 07:17:39

Error running command: `sudo shutdown now`, return code: 127
NoneType: None

The main difference I have, which may be different from you all, is that I’m running HA using docker and when I check top it seems to indicate that HA is running under the root user. But if I change the shell_command to use /usr/sbin/shutdown -H instead it still returns the same error as per above. Any suggestions regarding how I can get this working? Many thanks.

Just for the record, I ended up following the forum topic here to get it working. In short, as I’m running HA on a docker instance, I needed to ssh into the host machine to run the shutdown command.