How set rotery_encoder from HA

Hello, I’m creating a Fan control system consisting of 2 devices. One device is psychically connected to the FAN system and controllable from HA. A second device is a remote for the FAN system and equipped with a display and a rotery encoder. Turning the rotery encoder controls the speed of the fan. When the fan is controlled from HA I need the rotery encoder to follow the percentage of the Fan component.
I can’t figure out how to set the rotery position when changing the Fan speed from HA.
This is what i have so far.

wtwcontroller.yaml

substitutions:
  node_name: wtw-controller

esphome:
  name: ${node_name}
  friendly_name: Itho WTW besturing
  comment: Draadloze besturing voor Itho warmte terugwin installatie
  libraries:
  includes:
  
esp32:
  board: esp32dev
  framework:
    type: arduino

wifi:
    ssid: !secret wifi_ssid
    password: !secret wifi_password
  
    ap:
      ssid: "WTW Fallback Hotspot"
      password: "12345678"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

i2c:
  sda: 21
  scl: 22
  scan: true

# Set the output with default (address: 0x60 / global i2c)
output:
  - platform: mcp4725
    id: dac_output
    min_power: 0.1
    max_power: 0.9  

fan:
  - platform: speed
    output: dac_output
    id: "wtw"
    name: "WTW"
    on_turn_off:
      - component.update: ttgo
    on_speed_set:
      - component.update: ttgo

binary_sensor:
  - platform: gpio
    entity_category: config
    pin:
      number: GPIO0
      inverted: true
      mode:
        input: true
        pullup: true
    filters:    
    - autorepeat:
      - delay: 200ms
        time_off: 100ms
        time_on: 400ms
      - delay: 500ms
        time_off: 50ms
        time_on: 50ms        
    id: tdisplay_button_input_0
    on_press:
      then:
        - logger.log: "on_press_down"
        - lambda: |-
            int current_speed = id(wtw).speed;
            current_speed--;
            auto call = id(wtw).turn_on();
            call.set_speed(current_speed);
            call.perform();
        - component.update: ttgo            

  - platform: gpio
    entity_category: config
    pin:
      number: GPIO35
      inverted: true
      mode:
        input: true
    filters:    
    - autorepeat:
      - delay: 200ms
        time_off: 100ms
        time_on: 400ms
      - delay: 500ms
        time_off: 50ms
        time_on: 50ms        
    id: tdisplay_button_input_1
    on_press:
      then:
        - logger.log: "on_press_up"
        - lambda: |-
            int current_speed = id(wtw).speed;
            current_speed++;
            auto call = id(wtw).turn_on();
            call.set_speed(current_speed);
            call.perform();
        - component.update: ttgo

font:
  - file: "fonts/Montserrat-Bold.ttf"
    id: speed_font
    size: 72

spi:
  clk_pin: GPIO18
  mosi_pin: GPIO19

display:
  - platform: st7789v
    model: TTGO TDisplay 135x240
    backlight_pin: GPIO4
    cs_pin: GPIO5
    dc_pin: GPIO16
    reset_pin: GPIO23
    rotation: 90
    update_interval: never
    id: ttgo
    lambda: |-
      if (id(wtw).state) {
        it.printf(40, 20, id(speed_font), "%d%%", id(wtw).speed);
      } else {
        auto red = Color(255, 0, 0);
        it.fill(red);
        it.print(15, 20, id(speed_font), "-OFF-");
      }

sensor:
  - platform: wifi_signal # Reports the WiFi signal strength/RSSI in dB
    name: "WiFi Signal dB"
    id: wifi_signal_db
    update_interval: 60s
    entity_category: "diagnostic"

wtwremote.yaml

substitutions:
  node_name: wtw-remote

esphome:
  name: ${node_name}
  friendly_name: Itho WTW afstandsbesturing
  comment: Draadloze besturing voor Itho warmte terugwin installatie
  libraries:
  includes:
  
esp32:
  #board: esp32dev
  board: lolin_s2_mini  
  framework:
    type: arduino

wifi:
    ssid: !secret wifi_ssid
    password: !secret wifi_password
  
    ap:
      ssid: "WTW Fallback Hotspot"
      password: "12345678"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

sensor:
  - platform: rotary_encoder
    name: "Draaiknop WTW"
    pin_a:
      number: 12
      mode:
        input: true
        pullup: true
    pin_b:
      number: 14
      mode:
        input: true
        pullup: true
    min_value: 0
    max_value: 100
    restore_mode: RESTORE_DEFAULT_ZERO
    id: encoder
    on_clockwise:
      - logger.log: "Turned Clockwise"
      - component.update: displ
      - homeassistant.service:
          service: fan.set_percentage
          data:
            entity_id: fan.wtw_controller_wtw
          data_template:
            percentage: "{{ encoder_step }}"
          variables:
            encoder_step: |-
              return id(encoder).state;  
    on_anticlockwise:
      - logger.log: "Turned Anticlockwise"
      - component.update: displ
      - homeassistant.service:
          service: fan.set_percentage
          data:
            entity_id: fan.wtw_controller_wtw
          data_template:
            percentage: "{{ encoder_step }}"
          variables:
            encoder_step: |-
              return id(encoder).state;

  - platform: wifi_signal # Reports the WiFi signal strength/RSSI in dB
    name: "WiFi Signal dB"
    id: wifi_signal_db
    update_interval: 60s
    entity_category: "diagnostic"

font:
  - file: "fonts/Montserrat-Bold.ttf"
    id: speed_font
    size: 72

output:
  - platform: ledc
    pin: GPIO4
    id: gpio_4

light:
  - platform: monochromatic
    output: gpio_4
    name: "backlight"
    restore_mode: RESTORE_DEFAULT_ON

spi:
  clk_pin: GPIO36     # SCL on display   GPIO19
  mosi_pin: GPIO35    # SDA on display   GPIO18
    
display:
  - platform: st7735
    model: INITR_REDTAB
    cs_pin: GPIO034 #GPIO16
    dc_pin: GPIO17
    reset_pin: GPIO5
    rotation: 0
    device_width: 160
    device_height: 80
    col_start: 0
    row_start: 0
    eight_bit_color: false
    use_bgr: false
    update_interval: never 
    id: displ
    lambda: |-
      it.printf(40, 20, id(speed_font), "%d%%", id(encoder).state);
    

This was the solution (added to wtwremote.yaml):

Sensor:
  - platform: homeassistant
    name: "WTW Fan Sensor From Home Assistant"
    entity_id: fan.wtw_controller_wtw
    attribute: percentage
    id: wtw
    on_value:
      then:
        - sensor.rotary_encoder.set_value:
            id: encoder
            value: !lambda 'return int(x);'