Hm. The whole sequence of movements, plus M400 and G4, looks good to me.
Maybe double and triple check that the microswitch is really pressed correctly every single time? Or check if the cable from the switch to the boards runs close to other cables, where it could pick up noise. No idea how sensitive it would be to noise, but checking it and trying to move it away from other cables can't hurt.
Besides that, I have no more ideas than what I've suggested earlier: try writing a gcode file that simulates the tool change without actually invoking the T command. Just grab the tool, then release it again, in a while loop, and see what happens. In my experience, having a minimal example that reproduces the issue often helps.