Pay attention to hysteresis but for an example of how to do specifically what you are asking for see the antiflapping timer in Generic Presence Detection and Design Pattern: Motion Sensor Timer
Pay attention to hysteresis but for an example of how to do specifically what you are asking for see the antiflapping timer in Generic Presence Detection and Design Pattern: Motion Sensor Timer