I highly recommend getting the code out of the slicer and into macros. If you had your “Machine End g-code” as a macro instead of embedded in the slicer, you could simply make the change I’m about to tell you and any future parts you print would follow the new code. Instead, you’re going to have to make the adjustment in your slicer and then re-slice. It’s not too late to copy all of your end code to a macro and then delete it from the slicer, replacing it with the name of the macro, most likely END_PRINT … but I do recommend starting by finding the examples codes and copying them, then copying the code block from the slicer into the example macro. It’s not necessary, just more convenient for making changes in the future. Anyway … Moving on to what the problem actually is:
It’s not your homing that’s the issue, so testing the homing isn’t going to help. It’s a code your slicer added at the very end of the print. I had this happen to me to when I was first setting up my END_PRINT macro and wasn’t entirely sure what I was doing, so I recognized it immediately from your description. In your end code there is a line that says “G1 Z5 F5000 ; lift nozzle” … except it’s actually NOT lifting the nozzle. It’s trying to move to (absolute value) Z5, because the part was printing using absolute movement settings and when the part ended nothing switched it into relative movement. You need to put a “G91 ; set movement to relative” (the comment is not needed, but I like to add it to keep everything organized) before the “G1 Z5” so it will actually move the Z axis +5mm (relative) from wherever it currently is when the part finishes, instead of trying to go to (absolute) Z5. G90 will put it back to absolute movement, but you don’t need it in this case because the G28 X will override it anyway.
Not part of the problem, but is there a reason you do not also move the Y, either to the maximum value if you have a bed-slinger or to the minimum value (aka home) if you have the kind that the gantry itself moves in the Y? It’s not truly necessary, but I find it convenient.