Way to turn off switch (extractor fan) based on how long light has been on

I have linked a Sonoff into my extractor fan as I have a hue bulb in my bathroom. (this meant that the extractor fan was on all the time due to the hue bulb being constantly powered… I digress

Want I currently have is if the light turns on and remains on for over 3.5 mins then turn on the extractor fan relay, and then then the light turns off, turn off the relay. This is fine (better than how it was) but I want to improve it.

I want to be able to delay the turn off command based on how long the light has been on. So if the light was on for a little while turn the extractor fan off as soon as the light goes off, but if the light was on for a while (aka a shower), then keep the extractor fan on for longer, say a few minutes after the light goes off.

I see there is a timer, but that counts down, ideally I want the reverse. Is there an easy way to achieve this? There may be something I am not thinking. I guess one option is to install a humidity sensor and use that as a condition, but I dont have one of those, so hoping I can do it through logic for now.

post what you have working now (properly formatted).

Below is what I have. 2 separate automations, one to turn on and the other to turn off. Both created with the UI:

alias: turn off extraction fan when lights turn off
description: ''
trigger:
  - platform: device
    type: turned_off
    device_id: ea3a4fe713d811ebbc5321980fac58c9
    entity_id: light.bathroom
    domain: light
condition: []
action:
  - type: turn_off
    device_id: f406a8c0f30aac29b11b67d4d0556c79
    entity_id: switch.bathroom_extractor_2
    domain: switch
mode: single

The turn on automation:

alias: turn on bathroom extraction fan
description: ''
trigger:
  - platform: device
    type: turned_on
    device_id: ea3a4fe713d811ebbc5321980fac58c9
    entity_id: light.bathroom
    domain: light
    for:
      hours: 0
      minutes: 3
      seconds: 30
condition:
  - condition: time
    after: '06:30'
    before: '21:30'
action:
  - type: turn_on
    device_id: f406a8c0f30aac29b11b67d4d0556c79
    entity_id: switch.bathroom_extractor_2
    domain: switch
mode: single

I was hoping for a turn off condition of: if light timer is 3 mins then wait 1 mins before turning off, if light timer is 5 mins then wait 5 mins before turning off.

So this works for what I want. I need to test it a few times to make sure it catches every case. But I created a timer that lasts 8 mins (may need to adjust, but I doubt I take longer than that in the shower). When the extractor fan turns on (3.5mins after the light turns on) it will start the timer. When the light turns off it will check the condition of the timer, if the timer is active (aka the light has been on for less than 8 mins) then it will turn the extractor fan off 30 seconds after the light. If the timer hits zero then the extractor fan will stay on for 4 mins after the light goes off.

alias: turn off extraction fan when lights turn off
description: ''
trigger:
  - platform: device
    type: turned_off
    device_id: ea3a4fe713d811ebbc5321980fac58c9
    entity_id: light.bathroom
    domain: light
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: timer.shower
            state: active
        sequence:
          - service: timer.cancel
            data:
              entity_id: timer.shower
          - delay: '00:00:30'
          - type: turn_off
            device_id: f406a8c0f30aac29b11b67d4d0556c79
            entity_id: switch.bathroom_extractor_2
            domain: switch
      - conditions:
          - condition: state
            entity_id: timer.shower
            state: idle
        sequence:
          - delay: '00:04:00'
          - type: turn_off
            device_id: f406a8c0f30aac29b11b67d4d0556c79
            entity_id: switch.bathroom_extractor_2
            domain: switch
    default: []
mode: single

I will likely need to adjust the numbers as 8 mins is probably far too long.