Searching for a beeter Way (rotary encoder sets binry sensor)

Hi,
i want to set the binary sensor for an feedback Cover.
for now i use this code, but with delay the state alwayr changes regardles if the rotary encoder is still moving.
Someone knows a better way to archiv this ?

Thank you very much

sensor:
  - platform: rotary_encoder
    name: "Rotary Encoder"
    pin_a: D2
    pin_b: D4    
    on_clockwise:
      - logger.log: "Turned Clockwise"
      - lambda: |-
          id(open_movement_binary_sensor).publish_state(true);
          id(close_movement_binary_sensor).publish_state(false);
      - delay: 200ms
      - lambda: |-
          id(open_movement_binary_sensor).publish_state(false);
          id(close_movement_binary_sensor).publish_state(false);         

    on_anticlockwise:
      - logger.log: "Turned Anticlockwise"
      - lambda: |-
          id(open_movement_binary_sensor).publish_state(false);
          id(close_movement_binary_sensor).publish_state(true);
      - delay: 200ms
      - lambda: |-
          id(open_movement_binary_sensor).publish_state(false);
          id(close_movement_binary_sensor).publish_state(false);  

Ok i found an suitable way:
just want to share…

sensor:
  - platform: rotary_encoder
    name: "Rotary Encoder"
    id: encoder
    pin_a: D2
    pin_b: D4       
    on_clockwise:
      - script.execute: set_opening
    on_anticlockwise:
      - script.execute: set_closing

script:
  - id: set_opening
    mode: restart
    then:
      - lambda: |-
          id(close_movement_binary_sensor).publish_state(true);
          id(open_movement_binary_sensor).publish_state(false);
      - delay: 300ms
      - lambda: |-
          id(close_movement_binary_sensor).publish_state(false);
          id(open_movement_binary_sensor).publish_state(false);      
  - id: set_closing
    mode: restart
    then:
      - lambda: |-
          id(close_movement_binary_sensor).publish_state(false);
          id(open_movement_binary_sensor).publish_state(true);
      - delay: 300ms
      - lambda: |-
          id(close_movement_binary_sensor).publish_state(false);
          id(open_movement_binary_sensor).publish_state(false);