@toxs
There is an easier way...
Your are trying to calculate a sensor trigger height, but mechanically it does not change at all....
Use the endstop to set the Z0 datum
Use the sensor to align the print bed or generate the mesh.
First get Z0 at the end stop, then use the sensor to align the bed, after that get Z0 again at the end stop.
Do the same procedure to generate the bed mesh.
There are some videos on youtube on how to do this.