Help with fan automation

Can anybody help me with one automation?
I have a fan in my bathroom that is controlled from the built in moisture sensor.
It also has a trigger input where you with a switch can start the fan manually.
I want to have this input controlled. The problem is that if I manually trigger the fan with a controlled relay, it will be ON forever. I want to have an automation to switch the relay off after 5-10seconds after i manually trigger it. then the fan goes for 15min more(fan-parameter) then it stops.
I tried to used different things in Home Assistant automation but it looks like I cant have the same enities to trigger itself if you know what I mean. I reied to add a pause and then run an automation that turns off the relay, without success. Anyone done a similar solution?

Can we have a look at the code you have so far?

And do you have different entities for the fan and the relay or do you only have one entity for the fan, independant how it was triggert (by humidity or by the relay)?

You only need 1 entity A switch on/off, or why do you use a relay ? … normally you connect the mentioned input to a switch in the wall, but can have a builtin behind the wall-socket, or a “hidden somewhere” you “only” can control via HA

I only have 1 entitie for the the fan. Its direct connected to the AC. Then directly connected to the fan.

I only have one switch to the fan. Its showed as a entitie in Home Assistant.

alias: Styrning av badrumsfläkt
description: “”
trigger:

  • platform: state
    entity_id:
    • switch.badrumsflakt_2477
      from: “off”
      to: “on”
      condition: []
      action:
  • wait_for_trigger: []
    timeout:
    hours: 0
    minutes: 0
    seconds: 2
    milliseconds: 0
  • delay:
    hours: 0
    minutes: 0
    seconds: 3
    milliseconds: 0
    enabled: false
  • service: automation.turn_on
    data: {}
    target:
    entity_id: automation.stoppa_badrumsflakt
    mode: single

if i understand it right, turning on the switch makes the fan run and when you turn it off, it runs for 15 minutes or so.
Then it would be an easy automation:

- id: "turn_fan_switch_off" 
  alias: Turn Fan Switch off after 15 seconds when turned on
  trigger:
    - platform: state
      entity_id: switch.badrumsflakt_2477
      from: 'off'
      to: 'on'
      for:
        seconds: 15
  action:
    - service: switch.turn_off
      entity_id: switch.badrumsflakt_2477

When the switch is turned on, after 15 seconds it would be automatically turned off. This happens every time the on state reaches 15 seconds. The fan then works about 15 minutes in its own automation (not belonging to home assistant)
Is it this what you need?

By the way, when you post your code please format it right so it could be read easily. Look in the forum guidelines how to:

Thank for your support.

Hello!
I had to do some small adjustments to the code, but this worked. Thanks @madface

alias: Styrning av badrumsfläkt
description: “”
trigger:
platform: state
entity_id:
switch.badrumsflakt_2477
from: “off”
to: “on”
action:
delay:
hours: 0
minutes: 0
seconds: 2
milliseconds: 0
service: switch.turn_off
entity_id: switch.badrumsflakt_2477
mode: single

The only difference between the automations (apart from the different seconds) is, that yours trigger in the moment the fan switches on and mine after the switch is on for x seconds.
So if you switch the fan on and off between 1 second, yours will trigger anyway, then wait 2 seconds and then switch off (which is no problem). Mine wouldn’t trigger in this case.
Anyway, glad it is solved and working as you wish. But as you have marked your code as solution, please format it right so any other could copy the code to use it.
Little hint, mode: single is default, you do not have to put this line in.