Fan with 3 speed and one switch (relay controlled)

I have an esp connected to 1 relay to trigger a fan push button and I’m really struggling understand how to make this work.

The fan is normally at speed 1.
To trigger speed 2 the relay needs to be trigger for half a second, same to go from 2 to 3 or from 3 to 1. Also after 1 hr of being on speed 2 or 3 it goes automatically to speed 1.
I would like to have a dashboard that shows as speed the slider which goes from 1 to 3 and the automatic return to 1 after the timer.

I was able to make the binary fan example work but not the triggering of the turn off of the relay.

Any help please?

Did you have some work in progress config?

I think you’ll need a number template or similar to track the fan speed and drive a timer.

For your auto 1hr timer you could look at building your own timer (if you would like to see a countdown) or look at for conditions.

See around here for timer example.

#Helpers for the timers

Can you detect when the fan speed changes or only trigger it?

I appreciate the hints. So far this is what I got:

The log information does not display the right value. Aslo would like some sort of control on a dashboard that shows the speed (a button three speeds/state). The moment the state is the state of the switch. I don’t know how to link the “fan:” structure

  - platform: template
    id: fan_speed
    optimistic: true
    min_value: 1
    max_value: 3
    step: 1

  - platform: gpio
    pin: GPIO16
    id: fan_switch
    name: "fan"
    icon: "mdi:fan"
    - delay: 500ms
    - switch.turn_off: fan_switch
    - number.increment: fan_speed
    - script.execute: fan_timer_script
    - logger.log:
        format: "Fan speed changed: %d"
        args: [ 'id(fan_speed).state' ]

  - id: fan_timer_script
    mode: restart
      - delay: 1 h
      - number.increment: fan_speed

I’m still not clear how to create the fan view in the dashboard with the 3 states, is this possible via the config of esphome or should it be done via home assistant template? What would be the best?