Put your rotation inside an if statement with the condition being your sensor is not made. Then check the box on the if statement that says check condition constantly. This turns the if statement into a while statement. Immediately follow this if with a script command stopj(8) 8 just being the acceleration to use in radians/sec/sec for the stop move. Then you can capture the angle of all the joints by using get_actual_joint_position(). This will create a list variable. If you then want to use any of the joint angles later you can either reference them with index notation or you can store a specific index location to a variable. Wrist 3 is index position 5 in the list.
You might want to put a small wait after the stop, I generally use 100 milliseconds to give the system time to stabilize. If you need to be real accurate you can use a wait command and call the function is_steady() as the wait condition.
jointPositions = get_actual_joint_positions()
wrist3Position =jointPositions[5]
I am currently toying with the idea of rotating a part in front of a laser sensor and when the sensor goes high I would like to save the position of wrist 3 then reference it in another point in my program. Or even save it in reference to a plane. I have tried a loop structure and if...else with no success. I am working with UR5 with Robotic 2-finger 85.