Generate Timelapse Videos of Your 3D Prints

Hi Guys!

Are you tired of staring at your 3D printer, waiting for it to complete a print? :sleeping: Well, fear not my friends! With the power of Homeassistant, you can create a timelapse of your 3D printer as it prints your masterpiece. :art:

So, here’s the deal: we’re going to create an automation that triggers whenever your 3D printer completes a print or goes into an error or paused state. :rotating_light: Also every 30 seconds, we’ll snap a photo of the ongoing print and store it in the Media folder of your ESP32-Cam. :camera_flash:

Once the print job is finished or has failed, we’ll trigger the do_movie command that will run a shell script to create a timelapse video using all the photos of the print.

So, come on, let’s create the ultimate 3D printing timelapse and show it off to the world! :rocket::man_astronaut::earth_africa:
automation-config:

alias: 3D Printer Timelapse
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.creality_k1_current_print_state
    from: printing
    to: complete
    id: printing_completed
  - platform: state
    entity_id:
      - sensor.creality_k1_current_print_state
    from: printing
    id: printing_paused
    to: paused
  - platform: state
    entity_id:
      - sensor.creality_k1_current_print_state
    from: printing
    id: printing_error
    to: error
  - platform: time_pattern
    seconds: /30
    id: every_30th_second
condition: []
action:
  - if:
      - condition: trigger
        id:
          - every_30th_second
      - condition: state
        entity_id: sensor.creality_k1_current_print_state
        state: printing
    then:
      - service: camera.snapshot
        metadata: {}
        data:
          filename: >-
            /media/esp32cam/{{
            states('sensor.creality_k1_filename')[:-6].replace('.', '_') }}/{{
            now().strftime("%Y%m%d-%H%M%S") }}.jpg
        target:
          entity_id: camera.esphome_3dprintcamera_keller_esp_cam
  - if:
      - condition: trigger
        id:
          - printing_completed
          - printing_error
    then:
      - service: shell_command.do_movie
        metadata: {}
        data: {}
mode: parallel
max: 3

shell-command:

do_movie: '/bin/bash /config/timelapse.sh'

timelapse.sh

#!/bin/bash

# Set the path to the Media folder
media_path="/media/esp32cam"

# Iterate through each subdirectory in the Media folder
for subdir in $media_path/*/; do
  echo "Creating timelapse for directory: $subdir"
  # Extract the folder name
  folder_name=$(basename "$subdir")
  # Create the timelapse video with the folder name as the filename
  ffmpeg -pattern_type glob -i "$subdir/*.jpg" -c:v libx264 -r 40 -crf 28 -preset slow -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2,format=yuv420p" -color_range 2 -movflags +faststart "$media_path/$folder_name-timelapse.mp4"

  # Delete the source JPEG files and the directory
  #rm -rf "$subdir"
done

Cheers :slight_smile:

Footnote: Just a heads up, to create a timelapse for your 3D printer using Homeassistant, you’ll need to become best friends with two key players: the ESP32Cam (using ESPHome) and a Moonraker API (for Klipper firmware). Think of it like a dynamic duo saving the day, but instead of capes, they wear circuit boards and wires. And if you’re a proud owner of a Creality K1, then you’ll definitely need to cozy up to the Moonraker API.