I need a Home Assistant automation to put my computer to sleep when my computer room is not occupied. This involves using OpenSSH, Psshutdown64 and Wake-on-LAN on the Windows computer and Terminal for SSH on the Home Assistant computer. It also requires Private/Public authentication to avoid requiring a password.
From William Campbell:
This is a YouTube video to setup SSH using private/public key authentication between Linux and Windows. Pssshutdown64 from Microsoft SysInternals command is needed for sleeping a Windows computer. Remaining is how to wake a computer from Home Assistant.
From ChatGPT:
1. Set Up SSH Access on the Windows Computer
Generate SSH Key Pair (if not already done):
- On your local machine (not the Windows computer), generate an SSH key pair if you haven’t already:bashCopy code
ssh-keygen -t rsa -b 4096Follow the prompts to generate the key pair. This will createid_rsa(private key) andid_rsa.pub(public key) in your~/.ssh/directory.
Copy Public Key to Windows Machine:
- Copy the contents of
id_rsa.pub(your public key) from your local machine. - Log in to your Windows machine.
- Create a
.sshdirectory in your Windows user’s home directory (C:\Users\YourUsername\.ssh) if it doesn’t exist. - Add your public key to
authorized_keysin your Windows user’s.sshdirectory. - Ensure proper permissions are set on the
.sshdirectory (700) andauthorized_keysfile (600).
2. Install psshutdown64 on the Windows Computer
Download Sysinternals Suite:
- Download the Sysinternals Suite which includes
psshutdown64. - Extract the suite and locate
psshutdown64.exe.
Add psshutdown64 to Path:
- Add the directory containing
psshutdown64.exeto your system’s PATH environment variable for easy execution from any directory.
3. Configure Home Assistant Automation
Home Assistant Automation Script:
- In your Home Assistant installation, create or modify an automation YAML file (e.g.,
automations.yaml).
Example Automation Script:
yamlCopy code# Example: Sleep Windows Computer using SSH
- alias: 'Sleep Windows Computer'
trigger:
platform: state
entity_id: binary_sensor.your_trigger_sensor # Replace with your trigger
to: 'on' # Trigger when sensor turns on
action:
service: shell_command.sleep_windows_computer
shell_command:
sleep_windows_computer: 'ssh -i /path/to/your/private/key user@windows-ip-or-hostname "psshutdown64.exe -d -t 0"'
- Replace
binary_sensor.your_trigger_sensorwith the entity ID of the sensor that triggers this automation (e.g., a button press, motion sensor, etc.). - Update
/path/to/your/private/key,user, andwindows-ip-or-hostnamewith your SSH private key path, Windows username, and Windows machine IP address or hostname respectively.
4. Testing the Automation
- Restart Home Assistant to apply the new automation.
- Trigger the automation by activating the specified trigger (e.g., pressing a button connected to the binary sensor).
- Home Assistant will execute the SSH command using the specified private key to connect to your Windows machine and run
psshutdown64.exeto put the computer to sleep.
Ensure that SSH access is enabled and properly configured on your Windows computer for this setup to work securely. This automation assumes that your Home Assistant instance can reach your Windows machine over the network and has SSH access configured with the appropriate private key. Adjust the paths and commands as necessary based on your specific setup and environment.