I finally got the macro working with following code:
; Source: https://forum.duet3d.com/topic/4202/homing-to-z-max-when-also-using-a-z-probe/19 ; Start g-code: G28, M98 P"0:/macros/Leveling/SetZ" ; run z fine tuning ; If the printer hasn't been homed, home it ;M291 P"checking homing status" R"Homing status" S1 ; Report status if !move.axes[0].homed || !move.axes[1].homed || !move.axes[2].homed ; Check if axis are not homed ;If axis are not homed, home axis echo "Axis are not homed, performing homing" M98 P"0:/sys/homeall.g" else ;If axis are already homed, home only the X and Y axis echo "axis are homed, re-homing x and Y" M98 P"0:/sys/homex.g" ; Run the Home X macro M98 P"0:/sys/homey.g" ; Run the Home Y macro ;G1 X100 Y100 H0 F1800 ; move to be centre slowly. ;End homeing check M291 P"Axis homed" R"Homing status" S1 ; Report status ;echo "start fine tuning z-level" G1 X100 Y100 H0 F1000 ; move to be centre slowly. G1 Z15 H1 F8000 ; Raise the bed to start probing G30 ; probe for z=0 G1 Z10 H1 F1000 ; raise nozzle G30 S-1 ; check z gap G1 Z10 H1 F1000 ; raise nozzle M400 ; Wait until all moves are completedI changed and tested several different thing, but i think the trick that did it was removing the empty line between the end of the "If" block and the start of the "else" block.