Gcode_macro MEASURE_AND_SET_Z_ZERO

I am sharing a macro developed by a very helpful member here ( gaolst):

gcode_macro MEASURE_AND_SET_Z_ZERO

What this macro does is probe the bed in the center only, with a BL Touch prior to each print job; it is embedded in my START_PRINT macro (it does not create a bed mesh) .

I have it set to probe the bed 10 times then average the results, once bed and nozzle are at temp, the print begins.

I have this running on a delta printer using a BL Touch probe. I wanted this macro to verify bed height before printing because I would get Z-height drift and have to baby-step at the start of each print. this macro eliminates that. This macro can be modified to use different probe type such as a micro switch.

Here is the macro:

[gcode_macro MEASURE_AND_SET_Z_ZERO]
gcode:
  G90
  #pre position toolhead for probing
  G0 X0 Y0 Z80 F2000
  PROBE
  QUERY_PROBE
  ALIGN_PROBE_TO_Z_OFFSET

[gcode_macro ALIGN_PROBE_TO_Z_OFFSET]
description: Query last probe result and align Z height to configured probe.z_offset
gcode:
    {% set z_probe = printer.probe.last_z_result|float %}
    {% set z_offset = printer.configfile.settings.bltouch.z_offset|float %}
    {% set z_delta = z_probe - z_offset %}
    {% set z_position = printer.toolhead.position.z|float %}
    {% set new_z_position = z_position - z_delta %}

    {action_respond_info(
        "Probe result: %.2f \n"
        "Configured Z_offset: %.2f \n"
        "Delta: %.2f \n"
        "Current Z:%.2f \n"
        "New Z:%.2f " % (
            z_probe,
            z_offset,
            z_delta,
            z_position,
            new_z_position))}

    G92 Z{new_z_position}
    #SET_GCODE_OFFSET Z={z_delta}

Here it is referenced in my START-PRINT macro:

[gcode_macro START_PRINT]
gcode:
    # For PLA
    {% set BED_TEMP = params.BED_TEMP|default(70)|float %}
    {% set EXTRUDER_TEMP = params.EXTRUDER_TEMP|default(205)|float %}
    M140 S{BED_TEMP}
    # Use absolute coordinates
    G90
    # Reset the G-Code Z offset (adjust Z offset if needed)
    SET_GCODE_OFFSET Z=0.0
    # Home the printer
    G28
    # default bed mesh is PEI_BuildTak plate
    BED_MESH_PROFILE LOAD=default
    MEASURE_AND_SET_Z_ZERO
    # Move the nozzle near the bed
    G1 Z20 F3000
    # Wait for bed to reach temperature
    M190 S{BED_TEMP}
    # Set and wait for nozzle to reach temperature
    M109 S{EXTRUDER_TEMP}

And here are my printer.cfg BL Touch settings (BTT Pico board):

[bltouch]
sensor_pin: ^gpio22
control_pin: gpio29
pin_move_time: 0.680
stow_on_each_sample: True
probe_with_touch_mode: False
x_offset: 0.0
y_offset: 30.0
#z_offset: 1.090
speed: 3.0
lift_speed: 3.0
samples: 10
sample_retract_dist: 4.0
samples_result: average
samples_tolerance: 0.100

If you need help setting this up, feel free to reach out to me.