Assumption: When someone enters the bathroom the motion detector will sense it and the corresponding HA entity (like @sparkydave, I’ll call it
binary_sensor.bathroom_motion, since you didn’t say what it was named) will go from ‘off’ to ‘on’.
It would seem to me you want:
- When someone enters the bathroom (and the motion detectors senses it), the light (I’ll call it
light.bathroom) should go on. This should not depend on the state of the door.
- When the motion sensor stops sensing motion, and if the door is open (I’ll call it
binary_sensor.bathroom_door, again, because you didn’t say what it was named, and
'on' means open), the light should go off. This is the case where someone goes into the bathroom, doesn’t close the door, and then leaves.
- When the door opens, the light should go off. This is the case where someone goes in the bathroom, closes the door, stays a while, then opens the door and leaves. In this case, it doesn’t matter if/when the motion sensor stops sensing motion.
If you think these events/conditions will work for you, then you could automate it like this:
- alias: Turn bathroom light on when someone enters
# Motion detector sensed motion
- alias: Turn bathroom light off when no motion and door open, or door opens
# Motion detector stopped sensing motion
- platform: state
# Door opened
- platform: state
# Door is open
The only likely scenario this doesn’t cover is if someone goes into the bathroom, doesn’t close the door, stays, but the motion sensor eventually stops detecting motion. In this case the light would go off, but I don’t see how that could be avoided. Except, maybe, you could add a timer to the mix and use it to make sure the light always stays on for a minimum amount of time.