This is one of these tricks that make animations of pistons easy. No xpresso, no math, nothing complicated.
To create a working piston, start by making 2 null objects, put one as a child of the “foot” in the bottom position of the piston and the other as a child of the “arm” object at the top position of the piston.
Add a target expression(tag) to both nulls and target them to the opposite null. That is it.
Now you can add cylinders and other objects to each null to create the actual piston.