Reboot Pi with script

Hi there,

what script do i need to use in order to safely issue a reboot command?

All the ideas mentioned in this forum that i have tried until now didnt work.

sudo reboot -now

I use this to reboot my pi also works well and haven’t run into any issues. Write a command line item first and the call it in the script.

@silvrr could you paste your full config for this ?
I’ve been struggling to have this exact function done without success to date
Also looked at the example on the home assistant without success setting that up

thanks for your help

On my side here’s what i have

logs:

16-10-28 06:22:40 homeassistant.core: Bus:Handling <Event call_service[L]: domain=homeassistant, service=turn_off, service_data=entity_id=switch.shutdown, service_call_id=1978220624-3>
16-10-28 06:22:40 homeassistant.core: Bus:Handling <Event call_service[L]: domain=switch, service=turn_off, service_data=entity_id=['switch.shutdown'], service_call_id=1978220624-4>
16-10-28 06:22:40 homeassistant.components.switch.command_line: Running command: sudo reboot
16-10-28 06:22:40 homeassistant.components.switch.command_line: Command failed: sudo reboot
16-10-28 06:22:40 homeassistant.core: Bus:Handling <Event service_executed[L]: service_call_id=1978220624-4>
16-10-28 06:22:40 homeassistant.core: Bus:Handling <Event service_executed[L]: service_call_id=1978220624-3>

Config:

switch:
  platform: command_line
  switches:
    shutdown:
      command_off: "sudo reboot"

I even tried the simple example copy pasting from homeassistant.io example, and it simply does not work, the switch name is too long as the switch command line component does not even load.

Example configuration.yaml entry

switch:
platform: command_line
switches:
Home Assistant system shutdown:
command_off: “/usr/sbin/poweroff”

Ok, sorry to mislead a bit, I have a script which calls a shell command.

Script:
  reboot_pi:
    alias: Reboot Raspberry Pi 3
    sequence:
      - alias: Reboot Raspberry Pi 3
        service: shell_command.pi 

shell_command:
  pi: "sudo reboot"

I guess I am a worrywart but I try to avoid rebooting unless I shutdown HA first.

Yeah me too , once I have the reboot function to work I’ll have a script that stops ha then reboot :slight_smile:

I’ll try it out later tonight and let you know how it went
Thanks for the full instructions

I can’t look up the exact service call as my work blocks my HA instance but I think you could just call the turn off service and then call the reboot command.

Script:
  reboot_pi:
    alias: Reboot Raspberry Pi 3
    sequence:
        service: homeassitant.turn_off
        service: shell_command.pi 

shell_command:
  pi: "sudo reboot"
1 Like

This is what I had in mind

I’ll reply here when I test it out after work , I’m blocked from work as well :wink:

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.