Fix bugs with restarting switch if motion is detected soon after shutdown