I automated the process of starting up my 3D printer and OctoPrint (running on a 1st gen Pi). Everything is plugged in to a WeMo switch - when shutting down, a command_line
is first sent to the OctoPrint Pi to do a proper shutdown before powering off the WeMo.
I am fairly new to Home Assistant, coming from a Veralite for the last 3 or 4 years, hopefully someone finds some use in this setup. I appreciate any comments or ideas for making it more efficient or cleaner as well.
The reason for automating this process is that there seemed to be several steps involved for setting up for a print: power on the 3D printer, power on the OctoPrint Pi (unless you leave it on), connect OctoPrint to the 3D printer via the OctoPrint web interface and finally, restart Home Assistant so that the OctoPrint component initializes properly. As well, I didnât need to leave the OctoPrint Pi running all the time so I decided to incorporate the proper shutdown of the Pi running OctoPrint into the automation.
Additional automations include powering off the printer if it has been idle for 30 min, and powering off the printer after 15 min of print completion (as long as another print has not begun).
Now I just use my Echo Dot; âAlexa, turn on/off the 3D Printerâ, and everything starts up and shuts down properly and, more importantly, automatically. To do this last part you have make sure that your template switch is made discoverable by the emulated hue component.
My inventory:
Hardware
- Wanhao Duplicator i3 v2.1 3D printer
- A cheap LED strip - turns on with the WeMo and lights up the print bed
- Raspberry Pi (1st gen)
- Hassbian running on a Pi 3
- WeMo switch (hooked up to a power bar)
- Echo Dot for voice control
HA components
- OctoPrint component
- WeMo component
- Command_line switch
- Template Switch
- Automations
- Scripts
SWITCHES
-
The command line switch does an ssh into the OctoPrint Pi to perform the shutdown command. Follow this guide to set up passwordless ssh between the HA Pi and OctoPrint Pi.
-
The template switch takes its state from the status of the WeMo switch.
On
runs theprinter_on
script andOff
runs theprinter_off
script.
Command Line
- platform: command_line
switches:
octoprint_pi:
command_off: "ssh [email protected] 'sudo /sbin/shutdown -h now'"
Template switch
- platform: template
switches:
printer:
value_template: "{{ is_state('switch.garage_pi', 'on') }}"
turn_on:
service: script.turn_on
entity_id: script.printer_on
turn_off:
service: script.turn_on
entity_id: script.printer_off
SCRIPTS
-
printer_on
turns on the WeMo, waits 2 min, and then restarts Home Assistant. -
printer_off
makes sure that OctoPrint is not currently printing, then runs thecommand_line
switch above, waits 1 min for proper shutdown of the OctoPrint Pi, and then switches off the WeMo.
Printer_on
printer_on:
alias: "Turn on the 3D Printer"
sequence:
- service: switch.turn_on
entity_id: switch.garage_pi
- delay:
minutes: 2
- service: homeassistant.restart
Printer-off
printer_off:
alias: "Turn off 3D printer"
sequence:
- condition: template
value_template: "{% if is_state('binary_sensor.octoprint_printing', 'off') %}true{% endif %}"
- service: switch.turn_off
entity_id: switch.octoprint_pi
- delay:
minutes: 1
- service: switch.turn_off
entity_id: switch.garage_pi
AUTOMATIONS
-
The first automation waits 15 min after completing a print before running the
printer_off
script (which checks to make sure another print has not started before shutting down). -
The second one starts a 30 min clock upon switching on the WeMo before running the same power down script. I use this one in case I fire up the printer and get sidetracked or if I accidentally power the system on.
-
The third sets the state of the
command_line
switch so that the power off command can be called by theprinter_off
script when needed.
AUTOMATIONS
- alias: 'turn off printer 15 min after print completion'
trigger:
platform: state
entity_id: binary_sensor.octoprint_printing
from: 'on'
to: 'off'
action:
- delay:
minutes: 15
- service: script.turn_on
entity_id: script.printer_off
- alias: 'Turn off printer if idle for 30 min'
trigger:
platform: state
entity_id: switch.garage_pi
to: 'on'
action:
- delay:
minutes: 30
- service: script.turn_on
entity_id: script.printer_off
- alias: 'Set octoprint switch on when booted up'
trigger:
- platform: template
value_template: "{% if is_state('switch.garage_pi', 'on') %}true{% endif %}"
action:
- service: switch.turn_on
entity_id: switch.octoprint_pi
Make your switch discoverable by emulated hue in customize
:
switch.printer:
icon: mdi:printer-3d
friendly_name: 3D Printer
emulated_hue: true
emulated_hue_name: 3D Printer
note: I donât think you need both friendly_name
and emulated_hue_name
, you can probably get by with one or the other.
Again, all comments are welcome. I am new to this and would love to improve it. Otherwise, I hope I was able to help someone out with a similar setup.