Reboot Pi with script

That should work. I don’t really see a use case for this in my instance though as the only real reason I reboot the Pi is if I am having a major issue with HA. In that case I wouldn’t have access to the UI to begin with - but that’s my case.

I’m interested in what your use case for this may be seeing as how I may have overlooked something.

I think I have only used it a couple of times, after a few installations to support the DHT sensor or maybe the PIR sensor, both are directly attached to the PI.

Well my use case is that i’m still installing and configuring a lot of stuff on my Pi and i need to reboot kind of often , in fact is was also a test to see what i could do with the shell_command component.
For me as of now i can’t run anything from the command line but i can start any of the home assistant services without issue

I tried a lot of combination including the one proposed above.
here are the two script confgured

   reboot_pi:
     alias: Reboot Raspi 3
     sequence:
       - alias: Reboot Raspi 3
         service: shell_command.pi

   reboot_ha:
     alias: Restart Home Assistant
     sequence:
        service: homeassistant.restart

The home assistant restart works well , the other don’t
when i’m lauching the PI one nothing happens and here.s what i have in the logs

16-10-28 22:53:44 homeassistant.core: Bus:Handling <Event call_service[L]: service=pi, service_data=, service_call_id=1977528400-4, domain=shell_command>
16-10-28 22:53:44 homeassistant.core: Bus:Handling <Event service_executed[L]: service_call_id=1977528400-1>
16-10-28 22:53:44 homeassistant.core: Bus:Handling <Event service_executed[L]: service_call_id=1977528400-4>
16-10-28 22:53:44 homeassistant.core: Bus:Handling <Event service_executed[L]: service_call_id=1977528400-3>

then even if i click on it again ( activate ) nothings happen , i presume that there might be a bug with sending OS command from HS with shell_command

anyways, thanks for the help guys , what i wanted to do as of now is not that important , so i’ll jump onto my next thing :slight_smile:

Makes sense - I don’t have any other h/w attached to the Pi as of yet. Thanks for replying.

I cant understand for the life of me why this command wont work…I am using it as stated above
shell_command:
pi: “sudo reboot”

If i enter it from the putty it reboots

By default I don’t think user Hass has sudo permissions. If you ssh in you are likely logged in as user pi. Try switching to user hass and it likely won’t work through ssh either.

You can give hass sudo permission but I don’t recall the steps to do it. A bit of Google fu will get you there.

1 Like

$ sudo adduser hass sudo

and then a reboot. “hass” being the user created by the AIO installer.

Seems to do it, hass shows as being in the sudo group

pi@HARP3:~ $ groups hass
hass : hass dialout sudo

but still doesn’t seem to let hass execute shell commands from within Hass. I should mention that in looking up how to do this there were all sorts of warnings to NEVER do this but I do not really understand how permissions work and the dangers.

After testing you can remove hass from sudo group by

$ sudo deluser hass sudo

1 Like

Keep in mind hass likely still has a password for sudo use. You would need to remove that too or add it into the shell command.

1 Like

THx for the replies, but for the life of me i cant find out what the hass password is, i installed HA with AIO script.
Does anyone know?

I’m using the AIO install and have the same problem which at least makes me feel less like I’ve broken something. I’m going to try a fresh install on a spare SD card and see if that lets me run a shell command when I have some time.

Not helpful sorry, but in my searching I found this post which says the hass account has no password.

There is no password for hass from what I have read. There is a way to not prompt for the password when sudo is used. Pi has this permission by default and hass can be given the same permissions.

@mefistofelis Rather than do something that breaks security on the pi, try something different. You can execute commands remotely using a script. On Mac or Linux you can use a key based ssh login to remotely run a script. On Windows, I think you can do the same with plink (part of the Putty package). A double click on shortcut on your desktop could do the reboot.

1 Like

I was struggling to get this done also and managed to get it done this way

Hope it helps someone

4 Likes

This works well for me, thanks a lot!

I’ve been trying to setup something like this for a while (but failed).

I’m getting the following error when trying to execute this:

2018-09-26 20:49:51 ERROR (MainThread) [homeassistant.components.shell_command] Error running command: `sudo reboot`, return code: 1
NoneType: None

What platform are you running on? HassIO (resinOS), HassIO (HassOS), Virtual Enviroment, Hassbian?

This option likely won’t work for the first two options.

I’m running Hassbian on RP3

The problem here is that you are trying to sudo from homeassistant not as the pi user since HA runs under the homeassistant account. Only pi is automatically granted sudo access.

One solution is to allow home assistant permission to run /sbin/shutdown by adding the following to /etc/sudoers.d/020_homeassistant_hassbian-scripts
You can also just add a file. For example you can make a new file named 025_homeassistant_custom-scripts and place it in the same /etc/sudoers.d folder

%homeassistant ALL= NOPASSWD: /sbin/shutdown *

now you can create the shutdown command as

shell_command:
  reboot_pi: '/usr/bin/sudo /sbin/shutdown -r now'
  shutdown_pi: '/usr/bin/sudo /sbin/shutdown now'

I also add the ability to script a HA restart by adding the following

%homeassistant ALL= NOPASSWD: /bin/systemctl restart [email protected]
shell_command:
  reboot_pi: '/usr/bin/sudo /sbin/shutdown -r now'
  shutdown_pi: '/usr/bin/sudo /sbin/shutdown now'
  ha_restart: '/usr/bin/sudo /bin/systemctl restart [email protected]'
5 Likes

I don’t quite understand, can you eleborate?

It would help if you explained what part you didn’t understand. It really depends on your level of unix administration experience. Guessing that is low let me try to elaborate…

Hassbian has two default users.
pi - standard pi user in all instances of raspbian (has sudo access to become root)
homeassistant - special user used to run HA (does NOT have sudo access to become root, this is GOOD)

when you ssh into hassbian you do so as pi user.
Then you run hashell to become homeassistant. hashell is an alias it just runs the command to sudo to the new user “alias hashell=‘sudo su -s /bin/bash homeassistant’”

When HA runs it runs as “homeassistant” user. This means HA can’t do anything that requires a root user. That is by design or bad things could be injected into your server so you don’t want to change homeassistant to have full root access.

But you want HA to issue the command “sudo shutdown -r now”. But sudo is a request to become root, homeassistant user is not allowed to do that.

The information I provided in my last post allows you to grant permission to homeassistant user to run ONLY shutdown, as root.

So by adding the permission to sudoers.d for homeassistant to run shutdown as root the command should now work.

I hope that explains why the commands I provided would solve the issue. If not please state the parts you are not understanding.

I figured this all out because I have System Admin experience and I googled the heck out of why this wasn’t working when I first attempted. :slight_smile:

2 Likes