Raise Z when I stop a job?

If you’ll study some of my macro code at the following link, you’ll find code for a safe Z move. Perhaps it will work in your case.

G53 G1 Z-0.8 ; SAFE RETRACT