Skip to content

Instantly share code, notes, and snippets.

@steveneaston
Created January 31, 2025 12:31
Show Gist options
  • Save steveneaston/174b33a056f3b3158781bdd6488a799b to your computer and use it in GitHub Desktop.
Save steveneaston/174b33a056f3b3158781bdd6488a799b to your computer and use it in GitHub Desktop.
Move a Bambu Lab print head and bed in Home Assistant

Setup

If you haven't already, install Bambu Lab from HACS.

  1. (Optional) Create an "Input Select" Helper with options 1, 30, 50. This is used as a default movement distance.
  2. Create a new Script, switch to YAML mode and paste the contents of bblab_move_axis.yaml.
  3. If you haven't created the input select helper, update the distance variable to {{ iif(is_number(distance), distance, 10) }} so that the default movement is 10mm
  4. Update the sensor IDs throughout the script to your own device, e.g. sensor.p1s_print_status

Usage

You can either call the script as part of an automation, or add a button. The only variable requirement is direction (up, down, left, right, forward, back).

Example template button:

# Move the print head backwards on the Y axis by 50mm (i.e. towards the rear of the machine)
type: custom:mushroom-template-card
primary: ""
secondary: ""
icon: mdi:arrow-up-thick
tap_action:
  action: perform-action
  perform_action: script.p1s_move_axis
  data:
    direction: back
    distance: 50
grid_options:
  columns: 2
  rows: 1

Caution

This has only been tested on a P1S, it won't work on an A1 or A1 Mini. I also haven't tested this when there's multiple printers configured.

sequence:
- variables:
distance: >-
{{ iif(is_number(distance), distance,
states('input_select.axis_move_distance')) }}
- condition: or
conditions:
- condition: state
entity_id: sensor.p1s_print_status
state: failed
- condition: state
entity_id: sensor.p1s_print_status
state: idle
- condition: state
entity_id: sensor.p1s_print_status
state: finish
- condition: template
value_template: >-
{{ direction in ['up', 'down', 'left', 'right', 'forward', 'back']
}}
- condition: template
value_template: "{{ distance > 0 and distance <= 50 and distance % 1 <= 0 }}"
- variables:
axis: >-
{{ iif(direction in ['up', 'down'], 'Z', iif(direction in ['forward',
'back'], 'Y', iif(direction in ['left', 'right'], 'X', ''))) }}
distance: "{{ distance * iif(direction in ['up', 'left', 'forward'], -1, 1) }}"
speed: "{{ iif(direction in ['up', 'down'], 1500, 3000) }}"
- action: bambu_lab.send_command
data:
command: |
M211 S
M211 X1 Y1 Z1
M1002 push_ref_mode
G91
G1 {{ axis }}{{ distance }}.0 F{{ speed }}
M1002 pop_ref_mode
M211 R
enabled: true
alias: P1S Move Axis
description: ""
icon: mdi:axis-arrow
fields:
distance:
selector:
number:
min: 1
max: 50
step: 1
name: Distance
default: 10
required: true
direction:
selector:
select:
options:
- label: Up
value: up
- label: Down
value: down
- label: Left
value: left
- label: Right
value: right
- label: Forward
value: forward
- label: Back
value: back
multiple: false
mode: dropdown
name: Direction
required: true
default: down
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment