This tutorial will show you how to setup a flow in Node-RED to send a command through SSH to your Mac and make it sleep on command.
ssh keygen
on your Node-RED instance, and then ssh-copy-id user@host
to send that key to your mac. (You will change user and host with your username for your mac and it's IP.I'm not going to go over installing Node-RED, however here's an up to date guide on how to install it if needed.
Here's a screenshot of how I have Node-RED configured for this setup. On the http in node on the left, I've setup a get method with the url /mac-sleep
. Connected to that, I have a http response node which is configured to give the http status of 200 when there's an http input. Finally, I have an exec node configured with the command ssh user@host 'bash sleep.sh'
.
To setup your Mac for this, you will want to open a new terminal window and run cd ~
to make sure you are in your user's home directory. To create and edit the file you are going to make, run nano sleep.sh
. Nano is the text editor and sleep.sh is going to be the file name. Paste in the commands below into sleep.sh.
# Run Sleep Command
osascript -e 'tell application "System Events" to sleep'
# Exit SSH session / terminal
exit
You can type CTRL+X, <enter button> and then Y, <enter button> to save and close the file. Now, run the command chmod +x sleep.sh
. to make it executable.
That should be all. Comment on this post if you have questions or issues.