The mesh is the default cube. I select the cube, and tab into edit mode, selected all "A", duplicated "SHIFT+D" and moved "G" up along the Z-axis "Z". Tab back into object mode.
I then added an armature "SHIFT+A" -> Armature -> Single Bone. I select the armature and tab into edit mode. I aligned the bottom of the bone to the bottom of the lower cube by use of the snap tool set to vertex. I then snap the top of the same bone to the top of the lower cube. I then extrude that bone "E" and snap it to the top of the upper cube.
I then name my lower bone "Bottom" and my top bone "Top.Rest". I then parent the "Top.Rest" to the "Bottom" bone and ensure it is "Connected". The last step then is I duplicate the "Top.Rest" bone and rename it "Top.Constrained".
The "Top.Rest" bone will act as the bone that I want the armature to return to when it is at rest. The "Top.Constrained" bone will be the bone that is constrained to track the target.
Now to finalize my setup, I need 2 control bones, the "Target" and the "Switch". They can be placed anywhere to fit your needs. The "Switch will be used to switch from the "Top.Constrained" to the "Top.Rest" and the "Target" is what the "Top.Constrained" is going to target to. "Target" then can be used to track an object later on. In this example they will be parented to the "Bottom" bone and not "Connected", though every good armature should have a "Root" bone and that makes a better parent for these controls. So I went back and added "Root" and parented "Bottom", "Control" and "Target" to it, and "Connected" "Bottom".
The initial setup is provided in the "Before.blend" so you can see more closely the initial setup.
First we are going to constrain our "Control" bone to limit it's movement. Looking closer at our "Control" bone, we can see its Y-axis is pointing along the worlds Z-axis and this is the direction we want to move our bone to exert the control switch. When it is in it's default position, we will have it create an "Influence" of 1 (i.e. the TrackTo constraint is "on"). When we move it "up" along it's Y-axis by 1 blender unit, we will have it turn "off". Note, we will move it along the bones Y-Axis and not the Worlds Z-axis because this will allow us to move and rotate the armature and not interrupt the constraint switch (i.e. keep it relative to the armatures space).
Now, on the "TrackTo" constraint, right click on the "Influence" bar and select "Add Driver". The Influence bar will turn purple to indicate it is now being driven. We now need to go setup our driver.
Scroll down a little further and you should see an "Add Variable" button. It's already added a variable "var" for us, so we don't need another. Here though we need to set our Ob/Bone to our "Armature" and "Control" bone.
Recalling now that we setup our control bone to move along its local Y-axis between 0.0 and 1.0, this is where we will use that. For the "Transform Channel" we set "Type" to "Y Location" and "Space" to "Local Space".
Influence = m * (Bone Y pos) + b
We have 2 equations:
We can quickly see that from equation #2, b=1 so we can solve for m using equation 1 and get m=-1. We use these in our equation then as shown.
Just add put the Mesh together with the armature (which I am not covering here how to do), vertex groups, weight paint, etc. to get it setup. The important thing to know here however is that want the mesh to be deformed using the "Top.Constrained" bone and not the "Top.Rest". So we will set the "Top.Constrained" to "Deform" and ensure our vertex groups use "Top.Constrained" as it's name and set "Top.Rest" to not be deforming and not have a vertex group for it.
One more thing to say, is we could have also added a constraint to "Top.Rest" to "Copy Transforms" of "Top.Constrained" and added the driver to "Copy.Transforms". In fact this would be more aligned with how IK/FK switches work in armatures, so this can be a good exercise to try and practice with.
Of course, here is the final blend.
Questions, comments or corrections, please feel free to leave a comment and I will work to fix as soon as possible. Thanks for reading.