Shutdown and Wake up your Windows PC from a Raspberry Pi

Requirements

  1. HASS running on Raspberry PI
  2. samba-common-bin for ‘net rpc’ installed on Raspberry PI
    sudo apt-get install samba-common-bin
  3. etherwake for ‘WOL’ installed on Raspberry PI
    sudo apt-get install etherwake
  4. Edit registry on Windows PC and add following registry key in this location

HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Policies \ System
Key Name: adding DWORD, name: LocalAccountTokenFilterPolicy, Hex value = 1.

Now we are ready to edit configuration.yaml

#################################################################
## Customize
#################################################################
  customize:
    switch.copy:
      icon: mdi:monitor
      friendly_name: Work PC
    switch.pc:
      hidden: true
    device_tracker.work_pc:
      hidden: true
    switch.wol:
      hidden: true

#################################################################
## Switches
#################################################################
switch:
  - platform: command_line
    switches:
      PC:
        offcmd: 'net rpc shutdown -I 192.168.X.X -U user%password'

  - platform: wake_on_lan
    mac_address: "00:22:68:46:CC:D4"
    name: "WOL"
    host: "192.168.X.X"

  - platform: template
    switches:
      copy:
        value_template: '{{ states.switch.wol.state }}'
        turn_on:
          service: switch.turn_on
          entity_id: switch.wol
        turn_off:
          service: switch.turn_off
          entity_id: switch.pc

#################################################################
## Automations PC online status
#################################################################
- alias: Start up PC office lights on
  trigger:
    platform: state
    entity_id: light.office
    from: 'off'
    to: 'on'
  condition:
    platform: time
    after: '18:00:00'
    before: '23:59:00'
  action:
    service: switch.turn_on
    entity_id: switch.wol

- alias: Shutdown PC office lights off
  trigger:
    platform: state
    entity_id: light.office
    from: 'on'
    to: 'off'
  condition:
    platform: time
    after: '18:00:00'
    before: '23:59:00'
  action:
    service: switch.turn_off
    entity_id: switch.pc

- alias: Shutdown PC when leaving home
  trigger:
    platform: state
    entity_id: device_tracker.device_to_be_tracked
    from: 'home'
    to: 'not_home'
  action:
    service: switch.turn_off
    entity_id: switch.pc

I now have

  • a button that can turn my pc on and off.
  • automation that turns my pc off when I leave the house
  • automation that turns my pc on when I turn on my office light at night.
  • automation that turns my pc off when I turn off my office light at night.

All while my units speaks to me…literally.


#################################################################
## Automations Devices online status
#################################################################
- alias: "System Online"
  trigger:
    platform: state
    entity_id: device_tracker.work_pc
    from: 'not_home'
    to: 'home'
  action:
    service: shell_command.speak_sus

- alias: "System Offline"
  trigger:
    platform: state
    entity_id: device_tracker.work_pc
    from: 'home'
    to: 'not_home'
  action:
    service: shell_command.speak_sds

#################################################################
## Shell commands
#################################################################
shell_command:
 speak_goodmorning: tts "Good morning."
 speak_afternoon: tts "Good afternoon."
 speak_evening: tts "Good evening."
 speak_sus: tts "System Online"
 speak_sds: tts "System Offline"

There used to be automation that would turn on my pc at morning time on weekdays but that proved to be somewhat counter productive.

Will add video in a sec.

9 Likes

There is built-in support in the forum’s editor for formatting source code, no need for images of the source code and people can just copy-and-paste your configuration. :wink:

Tried copy and pasting it in (hence numerous edits) but formatting was off.
So I was stuck doing the image thing. Sorry.

To format messages try putting three backticks ( ` ), then a new line, paste your config, and on another new line, three more backticks.

1 Like

Thanks. Will give that a whirl…

Done!

Here is a total newbie and dumb question for you. In your setup is the host: 192.168.x.x, is that the computer being woken up, the router, or the HA host. I’ve seen host used in all three instances so I’m a little lost. Thanks

Thats the target that needs to be woken up yes. And only questions unasked are dumb.

Ok follow up, is there a special program that needs to download to make that work. I have the Computer begin woken up ip address and it’s not showing any change at all. Thanks

Just a quick note: WOL isn’t supported since Windows 8 - it collides with fast-boot as far as I can recall. You might need to disable this feature in order to get WOL back.

Your network card needs to support WOL. If onboard card check your BIOS for WOL feature being enabled.

I can turn on and off my computer, it’s just the feedback that doesn’t work. I didn’t specify that very well. It made sense in my mind. I followed your set up at the begining of the post and that works great. I can turn it on and off from anywhere. The feedback saying if it’s on just doesn’t work. At the moment I’m using a template to change being home to on and not_home to off then doing it that way. It works, but it takes 2-3 minutes to get the actual reading.

Ah yes you mean the tts part. For that you need to install a tts engine which you can call from commandline.

not quite, I’m having a hard time expressing my question. The switch once I turn my computer on the switch shows it as off, and doesn’t change to on. I have to use a template sensor to make the switch move, which takes 2-5 seconds to show a change. If the switch doesn’t change I can’t turn it off cause it’s saying it’s off.

host (Optional): The IP address or hostname to check the state of the device (on/off). <— this is what I can’t get to work. Checking the state of my computer being woken up.

I appreciate the help so far. Now I can also do the tts stuff, I wasn’t planning on it, but now I have a direction to to when I want to. That is what I love of this community. There is always someone somewhere willing to help. Thanks for all the info guys.

So switching on work but you can not check the state of your PC is what your saying?

You are running HA on a Raspberry correct?
And you have device tracker setup using nmap?
Nettools installed etc?

That is what is needed to check online status based on IP or MAC.

Add the PC as to be tracked device in known_devices.yaml

I am getiing the following error:

homeassistant.bootstrap: Invalid config for [switch.command_line]: [offcmd] is an invalid option for [switch.command_line]. Check: switch.command_line->switches->pc->offcmd.

  • platform: command_line
    switches:
    pc:
    offcmd: ‘net rpc shutdown -I myip -U myusername%mypassword’
    oncmd: ‘script.join_pcon’

  • platform: wake_on_lan
    mac_address: “my MAcadresse”
    name: “WOL”
    host: “myip”

  • platform: template
    switches:
    copy:
    value_template: ‘{{ states.switch.wol.state }}’
    turn_on:
    service: switch.turn_on
    entity_id: switch.wol
    turn_off:
    service: switch.turn_off
    entity_id: switch.pc

Any ideas?

There was a breaking change in the update 0.28 for de command line switch component.
“offcmd” needs to be changed to “command_off”
“oncmd” needs to be changed to “command_on”

check for more information the command line switch component page.

I hope this helps.:slight_smile:

thx that fixed that issue but my switch isnt appearing although i see it in the entities.

I have also created a group for it in order to show up.

I have seen in other threads that there are people facing the same problem.

Any ideas for that?

hmm, i assume you checked your config with:

hass --script check_config

you did also setup the customize part correctly. so that the switch.copy is not hidden.

Then what you can try is to temporarily delete the default_view: group. (if you have that one).
You should see alle your devices in de home tab after restart. So the missing switch should pop-up also.

thx , so stupid of me, it was set to hidden

First off thanks for posting this, I hadn’t thought of using net rpc to shutdown computers.

However, I cannot seem to get it running. Using YAMLlint I have a valid setup but Home Assistant doesn’t like it.

16-09-24 18:09:53 homeassistant.bootstrap: Invalid config for [switch.command_line]: [Desktop] is an invalid option for [switch.command_line]. Check: switch.command_line->switches->Desktop.

My swtiches section looks like:

switch 1:
  ## 433 Mhz Radio Switches ##
  platform: rpi_rf
  gpio: 17
  switches:
    Living Room Lamp 1:
      code_on: 12345
      code_off: 12345
      protocol: 1
      pulselength: 194
    Living Room Lamp 2:
      code_on: 12345
      code_off: 12345
      protocol: 1
      pulselength: 194
    Front Porch Light 3:
      code_on: 12345
      code_off: 12345
      protocol: 1
      pulselength: 194 
    Garage Lights 4:
      code_on: 12345
      code_off: 12345
      protocol: 1
      pulselength: 194
    Spare Switch 5:
      code_on: 12345
      code_off: 12345
      protocol: 1
      pulselength: 194

switch 2:
  platform: command_line
  switches:
    Desktop:
      command_off: net rpc shutdown -I 192.168.0.0 -U user%password

Username and password (removed above) and IP address are all correct and work in putty to shut down the computer. The error seems to be somewhere in how my switches are setup. I tried using a phrase other than desktop but that didn’t help.

Thanks in advance!