Stop cover only within timeframe

Hi

I have a cover that just works on a single pulse from a contact.
I’m trying to make a template where the stop button only reacts after the binary_sensor has changed in the last 20 seconds. This means that the cover is still moving and I can be stopped… but the stop button isn’t working…
(if it’s longer then I wil close/open it again when I use switch.turn_on

      kelderpoort_sensor:
        friendly_name: "Kelderpoort"
        unique_id: kelderpoort_sensor
        value_template: "{{ is_state('binary_sensor.poort_kelder', 'on') }}"
        open_cover:
          - condition: state
            entity_id: binary_sensor.poort_kelder
            state: "off"
          - action: switch.turn_on
            target:
              entity_id: switch.kelder_poort
        close_cover:
          - condition: state
            entity_id: binary_sensor.poort_kelder
            state: "on"
          - action: switch.turn_on
            target:
              entity_id: switch.kelder_poort
        stop_cover:
          - condition: state
            entity_id: binary_sensor.poort_kelder
            state: "{{ now() - timedelta(seconds = 20) < states.binary_sensor.poort_kelder.last_updated }}"
          - action: switch.turn_on
            target:
              entity_id: switch.kelder_poort

Make it a template condition, not a state condition. The template returns true or false, while the state of the entity you are checking is on or off. That why it is never equal.

If you also want to test the direction, make that a separate condition.

You might even need an if else condition on the state of the sensor if the direction of the command needs to match the direction of movement.