The problem of determining if a room is occupied using just a motion detector and door contact has proved a tough nut to crack, and even this solution won’t handle a situation where someone opens the door briefly while the room is occupied (suggestions welcome).
I live in a rented house where the downstairs toilet door doesn’t lock. This in itself hasn’t proved a problem as we have young children. However when guests come over it can be problematic. I have a light strip outside the room in sync with the light in the toilet room, which only has a small window so the light is required whatever time of day it is.
This solution covers the following scenarios
- Initial door closed, door closed while occupied, door closed on exit
- Initial door closed, door open while occupied, door closed on exit
- Initial door closed, door closed while occupied, door open on exit
- Initial door closed, door open while occupied, door open on exit
- Initial door open, door closed while occupied, door closed on exit
- Initial door open, door closed while occupied, door open on exit
- Initial door open, door open while occupied, door closed on exit
- Initial door open, door open while occupied, door open on exit
I had previously tried to solve this issue using Hubitat Elevation Motion & Mode Lighting App with the addition of a Rule Machine rule to turn the light off if there was not motion on door close after 10 seconds. This proved unsatisfactory and unreliable.
This solution owes much to @TRDeadbeat and his No touch bathroom fan and light control piston, on which it was based.