Trigger automation when a binary_sensor entity changes state two times within half a second

I would like to trigger an automation when a binary sensor changes state two times within the duration of one second. How can I implement this?

Context: I would like to dim a smart bulb when I toggle my detached light switch twice within half a second.

I believe the following automation is triggered when the binary_sensor entity changes state once:

alias: Dim office ceiling light
description: ''
mode: single
trigger:
  - platform: state
    entity_id: binary_sensor.switch_light_office_ceiling_input
condition: []
action:
  - service: light.turn_on
    data:
      brightness_pct: 1
    target:
      entity_id: light.office_ceiling_level_on_off

I don’t know how I can change the trigger to two state changes within the time frame of half a second?

found a solution: a trigger on the first state change, a delay of 500ms and then a check of the state change again. I used this to increase the brightness of a smart lightbulb stepwise when rapidly changing a smart wall switch from on to off back to on.

  trigger:
  - platform: state
    entity_id: light.office
    to: 'off'
    from: 'on'
  action:
  - delay:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: 500
  - condition: state
    entity_id: light.office
    state: 'on'
  - service: light.turn_on
    data_template:
      transition: '1'
      entity_id: light.office
      brightness: "{% set n = states.light.office.attributes.brightness\
        \ %} {% if n < 85 %}\n  85\n{% elif n < 170 %}\n  170\n{% elif n < 254 %}\n\
        \  254\n{% else %}\n  1\n{% endif %}\n"

on double toggle, it increases the brightness in steps of 33% and when triggered at full brightness it goes back to the lowest brightness.