Logic for a Bathroom Fan/Light

My bathroom has three automated Kasa switches.
#1 - Dimmable Motion Switch → Main bathroom light
#2 - Standard Switch → Shower Light
#3 - Standard Swtich → Shower Fan

I’d like the following to happen:
A. When you enter the room, the main bathroom light turns on. No logic is needed, this happens today because of motion switch #1.
B. When the Shower Fan is turned on, it will automatically turn off in 15 minutes.
C. When the Shower Fan is turned on, it will automatically turn off in 30 minutes, ONLY if the Shower Light is turned on or off during the first 15 minutes of operation.

Essentially what I want is the Shower Fan to stay on for 30 minutes only if the Shower Light is turned on. The only way the Shower Light is turned on is manually by somebody getting ready to take a shower.

Can somebody help me with the logic needed for this?

chat gpt:

Automation for turning off the Shower Fan after 15 or 30 minutes depending on the Shower Light state.

Automation 1: Turn off the Shower Fan after 15 minutes.

  • id: shower_fan_off_15
    alias: “Shower Fan Off After 15 Minutes”
    trigger:
    • platform: state
      entity_id: switch.shower_fan
      to: “on”
      for: “00:15:00”
      condition:
    • condition: state
      entity_id: light.shower_light
      state: “off”
      action:
    • service: switch.turn_off
      target:
      entity_id: switch.shower_fan

Automation 2: Monitor the Shower Light state within the first 15 minutes.

  • id: shower_fan_light_monitor
    alias: “Shower Fan Light Monitor”
    trigger:
    • platform: state
      entity_id: light.shower_light
      condition:
    • condition: state
      entity_id: switch.shower_fan
      state: “on”
    • condition: template
      value_template: >-
      {{ (now() - states.switch.shower_fan.last_changed).total_seconds() < 900 }}
      action:
    • service: input_boolean.turn_on
      target:
      entity_id: input_boolean.shower_light_activated

Automation 3: Turn off the Shower Fan after 30 minutes if the Shower Light was activated.

  • id: shower_fan_off_30
    alias: “Shower Fan Off After 30 Minutes”
    trigger:
    • platform: state
      entity_id: switch.shower_fan
      to: “on”
      for: “00:30:00”
      condition:
    • condition: state
      entity_id: input_boolean.shower_light_activated
      state: “on”
      action:
    • service: switch.turn_off
      target:
      entity_id: switch.shower_fan
    • service: input_boolean.turn_off
      target:
      entity_id: input_boolean.shower_light_activated

Input Boolean to track if the Shower Light was activated.

input_boolean:
shower_light_activated:
name: “Shower Light Activated”
initial: false

Please check out this link to properly format your code. #11

It helps expedite resolutions.