V1: PRINT_END

This does not use conditionals.

[gcode_macro PRINT_END]
gcode:
    # Get Boundaries
    {% set max_x = printer.configfile.config["stepper_x"]["position_max"]|float %}
    {% set max_y = printer.configfile.config["stepper_y"]["position_max"]|float %}
    {% set max_z = printer.configfile.config["stepper_z"]["position_max"]|float %}

    # Check end position to determine safe directions to move
    {% if printer.toolhead.position.x < (max_x - 20) %}
        {% set x_safe = 20.0 %}
    {% else %}
        {% set x_safe = -20.0 %}
    {% endif %}

    {% if printer.toolhead.position.y < (max_y - 20) %}
        {% set y_safe = 20.0 %}
    {% else %}
        {% set y_safe = -20.0 %}
    {% endif %}

    {% if printer.toolhead.position.z < (max_z - 2) %}
        {% set z_safe = 2.0 %}
    {% else %}
        {% set z_safe = max_z - printer.toolhead.position.z %}
    {% endif %}

    # Commence PRINT_END
    M400                             ; wait for buffer to clear
    G92 E0                           ; zero the extruder
    G1 E-14.5 F360                   ; retract to reduce oozing
    G91                              ; relative positioning
    G0 Z{z_safe} F3600               ; move nozzle up
    G0 X{x_safe} Y{y_safe} F20000    ; move nozzle to remove stringing

    M104 S0                          ; turn off hotend
    M140 S0                          ; turn off bed
    M106 S0                          ; turn off fan
    G90                              ; absolute positioning
    G0 X{max_x / 2} Y{max_y} F3600   ; park nozzle at rear

    # reset PA
    SET_PRESSURE_ADVANCE ADVANCE=0.0500

    M117 Fini, Lights Off!
    SET_FAN_SPEED FAN=daylights SPEED=0.40