Put your Windows PC to Sleep or execute anything else with OpenHAB

Hey there =)

Today I figured out how to put your Windows PC to Sleep from OpenHAB. This is NOT about shutting it off but to put it to SLEEP. However I guess with using this strategy you can shut it down as well as execute anything in the command line of the Target PC.
There are plenty of tutorials to shut your pc down however I could not find one to put it to sleep.

  1. Install OpenSSH on the Host/Target-PC (the PC you want to execute your commands on):
    There is a great and SUPER SIMPLE tutorial from Microsoft: https://docs.microsoft.com/de-de/windows-server/administration/openssh/openssh_install_firstuse This site is in German but on the top right there is a switch “Auf Englisch lesen” which toggles the site to use English.

  2. Try the ssh connection from your OpenHAB-Server:
    a) sudo -u openhab ssh user@target
    b) it establishes a connection
    c) close the ssh connection with exit

  3. To open a ssh connection to your server in a script (which is what we have to do) your script has to be not asked for a password for the for user of the target pc. In order to accomplish that type these instructions on your OpenHAB-Server. I am assuming your server is running on Linux. When using Windows as a server this commands should only be somewhat different.
    a) sudo -u openhab ssh-keygen -t rsa
    Dont enter a password for the file when asked. If you’d so so running the script later won’t work.
    This generates a pair of unique ssh keys to authenticate

    b) The contents of the just generated id_rsa.pub file have to put into C:\Users\USER\.ssh\authorized_keys. To accomplish that a just copied the file to the OpenHAB-Samba Share folder.
    To do this use: sudo cp /var/lib/openhab2/.ssh/id_rsa.pub /srv/openhab2-conf
    Now you can grab on your windows pc and attach the complete content of id_rsa.pub to the end of C:\Users\USER\.ssh\authorized_keys.
    c) You may want to restart the ssh server with Restart-Service sshd executed in the Windows PowerShell started as administrator.

  4. Test the ssh connection from your OpenHAB-Server again:
    a) sudo -u openhab ssh user@target
    b) this time you should not be asked for a password
    c) close the ssh connection with exit

  5. Now comes the simple and routine parts:
    a) Create a script to put your pc to sleep with touch /etc/openhab2/scripts/pc_to_sleep.sh
    b) Everything in between of the ’ ’ can be deleted and replaced with anything you want to execute in the command line of the host pc. This is the code to put the pc to sleep.
    c) sudo chown openhab:openhab /etc/openhab2/scripts/pc_to_sleep.sh
    d) sudo chmod 770 /etc/openhab2/scripts/pc_to_sleep.sh


ssh -o "StrictHostKeyChecking no" USER@HOST 'powercfg -hibernate off  &&  start /min "" %windir%\System32\rundll32.exe powrprof.dll,SetSuspendState Standby  &&  ping -n 3  &&  powercfg -hibernate on'

exit 0
  1. Configure OpenHAB:




Switch  Asterix_Power   "PC"                 
Switch  Asterix_WOL     "Wake-on-LAN Asterix"        {wol=""}
Switch  Asterix_ToSleep "PC Energiesparmodus"        {channel="exec:command:Command_Asterix_ToSleep:run"}

exec.things (the exec binding has to be installed)

Thing exec:command:Command_Asterix_ToSleep [command="/etc/openhab2/scripts/pc_to_sleep.sh", interval=0]


rule "PC an/aus"
    Item Asterix_Power received command
    if (receivedCommand == ON) {
        logInfo("Rule triggered", "Rule \"trigger_things_with_switches.rules: PC an/aus: an\" started")
    } else {
        logInfo("Rule triggered", "Rule \"trigger_things_with_switches.rules: PC an/aus: aus\" started")


Switch item=Asterix_WOL label="PC" mappings=[ON="AN"]

Now you are able to turn your pc on and off with OpenHAB.
I hope I could help you. If you have any questions hit me up =)



Thanks for this, very interesting!

Thanks for the code, thats a great idea!
For openhab 3, some pathes need to be changed. I struggeld with the authentication, thats because of some Changes in Windows 10:

After commenting out those two lines from the bottom of sshd_config it works well.

The ECMA / Javascript for openHAB3:

var log = Java.type("org.slf4j.LoggerFactory").getLogger("org.openhab.model.script.Rules.pc_to_sleep");
var Exec = Java.type('org.openhab.core.model.script.actions.Exec');
var Duration = Java.type('java.time.Duration');


execRes = Exec.executeCommandLine(Duration.ofSeconds(10),'/etc/openhab/scripts/pc_to_sleep.sh');

log.info("pc_to_sleep.sh: " + execRes);

The script for hibernate mode:


ssh -o "StrictHostKeyChecking no" user@host 'start /min "" %windir%\System32\rundll32.exe powrprof.dll,SetSuspendState &&  ping -n 3'

exit 0

Update: Works with Windows 11.


Just wanted to say thanks for this. I’ve recently started using windows 11 and it is having an issue not going to sleep per its settings. But it was a good time to do this setup and trigger sleep off of my office status on/off.

The only issue i had was with the permissions for the public key file on win11 under openssh. Beyond that it works like a charm, thx!

I have used this for a long while now, easy setup etc.

withe this rule

rule "Desktop OFF"
	Item Desktop changed from ON to OFF

Just thought I would add this to this thread as it may be useful to some.

1 Like

Can you please clarify with permissions are needed? Anything special? The standard is 644 and should work, doesn’t it?

Thanks for sharing Remote Shutdown Manager, @Adrian_Hills. I’ve been perfectly satisfied with Assistant Computer Control, but I don’t use ACC for anything other than putting my PC to sleep. RSM is a much more direct solution that was super easy to implement.

You bet. My daily ID for use with windows 11 is an admin ID (I know, I know :)) meaning that for passwordless auth with openssh server the public key must have ONLY admin permissions. If you have any local permissions setup at all it won’t work and will prompt for a password.

I’m tempted to actually try out a couple of the other methods above that were since listed, but I like the fact that I’m not dependent on any other services or applications with a direct call via SSH.


1 Like