Smart lock and door contact - lock close automation

Hey
I want lock my Maindoor after 30 minutes
The problem with my simple automation:
what if then door open and close, and the open and close again x10?
The auomation going to run twice and make lock close x10 ?

alias: maindoortest2minute
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.0x00158d0004184b4e_contact
    from: "off"
    to: "on"
condition:
  - condition: state
    entity_id: binary_sensor.0x00158d0004184b4e_contact
    state: "on"
action:
  - delay:
      hours: 0
      minutes: 30
      seconds: 0
      milliseconds: 0
    enabled: true
  - device_id: d7c8264e44e76869f1acb9e3fd5f7682
    domain: cover
    entity_id: cover.lock
    type: close
mode: single

What I think you want to do is create an entity in the Helper Templates.

Go to Devices/Services and select Helper tab. Create a Timer helper with your door contact sensor and 30 minute countdown, then use that in your automation to trigger the lights.

The Timer should reset every time the contact sensor goes off so you won’t have repeating automations going off.

I’m not sure how the automation need to be

did you can share the code ?

Here is an example code I made for my door and my couch light:

alias: Door close light off
description: ""
trigger:
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.smartlock_timer
    id: Timer Finished
  - type: not_opened
    platform: device
    device_id: xxxxxx
    entity_id: binary_sensor.u_bolt_pro_z_wave_smart_lock_current_status_of_the_door
    domain: binary_sensor
    id: Door closed
  - type: opened
    platform: device
    device_id: xxxxxxx
    entity_id: binary_sensor.u_bolt_pro_z_wave_smart_lock_current_status_of_the_door
    domain: binary_sensor
    id: Door Opened
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: Door closed
        sequence:
          - service: timer.start
            data:
              duration: "30"
            target:
              entity_id: timer.smartlock_timer
      - conditions:
          - condition: trigger
            id: Door Opened
        sequence:
          - service: timer.cancel
            data: {}
            target:
              entity_id: timer.smartlock_timer
      - conditions:
          - condition: trigger
            id: Timer Finished
        sequence:
          - type: turn_off
            device_id: xxxxxxx
            entity_id: light.couch
            domain: light
mode: single

You can replace the devices above and timer helper with your entities, although you can do all of this through the UI as well. You can follow this and it should lead you right to where you need to be.

https://www.youtube.com/watch?v=usg8cB8sd8E

This is also preferable as the Timer Helper will preserve the countdown after a power failure/reboot/etc.

Always preferable to the simple timers in automation.