Remote Shutdown/Restart/Sleep/Hibernate PC

Hi,

I thought I might share my attempt at remote Shutdown/Restart/Sleep/Hibernate of a windows machine.

I installed a windows program called Airytec Switch Off, I use to schedule a shutdown of my machine on a timer if I need it to run for an hour or two after I go to bed. it can also function as a web server and creates a little web page accessible over LAN and with port forwarding over the net (HTTP). you can also password protect the web page to stop mischevious goblins from playing pranks.


Here is the Setup:

  1. Download and install Airytec Switch Off.

  2. Setup a task to automatically start the program on login using Task Scheduler Guide

  3. Right-click the tray icon and go to options ā†’ remote ā†’ edit web interface settings. This opens another window. click the remote tab and enable a web interface and set a port number. if you are using home assistant remotely (away from home you likely have SSL so you donā€™t need to forward the port from this program)

  4. Click on View Web Interface you can now access this page on any machine over the local network to control your PC.
    (you may want to set your firewall to allow this program access to the home network)

  5. Ok now for the home assistant configuration:

input_select:
  pcoptions:
    name: PC Power Options
    options:
      - Restart
      - Shutdown
      - None
      - Sleep
      - Hibernate
    initial: None
    icon: mdi:desktop-tower

shell_command:
  shutdown_z800: 'curl -k (url of links from step 4)'
  restart_z800: 'curl -k (url of links from step 4)'
  sleep_z800: 'curl -k (url of links from step 4)'
  hibernate_z800: 'curl -k (url of links from step 4)'

automation:
  - alias: z800_restart
    trigger:
      platform: state
      entity_id: input_select.pcoptions
      to: "Restart"
    action:
      - service: shell_command.restart_z800
  - alias: z800_shutdown
    trigger:
      platform: state
      entity_id: input_select.pcoptions
      to: "Shutdown"
    action:
      - service: shell_command.shutdown_z800
  - alias: z800_sleep
    trigger:
      platform: state
      entity_id: input_select.pcoptions
      to: "Sleep"
    action:
      - service: shell_command.sleep_z800
  - alias: z800_hibernate
    trigger:
      platform: state
      entity_id: input_select.pcoptions
      to: "Hibernate"
    action:
      - service: shell_command.hibernate_z800   

In my setup, I also get a notification on telegram via IFTTT when any of these actions are performed.

And there you have itā€¦ super easy and very useful.

ā€œIā€™m sorry I wrote you such a long letter; I didnā€™t have time to write a short one.ā€
Blaise Pascal

35 Likes

Nice project and great quote!

Been doing the same. But didnā€™t think about the dropdown

Also itā€™s pretty cool that Switch Off can be used for other tasks beyond power management.

ye for sureā€¦ tbh I only implemented the drop down a few days ago, because I kept on pressing the restart switch by accident when using my phone. #fatfingers :sob:

3 Likes

Hi bilalY,

Thanks for this, I am using it now and it works well!

I added to the automation to make it re-select none after something was chosen:

  action:
    - service: shell_command.sleep_
    - service: input_select.select_option
      data:
        entity_id: input_select.pcoptions
        option: None

One question though. On my dashboard my input slider says ā€œInput Sliderā€ while yours says ā€˜Desktop PCā€™. How did you set it to that?

Thanks!

3 Likes

Hi,
Glad you you like itā€¦ that automation is a cool idea might have to stick it in my setup.

i made a group with the WOL switch and the drop down menu and named the group as Desktop PC.

desktop_pc:
  name: 'Desktop PC'
  control: hidden
  entities:
    - switch.z800_wol
    - input_select.pcoptions

https://home-assistant.io/components/group/
https://youtu.be/EsBtyF5tZQ4

Hope this helpsā€¦ Good luck.

1 Like

Very cool project.

1 Like

Looks like a much better option that the little software I coded to put my computer to sleep, will try that out :slight_smile:

1 Like

Wow, great idea. Thanks for sharing!
I was using the WOL comonent for some time now but didnā€™t had an option to implement a shutdown sequence via Hass. As a workaround I was using AutoRemote and EventGhost but this is much less complex!

I have trouble getting it to work though. I implemented it like this:

shell_command:
  pc_shutdown: 'curl -k (http://10.0.0.131:8000/?action=System.Shutdown)'
  pc_restart: 'curl -k (http://10.0.0.131:8000/?action=System.Restart)'
  pc_sleep: 'curl -k (http://10.0.0.131:8000/?action=System.Sleep)'
  pc_hibernate: 'curl -k (http://10.0.0.131:8000/?action=System.Hibernate)'

My Hass is running on a Raspberry Pi on the same network as my desktop PC. The (internal) IP of my PC is 10.0.0.131.
The links in the ā€œView Web Interfaceā€ look like this:

http://localhost:8000/?action=System.Sleep

What am I doing wrong here?

P.S I also tried without the ā€œ()ā€ but with the same result.

1 Like

I left my shell command as ā€¦

shutdown_z800: ā€˜curl -k http://localhost:9123/?action=System.Shutdownā€™

and it works fine (my pc is also on the same network and Iā€™m running HA on a RPI3), so you may want to leave localhost in there?

I tried but unfortunately not working. Shell command is executed by Hass, also there are no errors in my logfile.

Hmmm is your port 8000 being used for anything else?

hmm i can see how what i wrote can be confusing i will try to edit it though it may be too late to be able to edit the post since it has been so longā€¦

i use secrets to hide the url because if i have samba turned on anyone on the network has access to the hass config and as such could get the url from the filesā€¦ then my evil siblings would just spend the rest of their days turning off my computer while i am using it.

shutdown_z800: 'curl -k http://192.168.0.26:7760/?action=System.Restart'

this i how it would look like without secrets. mine doesnā€™t show up with local host but i shall investigate and get back to you

@keithh666 thanks or chiming in

1 Like

I have tried to implement it on my computer but for some reason the webserver can only accessed from that computerā€¦ I will have to look more into that.

that seems quite odd. maybe itā€™s a firewall issue?

For those having a lot of trouble with Airytec Switch Off, you might want to give this program a try SleepOnLan.
Before i used home assistant i used this Raspberry Pi Remote Wake/Sleep-On-LAN Server Tutorial by Jeremy Blum for turning my pc on and off remotely. It worked really well but HomeAssistant is better :smile:

1 Like

You are using IP, not localhost, so I will assume you are trying to control remote computer, in that case Airytec Switch Off would require a username and password.

You can setup the password on the Options tab of Airytec and then change your shell_command to :

shutdown_z800: 'curl -u User:<password> -k http://192.168.0.26:7760/?action=System.Restart'

** User and password is case sensitive.

Hello, I donā€™t understand why it doesnā€™t work for me (my ports are okay, no user auth etc) :frowning:

input_select:
  pcoptions:
    name: PC Power Options
    options:
      - Restart
      - Shutdown
      - None
    initial: None
    icon: mdi:desktop-tower

shell_command:
  shutdown_z800 : 'curl -k http://192.168.0.141:8909/?action=System.Shutdown'
  restart_z800 : 'curl -k http://192.168.0.141:8909/?action=System.Restart'

automation:
  - alias: z800_restart
    trigger:
      platform: state
      entity_id: input_select.pcoptions
      to: "Restart"
    action:
      - service: shell_command.restart_z800
  - alias: z800_shutdown
    trigger:
      platform: state
      entity_id: input_select.pcoptions
      to: "Shutdown"
    action:
      - service: shell_command.shutdown_z800

Thank you in advance

You have to have a web browser open on http://IP:11000/ i think (on the computer you want to shutdown/hibinate etc)

you do need to make sure that port is not blocked on Windows Firewall.
I donā€™t see any other issue with your config.

What happens when you issue the command manually from the RPI/ whatever you have your HA on?

I think heā€™s not blocked because I can access to the link and make all commands from another computer and itā€™s work :frowning:

Maybe I havenā€™t write the block at the good place in configuration.yaml ? (I have write the block juste under everything, I have edit anything else)