Skip to content

Instantly share code, notes, and snippets.

@jenrik
Last active January 21, 2025 23:41
Show Gist options
  • Save jenrik/7213fb1c9910d846ba033ea72d571613 to your computer and use it in GitHub Desktop.
Save jenrik/7213fb1c9910d846ba033ea72d571613 to your computer and use it in GitHub Desktop.
AI Spaghetti detection for Bambu Lab P1 and A1 series printers using Home Assistant and Obico
blueprint:
name: 3D printer spaghetti detector
domain: automation
input:
printer_status:
name: Printer current stage entity
selector:
entity:
filter:
- domain: sensor
camera:
name: Printer camera
description: Camera monitoring the builtplate
selector:
entity:
filter:
- domain: camera
obico_url:
name: Obico ML API base url
description: Base url for the Obico ML API
homeassistant_url:
name: Home Assistant base url
description: Base url for your Home Assistant server
confidence_helper:
name: Confidence
description: Confidence of spaghetti presence
selector:
entity:
filter:
- domain: input_number
detection_threshold:
name: "Detection threshold"
description: "Number between 0 and 1"
selector:
number:
min: 0
max: 1
step: "any"
#unit_of_measurement: "%"
mode: slider
detection_duration:
name: "Detection duration"
description: "Duration for which confidence must be above threshold to trigger a pause of the printer"
selector:
duration:
pause_action:
name: Pause printer
selector:
action:
resume_action:
name: Resume printer
selector:
action:
notification_device:
name: Notification action
selector:
text:
prefix: "notify."
mode: single
trigger_variables:
entity_id: "{{ this.entity_id }}"
triggers:
- trigger: time_pattern
seconds: /5
id: periodic
hours: "*"
minutes: "*"
enabled: true
- trigger: numeric_state
entity_id:
- !input confidence_helper
for: !input detection_duration
above: !input detection_threshold
id: spaghetti
- trigger: event
event_type: mobile_app_notification_action
event_data:
action: "{{ 'RESUME-PRINT-' ~ entity_id }}"
id: action_resume
conditions:
- condition: or
conditions:
- condition: trigger
id:
- action_resume
- condition: state
entity_id: !input printer_status
state: running
- condition: state
entity_id: !input printer_status
state: printing
actions:
- if:
- condition: trigger
id:
- periodic
then:
- action: camera.snapshot
metadata: {}
data:
filename: "/config/www/tmp/spaghetti-do-detection_{{ this.entity_id }}.jpg"
target:
entity_id: !input camera
- variables:
ha_url: !input homeassistant_url
- action: rest_command.obico_ml_api
metadata: {}
data:
host: !input obico_url
image: "{{ ha_url }}/local/tmp/spaghetti-do-detection_{{ this.entity_id }}.jpg"
response_variable: resp
- action: input_number.set_value
metadata: {}
data_template:
value: >-
{% set confidence = (resp.content['detections'] | map(attribute=1) |
max) %}{% if confidence is defined %}{{ confidence }}{% else %}0{%
endif %}
target:
entity_id: !input confidence_helper
- if:
- condition: trigger
id:
- spaghetti
then:
- alias: "Run the action"
choose: []
default: !input pause_action
- action: camera.snapshot
metadata: {}
data:
filename: "/config/www/tmp/spaghetti-{{ this.entity_id }}.jpg"
target:
entity_id: !input camera
- variables:
notification_device: !input notification_device
- action: "notify.{{ notification_device }}"
metadata: {}
data:
message: Spaghetti detected in printer
data:
image: "local/tmp/spaghetti-{{ this.entity_id }}.jpg"
actions:
- action: "RESUME-PRINT-{{ this.entity_id }}"
title: "Resume"
- if:
- condition: trigger
id:
- action_resume
then: !input resume_action
@jenrik
Copy link
Author

jenrik commented Dec 4, 2024

Requires a rest_command to be configured as follows:

rest_command:
  obico_ml_api:
    url: "{{ host }}/p/?img={{ image }}"
    method: GET

@jenrik
Copy link
Author

jenrik commented Dec 4, 2024

Build and run the ml_api container from Obico:

git clone https://github.com/TheSpaghettiDetective/obico-server.git
cd obico-server/ml_api
docker buildx build --tag obico-ml-api:latest .
docker run --restart=unless-stopped -p 3333:3333 -e DEBUG=True -e FLASK_APP=server.py --name obico-ml-api obico-ml-api:latest bash -c "gunicorn --bind 0.0.0.0:3333 --workers 1 wsgi"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment