Fan control with another esphome device

Brief background, I have a Sonoff ifan02 connected to ceiling fan, and a Sonoff T1 wall switch.
Both flashed with EspHome.
I want to be able to increment the fan speed when pressing a button on T1, and ultimately switch it off.
Off>Low>Medium>High>Off, alternatively toggle between Off and Low.
It’s easy enough to use the home assistant service fan.set_percentage to turn it on, but my lack of knowledge on templates/lambdas (I assume that’s what I should use) and HA in general keep me from achieving the goal.

wall_switch - yaml

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO9
      mode:
        input: true
        pullup: true
      inverted: true
    id: button_2
    on_press:
      - homeassistant.service:
          service: fan.set_percentage
          data:
#don't know how to change and check for existing fan state, and toggle as required
            percentage: '6'
            entity_id: fan.study_fan    
      # then:
      #   - light.toggle: light_2

I will also be using this in 4 other rooms, as well as a central NSPanel.
Thanks in advance for any assistance

Something like this? (untested)

globals:
  - id: fan_speed
    type: int
    restore_value: no
    initial_value: 0
    
binary_sensor:
  - platform: gpio
    pin:
      number: GPIO9
      mode:
        input: true
        pullup: true
      inverted: true
    id: button_2
    on_press:
      then:
        - lambda: |-
            if (id(fan_speed) < 3) {
              id(fan_speed) += 1;
            } else {
              id(fan_speed) = 0;
            }
        - homeassistant.service:
            service: fan.set_percentage
            data_template:
              percentage: |-
                return id(fan_speed)*33; 
                # convert to percentage?
              entity_id: fan.study_fan    

Thanks for this, after small change to global variable ‘1’, it compiled and uploaded.
However, nothing happens. I have added a few log entries which shows the increment and off states.

        - lambda: |-
            if (id(fan_speed) < 3) {
              ESP_LOGD("main", "Fan speed increment");
              id(fan_speed) += 1;
            } else {
              ESP_LOGD("main", "Fan set to Off");
              id(fan_speed) = 0;
            }
[17:59:40][D][binary_sensor:036]: 'button_2': Sending state ON
[17:59:40][D][main:048]: Fan speed increment
[17:59:40][D][binary_sensor:036]: 'button_2': Sending state OFF
[18:25:40][D][binary_sensor:036]: 'button_2': Sending state ON
[18:25:40][D][main:048]: Fan speed increment
[18:25:40][D][binary_sensor:036]: 'button_2': Sending state OFF
[18:25:41][D][binary_sensor:036]: 'button_2': Sending state ON
[18:25:41][D][main:048]: Fan speed increment
[18:25:42][D][binary_sensor:036]: 'button_2': Sending state OFF
[18:25:42][D][binary_sensor:036]: 'button_2': Sending state ON
[18:25:43][D][main:051]: Fan set to Off
[18:25:43][D][binary_sensor:036]: 'button_2': Sending state OFF

I suspect the code is correct, but the fan needs an ‘on’ command, the call and percentages works in developer tools.

I managed to get it working.
@zoogara Thanks for pointing me in right direction, I admire everyone with their HA and EspHome knowledge.
Here is a working solution:

globals:
  - id: fan_speed
    type: int
    restore_value: no
    initial_value: '0'

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO9
      mode:
        input: true
        pullup: true
      inverted: true
    id: button_2
    on_press:
      then:
        - lambda: |-
            id(fan_speed) += 1;
        - homeassistant.service:
            service: fan.turn_on
            data_template:
              percentage: "{{ my_speed }}"
              entity_id: fan.study_fan    
            variables:
              my_speed: |-
                if (id(fan_speed) == 1) {
                  ESP_LOGD("main", "Fan speed 1");
                  return 33;
                }
                if (id(fan_speed) == 2) {
                  ESP_LOGD("main", "Fan speed 2");
                  return 66;
                }
                if (id(fan_speed) == 3) {
                  ESP_LOGD("main", "Fan speed 3");
                  return 100;
                }
                else {
                  ESP_LOGD("main", "Fan set to Off");
                  return 0;
                }