So you want to have a stopwatch that will be reset every time the sensor is activated…
You can achieve this using ewelink + https://sequematic.com
On the ewelink side you will need two scenes.
On the SM side you will need three sequences and pairing your ewelink account and one variable.
On the ewelink side, you need two scenes, one to start the countdown and the other to change the value of the variable so that there are no mistakes, let’s call it STOP.
On the SM side, we need three sequences, the first TIMER action which stops the time for a period of 2 minutes and then turns OFF the sonoff device.
The two remaining sequences are START and STOP to execute the TIMER sequence according to the STATUS value, which you also need to create.
The automation logic is quite simple… Add the “STOP” logic to the door sensor and the open variable. This way we clear the STATUS and avoid strange loops.
Add START logic to the closed sensor.
Door closed = START
Door open = STOP
The operation is as expected when the door is opened, the only thing that happens is the value in STATUS changes to STOP. When the door is closed, STATUS is set to START and the action is executed, paused for two minutes and the OFF command for the device you want is executed. Finally, the STATUS value is changed again to be sure.
The entire automation will always do what you want, i.e. it will wait two minutes from the last time the door is closed and only then will it turn off the device (light). This will work properly even if you interrupt the flow by opening and closing the door. It will always count two minutes from the last closing sequence.
I tested it and it seems to work, at least I didn’t have any problems.