ESP32 connection problem to HA after restart

HW Raspberry Pi4
HomeAssistant Core 2024.4.3
Supervisor 2024.04.0
OS 12.2
Frontend 20240404.2
Esphome 2024.3.2
Esphome hw ESP32-WROOM-32 (AZ-Delivery)
Python 3.12.3
(issue know too for older versions of HA and Esphome)

Description of issue
When are power off RPi4 and Esphome - then after power on HA is started without any issue and ESP32 is on (RED LED ON, power 5VDC), but not found online in HA. Then power adapter of ESP32 is unpluged and again powered and then is again online in HA (issue can be reproduced and is happened repeately).
When only RPi4 is switch to power off state. Then is powered on again (ESP32 is still under voltage) - but not found ESP32 online in HA Esphome. Then power adapter of ESP32 is unpluged and again powered and then is again online in HA Esphome (issue can be reproduced and is happened repeately).

How to solve this repeated issue, please?

And source code
(some settings in source code exhanged by question mark)

esphome:
  name: "plynomer"
  friendly_name: Plynomer

esp32:
  board: esp32dev
  framework:
    type: arduino

logger:
  level: NONE
  # baud_rate: 9600
  baud_rate: 0

# Enable Home Assistant API
api:

ota:

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

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

captive_portal:

substitutions:
  platform: ESP32
  board: esp32dev
  device_name_short: "plynomer" # used by esp-home config
  friendly_name: "BK-G4MT" # Elster DE-07-MI002-PTB002, No 06832435-041-11-I, 1 imp = 0,01 m3
  device_description: "Plynomer"
  update_interval: 60s # 60s 5.7.2023 17:40
  impulsfactor: "0.0100"
  appversion: "1.1.7"

# ----------------------------------------------------------------
# Global variables
# ----------------------------------------------------------------
globals:
  - id: z61count
    type: int
    restore_value: yes
    initial_value: "0"
  - id: z61counter
    type: float
    restore_value: yes
    initial_value: "0.0"
  - id: m3kw
    type: float
    restore_value: yes
    initial_value: "10.94"
  - id: gas_meter_displayinital
    type: float
    restore_value: yes
    initial_value: "29060.654"
  - id: gas_meter_displayvalue
    type: float
    restore_value: yes
    initial_value: "29246.433"
  - id: gas_meter_totalm3
    type: float
    restore_value: yes
    initial_value: "0.0"

# ----------------------------------------------------------------
# Global variables
# ----------------------------------------------------------------

  - id: boot_counter
    type: int
    restore_value: yes
    initial_value: "0"
  - id: daily_value
    type: float
    restore_value: yes
    initial_value: "0.0"
  - id: hour_value
    type: float
    restore_value: yes
    initial_value: "0.0"

## ---------------------------------------------------
## binary sensors
## ---------------------------------------------------

## ---------------------------------------------------
## switches
## ---------------------------------------------------
switch:
  - platform: restart
    id: restart_device
    name: ${friendly_name} reštart
  - platform: template
    name: ${friendly_name} reset boot počítadla
    turn_on_action:
      then:
        - lambda: |-
            id(boot_counter) = 0;
            id(bootcounter).update();
        - logger.log: ${device_name_short} reset boot counter o.k!
  - platform: template
    name: ${friendly_name} reset všetko
    turn_on_action:
      then:
        - lambda: |-
            id(boot_counter) = 0;
            id(z61counter) = 0.0;
            id(z61count) = 0;
            id(gas_meter_totalm3)= 0.0;
            id(gas_meter_displayvalue)= 0.0;
            id(daily_value)= 0.0;
            id(hour_value)= 0.0;
            id(bootcounter).update();
            id(gas_meter_display).update();
            id(gas_meter_total_m3).update();
            id(gas_meter_total_kw).update();
            id(gas_meter_total_kw_day).update();
        - logger.log: ${device_name_short} all values reset!
## ---------------------------------------------------
## all default sensors
## ---------------------------------------------------

## ---------------------------------------------------
## all text sensors
## ---------------------------------------------------
text_sensor:
  - platform: version
    name: ${friendly_name} Verzia
    id: appver

  - platform: template
    name: ${friendly_name} online od
    id: uptime_human
    icon: mdi:clock-start

  - platform: wifi_info
    ip_address:
      name: ${friendly_name} IP
      id: wifiip
      icon: mdi:ip-network

    ssid:
      name: ${friendly_name} SSID
      id: wifiip_ssid
      icon: mdi:wifi

    bssid:
      name: ${friendly_name} BSSID
      id: wifiip_bssid
      icon: mdi:wifi

    mac_address:
      name: ${friendly_name} MAC Wifi Adresa
      id: wifimac_address
      icon: mdi:wifi

# ---------------------------------------
# Base packages wifi, timeserver...
# ---------------------------------------
#packages:
#  base: !include common/base.yaml
#  base_webserver: !include common/base_webserver.yaml
#  base_global: !include common/base_global.yaml

# ----------------------------------------------------------------
# Switch to restart, reset all, reset bootcounter
# ----------------------------------------------------------------

# ----------------------------------------------------------------
# GASCOUNTER ZI-61 REED CONTACT
# GPIO19 <-- o-RC-o <--- GND
#            o-10k ----> +5V
# +++++++++++++++++++++++++++++++
# Gasmeter: Krom Schröder BK-G4
# ++++++++++++++++++++++++++++++++
# Q_max: 4 m³/h
# Q_min: 0.025 m³/h
# V: 1.2 dm³
# p_max: 0.5 bar
# 1 imp = 0.01 m³
# ----------------------------------------------------------------
binary_sensor:
  - platform: status
    name: ${friendly_name} Status
  - platform: gpio
    pin:
      number: GPIO19
      inverted: false
      mode: # INPUT_PULLUP # set ESP32 pin to input pull-up mode
        input: true
        pullup: true
    name: ${friendly_name} Plynomer Z-61 Impulzy
    id: gassensor_state
    device_class: window
    filters:
      - delayed_off: 10ms
    ## This automation will be triggered when a button press ends,
    ## or in other words on the falling edge of the signal.
    on_release:
      then:
        - lambda: |-
            ESP_LOGI("main", " -------  Z-61 SET NEW VALUES !!!!!");
            id(z61count) += 1;
            id(z61counter) += ${impulsfactor};
            id(gas_meter_displayvalue) += ${impulsfactor};
            id(gas_meter_totalm3) += ${impulsfactor};
            id(daily_value) += ${impulsfactor};
            id(hour_value) += ${impulsfactor};
            id(gas_meter_display).update();
            id(gas_meter_total_m3).update();
            id(gas_meter_total_kw).update();
            id(gas_meter_total_kw_day).update();
            id(gas_meter_m3_today).publish_state(${impulsfactor});
# ----------------------------------------------------------------
# ALL SENSORS
# ----------------------------------------------------------------
sensor:

  - platform: pulse_meter
    pin: GPIO21 # priamo na +5VDC
    name: "X_Instant water consumption"
    unit_of_measurement: "L/min"
#    timeout: 1s
    internal_filter_mode: PULSE
    internal_filter: 50ms
    accuracy_decimals: 3
    filters:
      - lambda: return (x / 0.1);
    total:
      name: "X_Total_gas_consumption"
      unit_of_measurement: "m³"
      device_class: gas
      state_class: total_increasing
      accuracy_decimals: 3
      filters:
        - lambda: return (x / 100.0);

#  - platform: adc
#    pin: GPIO32
#    name: "Napätie batérie"
#    unit_of_measurement: "V"
#    update_interval: 60s
#    attenuation: 11dB

# Attenuation (db)
# +----------+-------------+-----------------+
# |          | attenuation | suggested range |
# |    SoC   |     (dB)    |      (mV)       |
# +==========+=============+=================+
# |          |       0     |    100 ~  950   |
# |          +-------------+-----------------+
# |          |       2.5   |    100 ~ 1250   |
# |   ESP32  +-------------+-----------------+
# |          |       6     |    150 ~ 1750   |
# |          +-------------+-----------------+
# |          |      11     |    150 ~ 2450   |
# +----------+-------------+-----------------+
# |          |       0     |      0 ~  750   |
# |          +-------------+-----------------+
# |          |       2.5   |      0 ~ 1050   |
# | ESP32-S2 +-------------+-----------------+
# |          |       6     |      0 ~ 1300   |
# |          +-------------+-----------------+
# |          |      11     |      0 ~ 2500   |
# +----------+-------------+-----------------+

  - platform: pulse_meter
    unit_of_measurement: 'kW'
    #device_class: 'power'
    #state_class: 'measurement'
    name: 'Stromzähler'
    pin: GPIO18 # D
    filters:
      - multiply: 1 # (60s / 75 pulses per kWh) * 1000 W
      - filter_out: nan
    internal_filter_mode: PULSE  
    internal_filter: 50ms
    accuracy_decimals: 3
    total:
      name: "Total"
      unit_of_measurement: 'imp'
      device_class: gas # nove od 6.8.2023
      state_class: total_increasing # nove od 6.8.2023
      accuracy_decimals: 0
      filters:
        - multiply: 1
      #  - debounce: 300ms
      on_value:
        then:
          - lambda: |-
             ESP_LOGI("main", " -------  Z-61 SET NEW VALUES !!!!!");
             id(z61count) += 1;
             id(z61counter) += ${impulsfactor};
             id(gas_meter_displayvalue) += ${impulsfactor};
             id(gas_meter_totalm3) += ${impulsfactor};
             id(daily_value) += ${impulsfactor};
             id(hour_value) += ${impulsfactor};
             id(gas_meter_display).update();
             id(gas_meter_total_m3).update();
             id(gas_meter_total_kw).update();
             id(gas_meter_total_kw_day).update();
             id(gas_meter_m3_today).publish_state(${impulsfactor});

  - platform: template
    name: ${friendly_name} počítadlo štartov
    id: bootcounter
    accuracy_decimals: 0
    lambda: |-
      return (id(boot_counter));
  - platform: uptime
    name: ${friendly_name} Uptime Sensor
    id: uptime_sensor
    update_interval: ${update_interval}
    on_raw_value:
      then:
        - text_sensor.template.publish:
            id: uptime_human
            state: !lambda |-
              int seconds = round(id(uptime_sensor).raw_state);
              int days = seconds / (24 * 3600);
              seconds = seconds % (24 * 3600);
              int hours = seconds / 3600;
              seconds = seconds % 3600;
              int minutes = seconds /  60;
              seconds = seconds % 60;
              return (
                (days ? String(days) + "d " : "") +
                (hours ? String(hours) + "h " : "") +
                (minutes ? String(minutes) + "m " : "") +
                (String(seconds) + "s")
              ).c_str();
  - platform: wifi_signal
    id: wifisignal
    name: ${friendly_name} WiFi signál
    update_interval: 60s # 6.8.2023 17:30 60s 

  - platform: template
    id: sensor_wifi_signal_percentage
    name: "${friendly_name} WiFi signál percentuálne"
    icon: "mdi:wifi"
    unit_of_measurement: "%"
    update_interval: 60s # 6.8.2023 17:30 60s
    lambda: |-
      if (id(wifisignal).state) {
        if (id(wifisignal).state <= -100 ) {
          return 0;
        } else if (id(wifisignal).state >= -50) {
          return 100;
        } else {
          return 2 * (id(wifisignal).state + 100);
        }
      } else {
        return NAN;
      }
  - platform: template
    name: ${friendly_name} Plynomer zobrazené
    id: gas_meter_display
    accuracy_decimals: 2
    unit_of_measurement: "m³"
    icon: "mdi:counter"
    device_class: gas
    lambda: |-
      return id(gas_meter_displayvalue);
  - platform: template
    name: ${friendly_name} Plynomer aktuálne
    id: gas_meter_m3_today
    state_class: measurement
    device_class: gas
    unit_of_measurement: "m³"
    accuracy_decimals: 3

  - platform: template
    name: ${friendly_name} impulzov
    id: gas_meter_impulse_count
    state_class: measurement
    unit_of_measurement: "imp"
    accuracy_decimals: 0
    lambda: |-
      return (id(z61count));
  - platform: template
    name: ${friendly_name} m³ celkovo
    id: gas_meter_total_m3
    accuracy_decimals: 2
    unit_of_measurement: "m³"
    device_class: gas
    state_class: total_increasing
    lambda: |-
      return id(gas_meter_totalm3);
  - platform: template
    name: ${friendly_name} m³ za deň
    id: gas_meter_total_m3_day
    unit_of_measurement: "m³"
    state_class: total_increasing
    accuracy_decimals: 2
    icon: mdi:gauge
    lambda: |-
      return (id(daily_value));
  - platform: template
    name: ${friendly_name} m³ za hodinu
    id: gas_meter_total_m3_hour
    unit_of_measurement: "m³"
    state_class: total_increasing
    accuracy_decimals: 2
    icon: mdi:gauge
    lambda: |-
      return (id(hour_value));
  - platform: template
    name: ${friendly_name} kWh celkovo
    id: gas_meter_total_kw
    accuracy_decimals: 3
    device_class: energy
    state_class: measurement
    unit_of_measurement: "kWh"
    lambda: |-
      return (id(gas_meter_totalm3) * id(m3kw));
  - platform: template
    name: ${friendly_name} kWh za deň
    id: gas_meter_total_kw_day
    unit_of_measurement: "kWh"
    state_class: measurement
    device_class: energy
    accuracy_decimals: 3
    lambda: |-
      return (id(daily_value) * id(m3kw));
  - platform: template
    name: ${friendly_name} kWh za hodinu
    id: gas_meter_total_kw_hour
    unit_of_measurement: "kWh"
    state_class: measurement
    device_class: energy
    accuracy_decimals: 3
    lambda: |-
      return (id(hour_value) * id(m3kw));