I recently have been running longer prints that sometimes finish in the middle of the night (or day when I’m not around). Sometimes these prints would finish at 2am and I would hate for the printer to be left on until I wake up in the morning.

So, I set out to automate turning the printer off after a print is finished. To do this I have the printer plugged into a Meross smart plug and the printer is controlled by Octoprint (running on a pi). Throughout this post, I’m assuming you have Octoprint already setup and working with your printer.

Octoprint allows you to configure events that can run any system command. For me, I’m most comfortable in JavaScript. I’m going to run a JS script once Octoprint sends the “PrintDone” event.

Controlling the smart plug

First thing we need to do is make sure Node.js is installed on the pi. If not, you can install it by running:

$ sudo apt-get update` 
$ sudo apt-get install nodejs

Once Node is installed, you can add the script that will turn the printer off. This is the function I wrote to do this with a Meross smart plug. For me, I placed the contents of that GitHub gist in a scripts folder under the pi users root directory:

$ cd ~/scripts
$ curl https://gist.githubusercontent.com/Robdel12/60011235a0e1e3d8a99367bf9fcb388b/raw/e3c730fe12d509705abd5118175400bd40413806/toggle-plug.js -o turn-off-plug.js

Running the curl command will place a `turn-off-plug.js` file into the directory it was run in. You will need to know the IP address of the Meross plug. Once you find that (I use debokee), edit turn-off-printer.js to fill in your plugs IP:

// Example usage:
// Comment these lines below out and add your plugs IP:
(async () => {
await toggleMerossPlug(“[IP_HERE]”, “off”);
})();

The last thing that needs to be done to make that file work is installing the `request` dependency. First run `npm init` in ~/scripts & follow the prompts. After that run `npm i -D request`. Now you can test your script by running `node ~/scripts/turn-off-plug.js`! If that turns the plug off, everything works. 🎉

Controlling the smart plug from Octoprint

Now that we can control the plug (and the printer) from a node script, lets wire that up to Octoprint. We’ll use the `PrintDone` event to run our node script. As it’s named, that event will fire once the print has finished up.

We will need to edit the `config.yaml` file Octoprint has. First cd into the `.octoprint` directory (`$ cd ~/.octoprint`) and add the following config:

Save the config file and restart the rasbperry pi. Now run a test print to verify everything works. I used the bed leveling gcode so its quick and easy to see if it works. 🎉