Controling of bathroom light by using Magnetic Switch (Sensor)
As mentioned, constructing something from loose parts is often a local code violation. The common solution to this issue now is easier to implement, it's called an "occupancy sensor" that is built into the switch. it uses Passive Infra-Red (PIR) sensors that detect if a human (or animal) is in the room giving off heat and moving. They will have a timer built-in as well so that if you don't move for a few minutes, it doesn't go dark on you. Search for a "PIR occupancy sensor" that is available wherever you are located.