Piggybacking rotary encoder on thermostat

I tried linking my central heating in using EMS-ESP, but the thermostat was incompatible.
The incremental rotary encoder in the thermostat uses quadrature encoding at ca. 3V which I thought would be straightforward enough to mimic using an ESP8266. I’ve attached connections to the relevant terminals of the encoder but have not managed to get functional code working yet for the ESP, so have not connected it yet. I’m relatively new to coding so may be making rookie mistakes…

This is my current code:

esphome:
  name: esphome-web-10453a
  friendly_name: ch-thermostat

esp8266:
  board: esp01_1m

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "myKey="

ota:

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esphome-Web-10453A"
    password: "myPassword"

captive_portal:

#use GPIO4 and GPIO5 as no conflicts at boot

#read
# binary_sensor:
#   - platform: gpio
#     pin: GPIO5
#     id: read_a

#   - platform: gpio
#     pin: GPIO4
#     id: read_b

#write
switch:
  - platform: gpio
    pin: GPIO5
    id: encoder_a
    restore_mode: DISABLED

  - platform: gpio
    pin: GPIO4
    id: encoder_b
    restore_mode: DISABLED

button:
  - platform: template
    id: CHup
    on_press:
      - switch.toggle: encoder_a
      - delay: 50ms
      - switch.toggle: encoder_b
      - delay: 50ms

  - platform: template
    id: CHdown
    on_press:
      - switch.toggle: encoder_b
      - delay: 50ms
      - switch.toggle: encoder_a
      - delay: 50ms

Is the state of a GPIO switch stored somewhere, or is it actually checked first?
If the former, does it also need to be a binary sensor to check?

I’ve also tried this, but am getting complaints about syntax errors:

esphome:
  name: esphome-web-10453a
  friendly_name: ch-thermostat

esp8266:
  board: esp01_1m

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "myKey="

ota:

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esphome-Web-10453A"
    password: "myPassword"

captive_portal:

#use GPIO4 and GPIO5 as no conflicts at boot

#read
binary_sensor:
  - platform: gpio
    pin: GPIO5
    id: read_a

  - platform: gpio
    pin: GPIO4
    id: read_b

#write
switch:
  - platform: gpio
    pin: GPIO5
    id: encoder_a
    restore_mode: DISABLED

  - platform: gpio
    pin: GPIO4
    id: encoder_b
    restore_mode: DISABLED

button:
  - platform: template
    id: CHup
    on_press:
      if:
        binary_sensor.is_on: read_a
        then:
         - switch.turn_off: encoder_a
         - delay: 50ms
        else:
          - switch.turn_on: encoder_a
          - delay: 50ms
          
        if:
          binary_sensor.is_on: read_b
          then:
          - switch.turn_off: encoder_b
          - delay: 50ms
          else:
          - switch.turn_on: encoder_b
          - delay: 50ms

  - platform: template
    id: CHdown
    on_press:
      if:
        binary_sensor.is_on: read_b
        then:
         - switch.turn_off: encoder_b
         - delay: 50ms
        else:
          - switch.turn_on: encoder_b
          - delay: 50ms
        if:
          binary_sensor.is_on: read_a
          then:
          - switch.turn_off: encoder_a
          - delay: 50ms
          else:
          - switch.turn_on: encoder_a
          - delay: 50ms

Any pointers very gratefully received! TIA

This? Rotary Encoder Sensor — ESPHome

Thanks - but as I understand it, that’s to read a physical rotary encoder in operation.

I want to mimic one in software, but believe I also need to take into consideration the current output states of the physical one it’s connected across for it to be effective.
E.g. if I set encoder_a to high, then encoder_b to high, if encoder_a is already high it looks like encoder_b is the first signal, indicating a turn in the opposite direction!