If you have to do it in assembly, you have to use a combination of connections between variables and sensors: that is, a sensor that assumes values according to the position of the assembly, and then you have to connect the length of the spring to the variable associated with the sensor.