Stopping tangled 3D prints with Home Assistant and System One models

Sometimes a 3d print that's been running stably for 30 minutes still ends in a blob of spaghetti. Here's how I wired up Home Assistant with a AI models to catch tangles early.

Stopping tangled 3D prints with Home Assistant and System One models
Photo by Kier in Sight Archives / Unsplash

A few years back I upgraded to a Bambu Lab P1P 3D printer from my Ender 3 Pro. The latter, a printer that required manual calibration and constant panic checks to make sure the print wasn't messing up and stuck to the build plate. The P1P fixes that … most of the time...

Sometimes a print that's been running stably for 30 minutes will still manage to get tangled and result in a blob of PLA filament spaghetti. What caused it can be different things: I was in a rush and didn't check the slicing settings properly before sending it to the printer, or I printed something that should've had a brim to stick to the plate properly, or objects close to each other unstick from the plate and get molten together accidentally.

Anyway, I often discovered tangles, after disaster had struck, and my printer had continued to print a failed print for the last hour. Since it now lives in another room than my office, I don't have a constant eye on it.

But then I realised that with the power AI this is fixable. Multimodal LLMs or vLLMs and the recent System One (aka decision) AI models combined, I could observe the Bambu Lab's camera and detect if a print gets messed up, to notify me in a timely manner and wire all of that up via Home Assistant.

Getting the camera feed


🔔
For Bambu Lab printers: you'll need the HA Bambu Lab custom integration from the Marketplace. I prefer to use it in LAN Mode to not use Bambu's cloud.

Once you have your 3D printer connected through an integration, you will need its camera feed. The HA Bambu Lab integration exposes the ~5fps camera feed from the printer directly. Slow, but it will do for our purpose.

Setting up the AI task


With the camera connected, it's time to set up an AI integration to properly wire up a model to analyse the camera feed. Home Assistant has split up AI capabilities into different AI Tasks, with the Evaluate task being our interest here.

I went with HA's OpenAI integration (see the Open Ends at the end of the post for why). Go to Settings > Integrations, add the OpenAI integration and its credentials if you haven't already, and once set up, add an AI task evaluation.

The Evaluate task model set up via OpenAI. At this time, only gpt-6-luna was available (which isn't officially a System One model, but just a regular LLM)

Creating a detection script


I wanted to be able to run my tangle detection both when something is printing and ad hoc, whenever I wanted to manually check without going to the printer. That last option lets me run detection when I'm not home and can't physically run to the printer, but want to quickly check, triggered via e.g. an Apple Shortcut. So I created a new Script in Home Assistant with the following sequence:

  1. ❓ Check if the printer is running. Otherwise, performing the vision and evaluate task only costs money without any purpose.
  2. ✨ Run the Evaluate task with a prompt to check for tangles against the P1P's camera feed.
  3. 🚩 Check if the returned noul response (basically 'the probability') is above my threshold. I picked 80% since it's quite a simple image view and check.
  4. 📱 If the print was determined as tangled, take a snapshot and send it straight to my devices as a notification, with the option to stop the print from the notification.
The full Script sequence for checking tangles & failed prints

Or in YAML, so you can copy/paste and adapt:

alias: '3D Printer: Check for tangle'
description: Uses AI vision and eval to check if a print got tangled
icon: mdi:message-alert
sequence:
  - variables:
      probability_threshold: 0.8
  - condition: state
    state:
      - running
    entity_id: sensor.thib_s_bambu_lab_p1p_print_status
    enabled: true
  - action: ai_task.evaluate
    data:
      entity_id: ai_task.openai_evaluation
      task_name: check_tangle
      attachments:
        - media_content_id: media-source://camera/camera.thib_s_bambu_lab_p1p_camera
          media_content_type: image/jpeg
      questions:
        is_tangled:
          type: noul
          instructions: >
            Check if the running 3d print is messed up. Check for tangles,
            objects loose from the build plate or alignment shifts.
    response_variable: eval_response
  - condition: template
    value_template: '{{ eval_response.answers.delivery_driver.noul >= probability_threshold }}'
    alias: The probability of tangling is high
  - action: camera.snapshot
    target:
      entity_id: camera.thib_s_bambu_lab_p1p_camera
    data:
      filename: /media/notification_snapshots/3d_printer.jpg
  - action: notify.thibault_devices
    data:
      message: Something went wrong with the 3D print.
      data:
        image: /media/local/notification_snapshots/3d_printer.jpg
        actions:
          - action: 0b8ec386_stop_printer
            title: Stop printer
      title: 🍶 3D Print failed!

Auto checking prints via the automation

Then an automation wires it up to run on an interval and handle the action to stop printing. Every 7 minutes the automation is triggered, and only if the printer is still running will it call the script to run detection.

I've also added an additional condition to only run the script when the progress of the print is below 80%, as I've noticed prints that exceeded that always printed fine in the end. Tangles happen mostly during the beginning phases (0–40%) for objects that aren't tall.

And then another trigger is set up to handle the mobile app action being fired and stop the print.

And again YAML in full for your convenience:

alias: 3D printer auto check for tangle
description: >-
  Every x minutes while the 3D printer is printing and progress is below 80%,
  run the tangle-check script to detect filament issues early.
mode: single
triggers:
  - trigger: time_pattern
    minutes: '7'
    id: generated-Giwu
  - trigger: event
    event_data:
      action: 0b8ec386_stop_printer
    event_type: mobile_app_notification_action
    id: generated-6Rlw
conditions:
  - condition: state
    entity_id: sensor.thib_s_bambu_lab_p1p_print_status
    state:
      - running
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - generated-Giwu
          - condition: numeric_state
            entity_id: sensor.thib_s_bambu_lab_p1p_print_progress
            below: 80
        sequence:
          - action: script.3d_printer_check_for_tangle
      - conditions:
          - condition: trigger
            id:
              - generated-6Rlw
        sequence:
          - action: button.press
            target:
              entity_id: button.thib_s_bambu_lab_p1p_stop_printing

Voilà! I wish prints would just run tangle-free, but at least now I can catch mistakes very early on and stop the printer.

Open ends


Long prints & costs

I haven't tested this on very long prints. For a print of ~1h30m at the 7-minute snapshot interval, that's 12 snapshots. With GPT-6 Luna priced at $0.10 input and $0.50 output, for a simple low-resolution image feed with a small prompt like this, it's going to cost me something, but I'm not too worried about it being expensive.

Not going with OpenAI

Unfortunately, not all AI integrations in HA support the Evaluate task (yet?). I would've liked to use LiteLLM here to wire up my own models (like an Ollama Cloud one) in the back, but the LiteLLM integration only offers options for adding a conversation agent and an STT agent.

Update: The december release of HA made it possible to have the Evaluate task in LiteLLM :)

Moving from notification only to voice

I am planning to expand this to detect if I'm sitting in my office (and not in a meeting) and then skip the notification and instead just use Assist and announcements on my smart speakers or HA Voice PE to tell me something went wrong instead.