Automation actions not performed correctly when power cable is reconnected

Hi,

I built a motor speed control using ESP8266 D1 mini board with a button to start the motor at 10% of its speed. I set up the PWM output at Pin D5 and a speed fan platform to control the motor speed. I also connect a button to pin D6 as binary sensor to trigger the action to turn on the fan at 10% of it speed.

Everything works fine, except, when I pulled out the power cable for D1 mini board and then re-plug it in again, when I first press the button, it turned on the fan with 100% of its speed. the subsequent attempts to turn on/off the fan, it works all correctly. The issue only occurs when power is dropped and reconnect again. In all other situation, the automation works as intended,

I am using the HA core version 2023.9.3 with OS 10.5. the ESPhome version is 2023.9.3
I attached my YAMAL file here:

esphome:
  name: esp-d1-mini-04
  comment: "Motor Speed Control"
  friendly_name: esp-d1-mini-04

esp8266:
  board: d1_mini


output:
  - platform: esp8266_pwm
    pin: D5
    id: pwm_output
    inverted: True
    frequency: 460 Hz
    min_power: 0%
    max_power: 100%

fan:
  - platform: speed
    id: cleaner_motor
    output: pwm_output
    name: "Vinyl recod cleaner"
    restore_mode: ALWAYS_OFF

binary_sensor:
  - platform: gpio
    pin:
      number: D6
      mode: INPUT_PULLUP
      inverted: true
    name: "Start button for cleaner"
    on_click:
      - min_length: 50 ms
        max_length: 350 ms
        then:
          - if:
              condition:
                - fan.is_off: cleaner_motor
              then:
                - fan.turn_on:
                    id: cleaner_motor
                    speed: 10
              else:
                - fan.turn_off: 
                    id: cleaner_motor

I am also attach the ESPhome log here as well. The log was generated when I recompiled the YAML file. The button worked correctly after uploaded the codes into my D1 mini board. however, when I unplugged the power cable, you can see a connection error in the log and subse4quently the connection is reestablished. after that, the fan is turned at the speed of 100%, after I pressed the button. The next the press of the button worked just fine.

INFO ESPHome 2023.9.3
INFO Reading configuration /config/esphome/esp-d1-mini-04.yaml...
INFO Generating C++ source...
INFO Compiling app...
Processing esp-d1-mini-04 (board: d1_mini; framework: arduino; platform: platformio/[email protected])
--------------------------------------------------------------------------------
HARDWARE: ESP8266 80MHz, 80KB RAM, 4MB Flash
Dependency Graph
|-- ESPAsyncTCP-esphome @ 2.0.0
|-- ESPAsyncWebServer-esphome @ 3.1.0
|-- DNSServer @ 1.1.1
|-- ESP8266WiFi @ 1.0
|-- ESP8266mDNS @ 1.2
|-- noise-c @ 0.1.4
Compiling .pioenvs/esp-d1-mini-04/src/main.cpp.o
Linking .pioenvs/esp-d1-mini-04/firmware.elf
RAM:   [====      ]  42.0% (used 34396 bytes from 81920 bytes)
Flash: [=====     ]  47.2% (used 493117 bytes from 1044464 bytes)
Building .pioenvs/esp-d1-mini-04/firmware.bin
esp8266_copy_factory_bin([".pioenvs/esp-d1-mini-04/firmware.bin"], [".pioenvs/esp-d1-mini-04/firmware.elf"])
========================= [SUCCESS] Took 22.33 seconds =========================
INFO Successfully compiled program.
INFO Resolving IP address of esp-d1-mini-04.local
INFO  -> 192.168.1.36
INFO Uploading /data/build/esp-d1-mini-04/.pioenvs/esp-d1-mini-04/firmware.bin (497264 bytes)
INFO Compressed to 348968 bytes
Uploading: [============================================================] 100% Done...

INFO Waiting for result...
INFO OTA successful
INFO Successfully uploaded program.
INFO Starting log output from esp-d1-mini-04.local using esphome API
WARNING Can't connect to ESPHome API for esp-d1-mini-04.local: Error resolving IP address: [Errno -5] No address associated with hostname (APIConnectionError)
INFO Trying to connect to esp-d1-mini-04.local in the background
INFO Successfully connected to esp-d1-mini-04.local
[15:08:02][I][app:102]: ESPHome version 2023.9.3 compiled on Oct  7 2023, 15:07:25
[15:08:02][C][wifi:546]: WiFi:
[15:08:02][C][wifi:382]:   Local MAC: XXXXXXXXXXXXXXXXXXX
[15:08:02][C][wifi:383]:   SSID: 'XXXXXXXXXXXXXXXX'[redacted]
[15:08:02][C][wifi:384]:   IP Address: XXXXXXXXXXXX
[15:08:02][C][wifi:385]:   BSSID: XXXXXXXXXXXXXXXXX[redacted]
[15:08:02][C][wifi:387]:   Hostname: 'esp-d1-mini-04'
[15:08:02][C][wifi:389]:   Signal strength: -56 dB ▂▄▆█
[15:08:02][C][wifi:393]:   Channel: 9
[15:08:02][C][wifi:394]:   Subnet: 255.255.255.0
[15:08:02][C][wifi:395]:   Gateway: XXXXXXXXXXXXXX
[15:08:02][C][wifi:396]:   DNS1: XXXXXXXXXXXXXXXXX
[15:08:02][C][wifi:397]:   DNS2: 0.0.0.0
[15:08:02][C][logger:357]: Logger:
[15:08:02][C][logger:358]:   Level: DEBUG
[15:08:02][C][logger:359]:   Log Baud Rate: 115200
[15:08:02][C][logger:361]:   Hardware UART: UART0
[15:08:02][C][esp8266_pwm:022]: ESP8266 PWM:
[15:08:02][C][esp8266_pwm:023]:   Pin: GPIO14
[15:08:02][C][esp8266_pwm:024]:   Frequency: 460.0 Hz
[15:08:02][C][esp8266_pwm:025]:   Inverted: YES
[15:08:02][C][gpio.binary_sensor:015]: GPIO Binary Sensor 'Start button for cleaner'
[15:08:02][C][gpio.binary_sensor:016]:   Pin: GPIO12
[15:08:02][C][speed.fan:016]: Speed Fan 'Vinyl recod cleaner'
[15:08:02][C][speed.fan:151]:   Speed: YES
[15:08:02][C][speed.fan:152]:   Speed count: 100
[15:08:02][C][captive_portal:088]: Captive Portal:
[15:08:02][C][mdns:115]: mDNS:
[15:08:02][C][mdns:116]:   Hostname: esp-d1-mini-04
[15:08:02][C][ota:097]: Over-The-Air Updates:
[15:08:02][C][ota:098]:   Address: esp-d1-mini-04.local:8266
[15:08:02][C][ota:101]:   Using Password.
[15:08:02][C][api:138]: API Server:
[15:08:02][C][api:139]:   Address: esp-d1-mini-04.local:6053
[15:08:02][C][api:141]:   Using noise encryption: YES
[15:08:03][D][api.connection:1031]: Home Assistant 2023.9.3 (192.168.1.204): Connected successfully
[15:08:26][D][binary_sensor:036]: 'Start button for cleaner': Sending state ON
[15:08:26][D][binary_sensor:036]: 'Start button for cleaner': Sending state OFF
[15:08:26][D][fan:021]: 'Vinyl recod cleaner' - Setting:
[15:08:26][D][fan:024]:   State: ON
[15:08:26][D][fan:030]:   Speed: 10
[15:08:26][D][fan:092]: 'Vinyl recod cleaner' - Sending state:
[15:08:26][D][fan:093]:   State: ON
[15:08:26][D][fan:095]:   Speed: 10
[15:08:27][D][binary_sensor:036]: 'Start button for cleaner': Sending state ON
[15:08:27][D][binary_sensor:036]: 'Start button for cleaner': Sending state OFF
[15:08:27][D][fan:021]: 'Vinyl recod cleaner' - Setting:
[15:08:27][D][fan:024]:   State: OFF
[15:08:27][D][fan:092]: 'Vinyl recod cleaner' - Sending state:
[15:08:27][D][fan:093]:   State: OFF
[15:08:27][D][fan:095]:   Speed: 10
[15:08:28][D][binary_sensor:036]: 'Start button for cleaner': Sending state ON
[15:08:29][D][binary_sensor:036]: 'Start button for cleaner': Sending state OFF
[15:08:29][D][fan:021]: 'Vinyl recod cleaner' - Setting:
[15:08:29][D][fan:024]:   State: ON
[15:08:29][D][fan:030]:   Speed: 10
[15:08:29][D][fan:092]: 'Vinyl recod cleaner' - Sending state:
[15:08:29][D][fan:093]:   State: ON
[15:08:29][D][fan:095]:   Speed: 10
[15:08:29][D][binary_sensor:036]: 'Start button for cleaner': Sending state ON
[15:08:30][D][binary_sensor:036]: 'Start button for cleaner': Sending state OFF
[15:08:30][D][fan:021]: 'Vinyl recod cleaner' - Setting:
[15:08:30][D][fan:024]:   State: OFF
[15:08:30][D][fan:092]: 'Vinyl recod cleaner' - Sending state:
[15:08:30][D][fan:093]:   State: OFF
[15:08:30][D][fan:095]:   Speed: 10
WARNING esp-d1-mini-04.local: Connection error occurred: [Errno 104] Connection reset by peer
INFO Processing unexpected disconnect from ESPHome API for esp-d1-mini-04.local
WARNING Disconnected from API
INFO Successfully connected to esp-d1-mini-04.local
[15:09:16][D][api.connection:1031]: Home Assistant 2023.9.3 (192.168.1.204): Connected successfully
[15:09:22][D][binary_sensor:036]: 'Start button for cleaner': Sending state ON
[15:09:22][D][binary_sensor:036]: 'Start button for cleaner': Sending state OFF
[15:09:22][D][fan:021]: 'Vinyl recod cleaner' - Setting:
[15:09:22][D][fan:024]:   State: ON
[15:09:22][D][fan:030]:   Speed: 100
[15:09:22][D][fan:092]: 'Vinyl recod cleaner' - Sending state:
[15:09:22][D][fan:093]:   State: ON
[15:09:22][D][fan:095]:   Speed: 100
[15:09:25][D][binary_sensor:036]: 'Start button for cleaner': Sending state ON
[15:09:25][D][binary_sensor:036]: 'Start button for cleaner': Sending state OFF
[15:09:25][D][fan:021]: 'Vinyl recod cleaner' - Setting:
[15:09:25][D][fan:024]:   State: OFF
[15:09:25][D][fan:092]: 'Vinyl recod cleaner' - Sending state:
[15:09:25][D][fan:093]:   State: OFF
[15:09:25][D][fan:095]:   Speed: 100
[15:09:27][D][binary_sensor:036]: 'Start button for cleaner': Sending state ON
[15:09:27][D][binary_sensor:036]: 'Start button for cleaner': Sending state OFF
[15:09:27][D][fan:021]: 'Vinyl recod cleaner' - Setting:
[15:09:27][D][fan:024]:   State: ON
[15:09:27][D][fan:030]:   Speed: 10
[15:09:27][D][fan:092]: 'Vinyl recod cleaner' - Sending state:
[15:09:27][D][fan:093]:   State: ON
[15:09:27][D][fan:095]:   Speed: 10
[15:09:32][D][binary_sensor:036]: 'Start button for cleaner': Sending state ON
[15:09:32][D][binary_sensor:036]: 'Start button for cleaner': Sending state OFF
[15:09:32][D][fan:021]: 'Vinyl recod cleaner' - Setting:
[15:09:32][D][fan:024]:   State: OFF
[15:09:32][D][fan:092]: 'Vinyl recod cleaner' - Sending state:
[15:09:32][D][fan:093]:   State: OFF
[15:09:32][D][fan:095]:   Speed: 10

This appears this is a bug in the system, rather than a configuration issue. Please help.

Thank you very much in advance.