Automation for the door lock

Hi, all
I am trying to set up following automation:
After 9pm HA should check every 5 minutes if the Door Lock is “locked”. In case the door is not locked HA waits 3 minutes and then locks the door.

Here is the code:

alias: Lock Door after 9pm
description: ""
trigger:
  - platform: time_pattern
    minutes: "5"
condition:
  - condition: time
    after: "21:00:00"
    before: "23:59:00"
    weekday:
      - sun
      - mon
      - tue
      - wed
      - thu
      - fri
      - sat
action:
  - if:
      - condition: device
        device_id: 7b0874ceb3d0767e517d60d5fe8b356d
        domain: lock
        entity_id: lock.front_door_lock
        type: is_locked
    then:
      - stop: ""
    else:
      - delay:
          hours: 0
          minutes: 3
          seconds: 0
          milliseconds: 0
      - device_id: 7b0874ceb3d0767e517d60d5fe8b356d
        domain: lock
        entity_id: lock.front_door_lock
        type: lock
mode: single

I am sure I am missing something very simple. Appreciate any help on the matter.

If the lock is left unlocked for at least 3 minutes between 21:00 and 23:59, the following automation will lock it.

alias: Lock Door after 9pm
description: ""
trigger:
  - platform: state
    entity_id: lock.front_door_lock
    from: 'locked'
    to: 'unlocked'
    for:
      minutes: 3
  - platform: time
    at: '21:00:00'
condition:
  - condition: state
    entity_id: lock.front_door_lock
    state: 'unlocked'
  - condition: time
    after: "21:00:00"
    before: "23:59:00"
    weekday:
      - sun
      - mon
      - tue
      - wed
      - thu
      - fri
      - sat
action:
  - service: lock.lock
    target:
      entity_id: lock.front_door_lock
mode: single

There’s no need to poll the lock’s state every 5 minutes because the State Trigger is responsible for detecting when the lock is left unlocked for 3 minutes. In other words, between 21:00 and 23:59, no matter how many times you leave the lock unlocked, within 3 minutes the automation will lock it.


EDIT

Correction. Removed line left over from the original automation.

Thank you.
Will give it a try. So far the only issue is with the
type: is_locked
Message malformed: extra keys not allowed @ data[‘action’][0][‘target’][‘type’]
I have removed that part and will test it tonight.

Remove that line. It’s part of your original automation and I left it there by mistake.

I have removed it from the example posted above.

Great, thank you.