Switch off after specified minutes after last movement

I want to keep door lock open as long as there’s movement during 15 minutes. So if I pass door now counter starts but if someone comes after 5 minutes it should start timer again from start.

I tried with this but realised that it doesn’t understand if someone comes after me.

  - alias: "Tallin sivu automaattilukitus kiinni"
    trigger:
      platform: state
      entity_id: binary_sensor.talli_sivuovi
      to: 'off'
      for:
        minutes: 25
    action:
      service: switch.turn_off
      entity_id: switch.talli_sivuovi_lukko

What would be solution ?

What sort of sensor is this?

Xiaomi door sensor.

If it’s not a movement sensor I don’t see how you can do this then:

Sorry, little bad explanation. When door is closed timer should start. If someone opens/closes door during this time, timer should start again.

What you have shown above will do that. The sensor has to be off for a continuous period of 25 minutes before it will trigger. Opening the door will reset that 25 minutes.

I think this do not work. I tried few times and it always closes lock after 25 minutes even I open door during this time…

I have many light movement sensors that depend on it working this way. It does work for me. This will not turn the lights off until there has been no movement for 3 minutes, moving around in front of the sensor keeps the light on for longer than 3 minutes:

- id: stair_lights_auto_off
  alias: 'Stair Lights Auto Off'
  initial_state: true
  trigger:
  - platform: state
    entity_id: binary_sensor.pir_stairs
    to: 'off'
    for:
      minutes: 3
  action:
  - service: light.turn_off
    entity_id: light.lifx_bottom_of_stairs

Let’s see if we can work out why it is not working for you.

Can you look at the developer tools states menu (on a phone would be handy) and see what states your door sensor returns as you open and close the door?

Yes, I checked and its on/off. Indeed I have same kind on solution with motion sensor indoor lights and it works correct. This is very weird now…

Is the second person closing the door behind them. If not the state will stay ‘on’.

I rebooted my NUC, xiaomi gateway, checked that everything is certainly up and running. Changed time for two minutes and tried - now it works! Maybe I had some kind of problem with Xiaomi or HA or… Now I need to test tomorrow with longer time if it still is ok. Thank for your patience!!!

1 Like

Still problem with this one. I can see that if I use short times ( below 20 minutes ) this works okay. But for some reason it closes automatically after 20 minutes no matter is there movement or not. I have checked my automations many many times and can’t find reason. Lock is powered with Wemos D1 pro and flashed with Tasmota 6.6.0 firmware. I have checked logbook and it just says switch.talli_sivuovi_lukko turned off. No automations before that. Also checked that there isn’t any timers in Tasmota. What could be problem ?

Tom’s automation should work.
But as you are having difficulty you may just want to try a different formulation of the same thing : -

- id: stair_lights_auto_off
  alias: 'Stair Lights Auto Off'
  initial_state: true
  trigger:
  - platform: state
    entity_id: binary_sensor.pir_stairs
    to: 'off'
  action:
  - delay: '00:30:00'
  - service: light.turn_off
    entity_id: light.lifx_bottom_of_stairs

This will not be the final form, we’d have to have the delay and turn off in a script called by this automation and we’d have to have an automation from the turn on cancel the script.
But let’s see if this works first

Just a word of warning, given that his use case is about locking the door I wouldn’t suggest relying solely on a 30 minute delay action. 30 minutes is a pretty big window and if HA happens to be restarted, updated, power loss, host update/reboot, etc. during that window then the delay will just be killed. Then since the state change to off already happened the door will stay unlocked until someone goes and locks it manually.

I don’t really know why the for option is not working since it seems like it should be working exactly as @tom_l describes. But if you do have to fallback and use a delay in conjunction with script.turn_off I would suggest also putting a second trigger that does this as well:

- platform: homeassistant
  event: start

Just to make sure the door doesn’t accidentally get left unlocked.

Also if you want, instead of a delay you could use a timer. Its basically the same thing as a delay except you could just do one timer.start service call instead of a script.turn_off followed by a script.turn_on. Also you could show the remaining time in your UI if you wanted to.

[EDIT] Forgot how triggers work for a second, adjusted to specify adding a second trigger to the same automation instead of a second automation which is unnecessary.

Thank you both. I need to try these. I can’t understand why it works with shorter delay’s but not over 20 minutes. Really confusing… but will test these tomorrow.

Good points well made.
And we are just testing case here.
The usage from the descriptions does not seem to be a domacile but a place of work.
I agree generally about locks (I won’t have any) but in this case it seems to be about limiting access rather than protection.
If I restart HA for any reason I always consider everything that ‘could’ have been running (but then again I am a very flawed human being :rofl: and make lots of mistakes ).
When leaving such a place of work I would assume additional manual locks and checks are observed.
But I also endorse the locking on restart (not sure the employee’s would though) but safe than sorry etc.

Try to put on for 00:20:00 instead of minutes: 25. It worked for me

I finally found reason. I have old Domoticz installation running in background ( for reporting services ) and had totally forgot that I have there 20 min timer :flushed: So it always locked door regarding my setup in HA. I think this is now solved, at least I hope so. Can’t test because my Xiaomi gateway messed up somehow ( locked ports ) and cannot get ports open with soldering and sending commands. I will get smartthings hub on Monday or Tuesday from my company. Can test this after that. But I’m quite positive first code will work as it works in my lobby also with motion sensor and lights. Sorry about bothering you…

And now when I finally got sensor work via Smartthings I can confirm that code really works!