Cookbook: Useful Event Hooks

This node's purpose is to collect useful event hooks in the form of shell or gcode commands to execute that you can add to OctoPrint's configuration in the events.subscriptions section to fulfill certain tasks. Feel free to contribute, this is a wiki node that any member with a trust level of 1 or higher can edit!

Automatically upload timelapses to YouTube

Using youtube-upload and the MovieDone event hook it is possible to automatically upload finished timelapses to YouTube.

You'll need to install the dependencies first

  • Python 2.6/2.7/3.x.
  • Packages: google-api-python-client, progressbar2 (optional).

Check if your operating system provides those packages (check also those deb/rpm/mac files), otherwise install them with pip:

sudo pip install --upgrade google-api-python-client progressbar2

Then you'll need to install youtube-upload. Assuming you are on a Pi, this can accomplished via

wget https://github.com/tokland/youtube-upload/archive/master.zip
unzip master.zip
cd youtube-upload-master
sudo python setup.py install

Latest version of youtube-upload uses OAuth2.0 to authentificate to youtube API, package includes a default client_secrets.json file. but if you plan to make a heavy use of the script, please create and use your own OAuth 2.0 file, it's a free service. Steps:

  • Go to the Google console.
  • Create project.
  • Side menu: APIs & auth -> APIs
  • Top menu: Enabled API(s): Enable all Youtube APIs.
  • Side menu: APIs & auth -> Credentials.
  • Create a Client ID: Add credentials -> OAuth 2.0 Client ID -> Other -> Name: youtube-upload -> Create -> OK
  • Download JSON: Under the section "OAuth 2.0 client IDs". Save the file to your local system.
  • Use this JSON as your credentials file: --client-secrets=CLIENT_SECRETS

After that, you'll need to add the following event subscription to events.subscriptions in your config.yaml

events:
  subscriptions:
    - command: 'youtube-upload --client-secrets=my_client_secret.json --title="Timelapse: {gcode}" --description="Timelapse of {gcode}, printed and recorded via OctoPrint" --category="Science & Technology" --tags=OctoPrint "{movie}"'
      event: MovieDone
      type: system

You may need to write the absolute path to the credential file. If you want to make your movie unlisted, add --privacy unlisted after the tags, if you want to make it private, add --privacy private.

Send an email incl. a snapshot of the printed object upon print completion

Note: This receipt explains things for a Raspbian/OctoPi-based setup. It also assumes that you've got mjpg-streamer setup as explained here and listening on port 8080.

First you'll need to install a couple of new packages on your system in order to be able to send emails. Open an SSH session into your Pi (or alternatively open a terminal on it if it happens to be connected to monitor, mouse and keyboard). Then:

sudo apt-get install ssmtp mailutils mpack

Modify /etc/ssmtp/ssmtp.conf according to the settings needed to send mail via your mail provider. For Gmail it must look something like this:

AuthMethod=LOGIN
AuthUser=<your gmail address>
AuthPass=<your gmail password>
UseTLS=YES
UseSTARTTLS=YES
mailhub=smtp.gmail.com:587

Add the following line to your event subscriptions in config.yaml, substituting <your mail address> with your email address where you'd like to get the mail sent to.

- event: PrintDone
  command: curl -o /tmp/printDone.jpg "http://localhost:8080/?action=snapshot" 
    && mpack -s "Print of {file} finished" /tmp/printDone.jpg <your mail address>
  type: system

This will send you an email with a subject of "Print of <printed gcode file> finished" and a webcam snapshot of the webcam attached each time a print finishes.

If you want to have a certain email address as sender (a.e. to send mails to ifttt) you have to specify an alias in /etc/ssmtp/revaliases (for user pi):

 pi:email@mydomain.de

In /etc/ssmtp/ssmtp.confyou have to add:

 rewriteDomain=mydomain.de
 hostname=mydomain.de
 FromLineOverride=NO

(not sure about hostname, maybe it's ok just to have rewriteDomain)


Version for OctoPrint versions less than 1.1.0 to add to events > systemCommandTrigger > subscriptions:

- event: PrintDone
  command: curl -o /tmp/printDone.jpg "http://localhost:8080/?action=snapshot" 
    && mpack -s "Print of %(filename)s finished" /tmp/printDone.jpg <your mail address>

Push notifications for Android / iOS via Pushover

Pushover is an application that lets you push notifications to your Android or iOS device via a simple REST endpoint.

You'll need to register for an account and install the app. Then register a new 'application' at https://pushover.net/apps/build (name it 'Octoprint' or something you remember).

The event hook will just do a simple CURL request:

- event: PrintDone
  command: curl -s 
    -F "token=API_TOKEN" 
    -F "user=USER_TOKEN" 
    -F "message=Completed {file}" 
    -F "title=OctoPrint" 
    https://api.pushover.net/1/messages.json
  type: system

The printer will send a notification to all phones logged in to your account. If you've got a Pebble smartwatch, you can enable the app to relay the notification there too.


Version for OctoPrint versions less than 1.1.0 to add to events > systemCommandTrigger > subscriptions:

- event: PrintDone
  command:  curl -s 
    -F "token=API_TOKEN" 
    -F "user=USER_TOKEN" 
    -F "message=Completed %(filename)s" 
    -F "title=OctoPrint" 
    https://api.pushover.net/1/messages.json

Push notifications for Android / iOS via PushBullet

PushBullet is a similar, free alternative to pushover.

You'll need to register for an account and install the app. Then you can either make a new channel for your printer, send it to all devices, or just to one.

Go into your account settings and find the access token. If you wish to only send it to a certain device, find the iden token for that device. Else, if it is a channel, insert the channel tag. If you wish to send it to all your devices, don't fill in either and remove the lines from the call.

The event hook will just do a simple CURL request:

- event: PrintDone
  command: 'curl
            --header 'Authorization: Bearer YOUR_ACCESS_TOKEN_HERE'
            -X POST https://api.pushbullet.com/v2/pushes
            --header 'Content-Type: application/json'
            --data-binary ''{{"type": "note",
                             "device_iden": "YOUR_DEVICE_IDENTIFIER",
                             "channel_tag": "YOUR_CHANNEL_HERE",
                             "title": "OctoPrint", 
                             "body": "Completed {file}"}}'''
  type: system

Push notifications to iOS via Mail

Pushover is nice, but not free.

Set up a iCloud email address and use the following code to get instant email alerts to your iDevice, make sure your account is set to push.

- command: mail -s "3DPrint of {file} DONE!" YOUR_EMAIL@me.com < /dev/null
  event: PrintDone
  type: system

If you prefer a slightly quicker alert then change 'PrintDone' to 'Alert' and put a M300 command at the start of your end code.

(Make sure there are no tabs in the code)

Make sure you install sstmp and edit the config with your email account information. I use my gmail account. See "Send an email incl. a snapshot of the printed object upon print completion" above for instructions.


Version for OctoPrint versions less than 1.1.0 to add to events > systemCommandTrigger > subscriptions:

- command: mail -s "3DPrint of %(filename)s DONE!" YOUR_EMAIL@me.com < /dev/null
  event: PrintDone

Automatically start/stop mjpg_streamer via eventhooks

In config.yaml add the following commands to your subscriptions for PrintStarted and PrintDone to automatically start and stop mjpg-streamer during prints in versions newer than 1.1.0. This way the mjpg-streamer isn't running all the time as in the OctoPi implementation.

Note: This recipe explains things for a scratch Raspbian based setup and does not work directly with OctoPi. It also assumes that you've got mjpg-streamer setup as explained here and listening on port 8080.

events:
  enabled: True
  subscriptions:
  - event: PrintStarted
    command: mjpg_streamer -b -i "/usr/local/lib/input_uvc.so -n" -o "/usr/local/lib/output_http.so -w /usr/local/www"
    type: system
  - event: PrintDone
    command: killall mjpg_streamer
    type: system