Magnet  

Magnetic sensor is common solution with doors.