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

Tags: #<Tag:0x00007f616f955cb8> #<Tag:0x00007f616f955bf0>

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 pit 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.
    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) touch /etc/openhab2/scripts/pc_to_sleep.sh
    d) sudo chown openhab:openhab /etc/openhab2/scripts/pc_to_sleep.sh
    e) sudo chmod 770 /etc/openhab2/scripts/pc_to_sleep.sh
    f) Copy the code to /etc/openhab2/scripts/pc_to_sleep.sh

#!/bin/bash

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

exit 0
  1. Configure OpenHAB:

exec.whitelist

/etc/openhab2/scripts/asterix_to_sleep.sh

XXX.items

Switch Asterix_Power “PC”

Switch Asterix_WOL “Wake-on-LAN Asterix” {wol=“255.255.255.255#MAC-ADRESS”}

Switch Asterix_ToSleep “Asterix 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]

XXX.rules

rule “PC an/aus”

when

Item Asterix_Power received command

then

if (receivedCommand == ON)

{

    logInfo("Rule triggered", "Rule \"trigger_things_with_switches.rules: PC an/aus: an\" started")

    Asterix_WOL.sendCommand(ON)

}

else

{

    logInfo("Rule triggered", "Rule \"trigger_things_with_switches.rules: PC an/aus: aus\" started")

        PC_ToSleep.sendCommand(ON)

}

end

XXX.sitemap

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 =)

Greetings,
Felix

2 Likes