Bathroom lights app - motion and contact same app

Hi I am new to writing apps, I newly discovered writing apps, and how to get other apps from people in the ide, I was just wondering Is it possible to write a app that say is for the bathroom that uses a switch to turn the lights and fan off and on and then motion to turn the switch on and then off after 2 mins of no motion and then if you close it door it wont turn the lights off until 10 mins has passed without motion.

so
motion - lights on
no motion - lights off 2 mins later
door open and motion - lights on
door closed lights on for 10 mins until motion and stays on longer.