HA + OctoPrint + WeMo + Alexa Automation

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 the printer_on script and Off runs the printer_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 the command_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 the printer_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.

2 Likes

So jealous of you 3D printer guys…

@Geoff_Brown i have this error:

2018-03-11 13:38:43 ERROR (SyncWorker_16) [homeassistant.components.switch.command_line] Command failed: ssh [email protected] ‘sudo /usr/local/bin/lights_off.sh’

I actually don’t use this setup anymore, so I am not sure how to help you with your error.

If I may, why do you not use this setup, and what are you using now? Looking to do something similar but just looking at ideas at this point.

I had to go away for work for a few months and had to take down my 3d printing setup just prior, so now I just have octoprint (on its own pi) reporting to node red via mqtt and handling automations that way. This is better because the old way relied on the printer being up and connected to octoprint when ha started (thus my restart ha automation) but node red and mqtt do not have such a requirement. I haven’t set up any fancier automations yet, such as auto shut off etc, but probably will do so one of these days.