Turning off a windows pc with home assistant

So, I found this great project on github to turn off a windows machine and wanted to share it as it’s something I’ve wanted to do for ages.
go to the releases section and download the zip file and the rest should be easy

here is the link

and how I’ve integrated it into my home assistant

switch:
- platform: command_line
  switches:
    pc:
      command_off: 'curl http://10.10.10.70:5001/hibernate'
 - platform: wake_on_lan
   name: windowswol
   host: 10.10.10.70
   mac_address: D8:CB:8A:C3:8C:A2
 - platform: template
   switches:
     windows:
       value_template: '{{ states.switch.windowswol.state }}'
       turn_on:
         service: switch.turn_on
         entity_id: switch.windowswol
       turn_off:
         service: switch.turn_off
         entity_id: switch.pc

Hope it helps someone

Sam

11 Likes

Yes, that works fine and used it along time. But I want to do it with mqtt so i made this : https://community.home-assistant.io/t/integrating-win10-in-ha-with-mqtt/

(sorry for “promoting” my own stuff :slight_smile: )

2 Likes

I addressed this problem by running a ‘net rpc’ command which I believe is part of samba.

Here is the command I run. There is a password in the command, obviously not a great solution, but there you go.

/usr/bin/net rpc -S 192.168.1.100 -U<My username>%<my password> shutdown -t 1 -f

I’m using his MQTT system as well :slight_smile: Works like a charm.

I added the MQTT command as the turn off for the WOL Switch thats connected to my PC.

On top i added some automation thats based on an open/close sensor thats connected to my Keyboard drawer. As soon as i pull out the Keyboard, the PC turns on, and when i hide it away, the PC turns off :slight_smile:

- alias: 'Turn ON PC'
  trigger:
platform: state
entity_id: binary_sensor.xiaomi_kontakt
from: 'off'
to: 'on'
for:
  seconds: 2
  action:
service: switch.turn_on
data:
  entity_id: switch.gaming
  
- alias: 'Turn OFF PC'
  trigger:
platform: state
entity_id: binary_sensor.xiaomi_kontakt
from: 'on'
to: 'off'
for:
  seconds: 10
  action:
service: switch.turn_off
data:
  entity_id: switch.gaming
2 Likes

that’s really cool :slight_smile:

Very good,
for completition if you want to turn off pc even you are not logged into windows you can create a service with this exe using nssm https://nssm.cc/download.
Just tried and work good

Hi, I´ve tried to use this code as mentioned in the first post but that didn´t worked for me, I don´t know what is wrong. Standby, Shutdown and Hibernate woks withe the entity switch.pc.
But WOL doesen´t with this entity, I got WOL working with an entity of switch.windowswol.

15

switch:
- platform: command_line
  switches:
    pc:
      command_off: 'curl http://XXX.XXX.XXX.XXX:5001/hibernate'
- platform: wake_on_lan
  name: windowswol
  host: XXX.XXX.XXX.XXX
  mac: XX:xX:XX:Xx:XX
- platform: template
  switches:
   windows:
     value_template: '{{ states.switch.windowswol.state }}'
     turn_on:
       service: switch.turn_on
       entity_id: switch.windowswol
     turn_off:
       service: switch.turn_off
       entity_id: switch.pc

I use a different script on my desktop PC (don’t quite remember which one), but this is the code I use. You just have to change the curl command to suite your needs:

shell_command:
  sleep_on_lan_desktop: 'curl http://192.168.xxx.xx:50809/sleep'

wake_on_lan:

switch:
  - platform: wake_on_lan
    mac: "XX:XX:XX:XX:XX:XX"
    name: "Desktop"
    host: 192.168.XXX.XX
    turn_off:
      service: shell_command.sleep_on_lan_desktop

This gives me 1 switch: turning it on wakes my desktop, turning it off puts it to sleep.

6 Likes

I’m using a shelly 1 connected to the power button pins on the board. The shelly has tasmota so I can easily turn it on on home assistant.
If PC is off, switching it will turn on. When the Pc is on, it hibernates.
Basically I configured the windows PC to hibernate when the power button is clicked and if it’s on.

Very simple and I don’t need any additional software in the PC neither WOL compatible card.

4 Likes

Easy…

Install rpc shutdown add-on

Add switch below in configuration

Switch:
  - platform: wake_on_lan
    mac: AA:11:22:33:44:AA
    name: pc
    host: 192.168.1.18
    turn_off:
      service: hassio.addon_stdin
      data:
        addon: core_rpc_shutdown
        input: htpc

3 Likes

Works if you only want shutdown, but RPC Shutdown is not currently compatible with hibernate (sleep, etc.)

1 Like

How to install shutdown-pc to pc?

Comment from an old Windows support guy, formerly managing over 10,000 Windows PCs…

Sleep and Hibernate cause more problems than they’re worth. Just shut it down and let it boot clean next time.

Yes, this is just my opinion. No doubt someone will chime in with an opposing view. I can only speak from my own experience over decades, starting with Windows 3.1. Take it or leave it.

4 Likes

What is the syntax to use the “Turn off screen” command please? * http://remote-host-name:5001/turnoffscreen? …/turn-off-screen? other?

Other question, do I have to open the 5001 port on my PC firewall? Because for now nothing works sadly

Is there anything to configure first in configuration.yaml to use curl commands? I have this error: Logger: homeassistant.components.command_line
Source: components/command_line/init.py:43
Integration: command_line (documentation, issues)
First occurred: 19:19:09 (2 occurrences)
Last logged: 19:20:24

Timeout for command: curl http://192.168.1.29:5001/test/turn-screen-off

Thanks for you help

quick and painless, thanks!

1 Like

What did you change to get the sleep option instead of hibernate?

The code is in my post you’re replying to. The script I was using at the time is this one (I believe):

In my code, it seems I was using a different port so beware that may need adjusting.
That’s about all I can contribute cause I’ve moved on to using IOT Link instead of the script.

Use hass agent with mqtt and homeassistant discovery…

1 Like

i get this error:

Command Line YAML configuration has moved

This stops working in version 2023.12.0. Please address before upgrading.

Configuring Command Line switch using YAML has moved.

Consult the documentation to move your YAML configuration to integration key and restart Home Assistant to fix this issue.

adding this to the yaml file fixed the warning messsage:

command_line:
  - switch:
      name: pc_lock
      command_on: curl 'http://192.168.145.123:5232/lock/lock/'
      command_off: curl 'http://192.168.145.123:5232/lock/lock/'
  - switch:
      name: pc_hibernate
      command_on: curl 'http://192.168.145.123:5232/lock/hibernate/'
      command_off: curl 'http://192.168.145.123:5232/lock/hibernate/'
  - switch:
      name: pc_restart
      command_on: curl 'http://192.168.145.123:5232/lock/restart/'
      command_off: curl 'http://192.168.145.123:5232/lock/restart/'
  - switch:
      name: pc_shutdown
      command_on: curl 'http://192.168.145.123:5232/lock/shutdown/'
      command_off: curl 'http://192.168.145.123:5232/lock/Shutdown/'
  - switch:
      name: pc_shutdown_delay_by_60sec
      command_on: curl 'http://192.168.145.123:5232/lock/shutdown/?delay=60'
      command_off: curl 'http://192.168.145.123:5232/lock/Shutdown/?delay=60'
  - switch:
      name: pc_forceshutdown
      command_on: curl 'http://192.168.145.123:5232/lock/forceshutdown/'
      command_off: curl 'http://192.168.145.123:5232/lock/forceshutdown/'
1 Like