Efficient way to correct for Z-drift?

The microscope seems to have some problems staying where it should in the Z direction, so when I do timelapses I often won’t get very far as the image is completely out of focus, sometimes after 30 minutes, sometimes after 5 minutes.

Then I thought of doing a 3D timelapse over a certain range of Z values, so that I’ll always have a slice, somewhere in the middle, that has the right focus. However, the 3D drift correction plugin only seems to work on largely static samples, whereas I’m recording samples that dissolve, get dimmer and form holes.

So right now my workflow is, for each of 40 frames: find the best-looking image by changing the Z, exporting that as an image, and then make a new timelapse out of the 40 “best-Z” images.

Is there a faster way?

I’ve attached some images that show how the “correct” Z value slowly shifts to the right (that is, the lens drops). Ideally I’d like some sort of macro that could pick the image with the highest mean color value, corresponding to a specific Z slice for every point in time.