Sensor for automatically detecting three positions (P1, P2, P3) of a piston driving a gripper along a linear stroke path and for automatically assigning the three positions (P1, P2, P3) to three gripper positions, wherein in a first gripping position ("idle") no object is gripped, in a second gripping position ("object") an object is gripped and in a third gripping position ("no object") no object is gripped although it should have been gripped, characterised in that the sensor comprises - an input means for starting a teach-in process, - a memory, - an evaluation unit, - wherein the sensor is designed to detect the position of the piston magnetically or inductively over the entire stroke path via a permanent magnet arranged on the piston, - wherein the sensor is designed to detect a stopping of the piston during the teach-in process and to store the position (P1, P2, P3) of the piston during this stop in the memory, - and is further adapted to detect at this stop a direction of movement (positive; negative) from which this position (P1, P2, P3) was approached and to store it as a direction of movement associated with the position, - and the evaluation unit is adapted to stop the teach-in process when three different positions (P1, P2, P3) with the associated directions of movement (positive, negative) have been stored, - and the evaluation unit is further adapted to assign each of the three positions (P1, P2, P3) to one of the three gripper positions ("idle", "object", "no object") from the stored positions (P1, P2, P3) and the stored associated directions of movement (positive, negative) - and is further adapted to sort the positions along the stroke path according to size and to assign the second gripping position to the middle positions and to assign the third gripping position to that of the two remaining edge positions to which the same direction of movement as to the middle position is stored and to assign the first gripping position to the remaining edge position. - an input means for starting a teach-in process, - a memory, - an evaluation unit, - wherein the sensor is designed to detect the position of the piston magnetically or inductively over the entire stroke path via a permanent magnet arranged on the piston, - wherein the sensor is designed to detect a stopping of the piston during the teach-in process and to store the position (P1, P2, P3) of the piston during this stop in the memory, - and is further adapted to detect at this stop a direction of movement (positive; negative) from which this position (P1, P2, P3) was approached and to store it as a direction of movement associated with the position, - and the evaluation unit is adapted to stop the teach-in process when three different positions (P1, P2, P3) with the associated directions of movement (positive, negative) have been stored, - and the evaluation unit is further adapted to assign each of the three positions (P1, P2, P3) to one of the three gripper positions ("idle", "object", "no object") from the stored positions (P1, P2, P3) and the stored associated directions of movement (positive, negative) - and is further adapted to sort the positions along the stroke path according to size and to assign the second gripping position to the middle positions and to assign the third gripping position to that of the two remaining edge positions to which the same direction of movement as to the middle position is stored and to assign the first gripping position to the remaining edge position.