Switches from ESP32 cannot be switched in HA dashboard

Home Assistant 2023.6.1
Supervisor 2023.06.2
Operating System 10.2
Frontend 20230608.0 - latest
ESPHome 2023.5.5

I want to reset up a feedback cover which was already running as an end-stop cover before on an older esp32 and different hardwareon the cover itself.
Before starting the new project I was may be not properly deleting the old node from my system. I took a brand new board and flashed it. Given a different name it showed up in esphome and I could integrate it in ha, but I used almost the same yaml.
Now a very rare effect occurred in HA frontend. In my dashboard I could see all entities but two switches from the ESP32 showed status “on” while in the yaml they are set to “always off”. Then I switched them off one after the other and after a few seconds they toggled back into the on state one after the other. I changed the GPIOs and the same behaviour. After that I took it off, deleted it from my fritzbox, from HA esphome integration and from esphome I took the old esp32 installed the yaml on that one, integrated it into esphome and HA and both switches were on and showed the same strange behaviour. I tried a lot of deletings from HA and esphome, but the behaviour never changed. The two switches always toggle back to on after a few seconds. For more then a week long this is driving me crazy, because I could not find anything to explain what the reason may be.
I hope someone can help me or pointing into the right direction.

yaml, logs.

log recorded directly after flashing:

i:141]:   Using noise encryption: YES
53
[C][ap32
load:0x40080400,len:3036
entry 0x400805e4
[     6][D][esp32-hal-cpu.c:244] setCpuFrequencyMhz(): PLL: 480 / 2 = 240 Mhz, APB: 80000000 Hz
[I][logger:262]: Log initialized
[V][esp32.preferences:059]: nvs_get_blob('233825507'): ESP_i:141]:   Using noise encryption: YES
[D][api:102]: Accepted 192.168.188.100
[W][api.connection:075]: 192.168.188.100: Socket operation failed: BAD_INDICATOR errno=11
[V][api:114]: Removing connection to 192.168.188.100
[D][api:102]: Accepted 192.168.188.100
[V][api.connection:941]: Hello from client: 'aioesphomeapi (192.168.188.100)' | API Version 1.7
[W][api.connection:087]: aioesphomeapi (192.168.188.100): Connection closed
[D][api:102]: Accepted 192.168.188.100
[V][api:114]: Removing connection to aioesphomeapi (192.168.188.100)
[V][api.connection:941]: Hello from client: 'Home Assistant 2023.6.1 (192.168.188.100)' | API Version 1.7
[D][api.connection:959]: Home Assistant 2023.6.1 (192.168.188.100): Connected successfully
[D][api.connection:163]: Home Assistant 2023.6.1 (192.168.188.100) requested disconnected
[V][api:114]: Removing connection to Home Assistant 2023.6.1 (192.168.188.100)
[D][api:102]: Accepted 192.168.188.100
[V][api.connection:941]: Hello from client: 'Home Assistant 2023.6.1 (192.168.188.100)' | API Version 1.7
[D][api.connection:959]: Home Assistant 2023.6.1 (192.168.188.100): Connected successfully
[288717][V][WiFiGeneric.cpp:360] _arduino_event_cb(): STA Disconnected: SSID: FRITZBox-7590-5193180, BSSID: 3c:37:12:4b:39:76, Reason: 34
[288718][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 5 - STA_DISCONNECTED
[288726][W][WiFiGeneric.cpp:950] _eventCallback(): Reason: 34 - STA_STOP
[W][wifi_esp32:504]: Event: Disconnected ssid='FRITZBox-7590-5193180' bssid=3C:37:12:4B:39:76[redacted] reason='Unspecified'
[W][wifi:126]: WiFi Connection lost... Reconnecting...
[I][wifi:258]: WiFi Connecting to 'FRITZBox-7590-5193180'...
[V][wifi:260]: Connection Params:
[V][wifi:261]:   SSID: 'FRITZBox-7590-5193180'
[V][wifi:264]:   BSSID: 3C:37:12:4B:39:76
[V][wifi:284]:   Password: '84Ax&921727Po2764'[redacted]
[V][wifi:289]:   Channel: 6
[V][wifi:296]:   Manual IP: Static IP=192.168.188.103 Gateway=192.168.188.1 Subnet=255.255.255.0 DNS1=0.0.0.0 DNS2=0.0.0.0
[V][wifi:300]:   Hidden: NO
[V][component:204]: Component wifi took a long time for an operation (0.05 s).
[V][component:205]: Components should block for at most 20-30ms.
[W][api.connection:062]: Home Assistant 2023.6.1 (192.168.188.100): Network unavailable, disconnecting
[V][api:114]: Removing connection to Home Assistant 2023.6.1 (192.168.188.100)
[288905][V][WiFiGeneric.cpp:353] _arduino_event_cb(): STA Connected: SSID: FRITZBox-7590-5193180, BSSID: 3c:37:12:4b:39:76, Channel: 6, Auth: WPA2_WPA3_PSK
[288908][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 4 - STA_CONNECTED
[288911][V][WiFiGeneric.cpp:367] _arduino_event_cb(): STA Got Same IP:192.168.188.103
[V][wifi_esp32:487]: Event: Connected ssid='FRITZBox-7590-5193180' bssid=3C:37:12:4B:39:76[redacted] channel=6, authmode=UNKNOWN
[288943][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 7 - STA_GOT_IP
[288943][D][WiFiGeneric.cpp:991] _eventCallback(): STA IP: 192.168.188.103, MASK: 255.255.255.0, GW: 192.168.188.1
[V][wifi_esp32:546]: Event: Got IP static_ip=192.168.188.103 gateway=192.168.188.1
[I][wifi:519]: WiFi Connected!
[C][wifi:363]:   Local MAC: C8:F0:9E:F4:52:04
[C][wifi:364]:   SSID: 'FRITZBox-7590-5193180'[redacted]
[C][wifi:365]:   IP Address: 192.168.188.103
[C][wifi:367]:   BSSID: 3C:37:12:4B:39:76[redacted]
[C][wifi:368]:   Hostname: 'hoftor'
[C][wifi:370]:   Signal strength: -63 dB ▂▄▆█
[V][wifi:372]:   Priority: -2.0
[C][wifi:374]:   Channel: 6
[C][wifi:375]:   Subnet: 255.255.255.0
[C][wifi:376]:   Gateway: 192.168.188.1
[C][wifi:377]:   DNS1: 0.0.0.0
[C][wifi:378]:   DNS2: 0.0.0.0
[D][wifi:528]: Disabling AP...
[V][component:204]: Component wifi took a long time for an operation (0.07 s).
[V][component:205]: Components should block for at most 20-30ms.
[I][ota:113]: Boot seems successful, resetting boot loop counter.
[D][esp32.preferences:114]: Saving 1 preferences to flash...
[V][esp32.preferences:126]: sync: key: 233825507, len: 4
[D][esp32.preferences:143]: Saving 1 preferences to flash: 0 cached, 1 written, 0 failed
[D][api:102]: Accepted 192.168.188.100
[V][api.connection:941]: Hello from client: 'Home Assistant 2023.6.1 (192.168.188.100)' | API Version 1.7
[D][api.connection:959]: Home Assistant 2023.6.1 (192.168.188.100): Connected successfully
[D][switch:016]: 'Hoftor auf' Turning OFF.
[D][switch:016]: 'Hoftor ein' Turning OFF.
[D][switch:016]: 'Hoftor auf' Turning OFF.
[D][switch:016]: 'Hoftor ein' Turning OFF.
[D][switch:016]: 'Hoftor ein' Turning OFF.
[D][switch:016]: 'Hoftor auf' Turning OFF.
[D][switch:016]: 'Hoftor auf' Turning OFF.
[D][switch:016]: 'Hoftor ein' Turning OFF.
esphome:
  name: hoftor
  friendly_name: Hoftor

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger: 
  level: verbose

# Enable Home Assistant API
api:
  encryption:
    key: !secret api_password

ota:
  password: !secret esp32-hoftor_password  
wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  manual_ip:
    # To set a static IP for the ESP
    static_ip: 192.168.188.103
    gateway: 192.168.188.1
    subnet: 255.255.255.0

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: fba-hoftor_ssid
    password: fba-hoftor_password    

captive_portal:

# Definitionen der Schalter für den Optokoppler
switch:

# Tor an, Betrieb      
- platform: gpio
  name: "Hoftor ein"
  id: ht_on
  pin: 
    number: 33    # Schaltet das Hoftor ein:   Kanal 6
    inverted: false
    mode:
      output: true
  restore_mode: always off

  #Polwendung Auf oder Zu  
- platform: gpio
  name: "Hoftor auf"  
  id: ht_auf
  pin:
    number: 32  # Polwenderelais ein: Auf, aus: Zu:   Kanal 7
    inverted: false 
    mode:
      output: true
  restore_mode: always off

#Torgeschwindigkeit  
- platform: gpio
  name: "Hoftor speed"
  id: ht_speed
  pin: 
    number: 22   # 24V / 12V Umschaltung: Kanal 8
    inverted: false
    mode:
      output: true

# Warnleuchte
- platform: gpio
  name: "Warnleuchte"
  id: warnleuchte
  pin: 
    number: 13    # Schaltet die Warnleuchte ein: Kanal 5
    inverted: false
    mode:
      output: true
  restore_mode: always off  
  

binary_sensor:   

# Bewegungsrichtung ermitteln aus Schalterzuständen 

- platform: template
  name: "Tor oeffnet"
  id: 'tor_oeffnet'
  lambda: |-
    if ((id(ht_on).state = "on") && (id(ht_auf).state = "on")) {
      // Tor öffnet.
      return true;
    } else {
      // Tor bewegt sich nicht.
      return false;
    }

- platform: template
  name: "Tor schliesst"
  id: 'tor_schliesst'
  lambda: |-
    if ((id(ht_on).state = "on") && (id(ht_auf).state = "off")) {
      // Tor schliesst.
      return true;
    } else {
      // Tor bewegt sich nicht.
      return false;
    }

# Lichtschranke 
- platform: gpio
  pin:
    number: 27    # Lichtschranke schaltet bei Unterbrechung aus 
    mode:
      input: true
      pullup: true
    inverted: false
  name: Lichtschranke
  id: lichtschranke

# Endschalter   
- platform: gpio
  pin:
    number: 25     # Endschalter ganz zu 
    mode: INPUT_PULLUP 
    inverted: false 
  name: Endschalter_S
  id: hoftor_zu

- platform: gpio
  pin:
    number: 26    # Endschalter ganz offen
    mode: INPUT_PULLUP 
    inverted: false 
  name: Endschalter_N
  id: hoftor_offen

# Art der Hoftorsteuerung  
cover:
- platform: feedback
  name: Hoftor
  id: hoftor
  device_class: gate
  assumed_state: true
  has_built_in_endstop: false
  max_duration: 60s
  open_duration: 55s
  direction_change_wait_time: 500ms
#  open_sensor: tor_oeffnet
  open_endstop: hoftor_offen
  open_obstacle_sensor: lichtschranke
  close_duration: 30s
#  close_sensor: tor_schliesst
  close_endstop: hoftor_zu
  close_obstacle_sensor: lichtschranke 
  obstacle_rollback: 5%      

  open_action:
    - switch.turn_on: warnleuchte
    - delay: 2s
    - switch.turn_on: ht_auf
    - switch.turn_off: ht_speed
    - switch.turn_on: ht_on
  close_action:
    - switch.turn_on: warnleuchte
    - delay: 2s
    - switch.turn_off: ht_auf  # ausgeschaltet schließt das Tor
    - switch.turn_on: ht_on
  stop_action:
    - switch.turn_off: ht_on
    - switch.turn_off: ht_auf
    - switch.turn_off: ht_speed
    - delay: 1s
    - switch.turn_off: warnleuchte

log recorded wirelessly installed in place


INFO ESPHome 2023.5.5
INFO Reading configuration /config/esphome/Hoftor.yaml...
INFO Starting log output from 192.168.188.103 using esphome API
INFO Successfully connected to 192.168.188.103
[00:51:53][I][app:102]: ESPHome version 2023.5.5 compiled on Jun 15 2023, 18:04:10
[00:51:53][C][wifi:505]: WiFi:
[00:51:53][C][wifi:363]:   Local MAC: C8:F0:9E:F4:52:04
[00:51:53][C][wifi:364]:   SSID: [redacted]
[00:51:53][C][wifi:365]:   IP Address: 192.168.188.103
[00:51:53][C][wifi:367]:   BSSID: [redacted]
[00:51:53][C][wifi:368]:   Hostname: 'hoftor'
[00:51:53][C][wifi:370]:   Signal strength: -74 dB ▂▄▆█
[00:51:53][V][wifi:372]:   Priority: -3.0
[00:51:53][C][wifi:374]:   Channel: 6
[00:51:53][C][wifi:375]:   Subnet: 255.255.255.0
[00:51:53][C][wifi:376]:   Gateway: 192.168.188.1
[00:51:53][C][wifi:377]:   DNS1: 0.0.0.0
[00:51:53][C][wifi:378]:   DNS2: 0.0.0.0
[00:51:53][C][logger:301]: Logger:
[00:51:53][C][logger:302]:   Level: VERBOSE
[00:51:53][C][logger:303]:   Log Baud Rate: 115200
[00:51:53][C][logger:305]:   Hardware UART: UART0
[00:51:53][C][switch.gpio:068]: GPIO Switch 'Hoftor ein'
[00:51:53][C][switch.gpio:091]:   Restore Mode: always OFF
[00:51:53][C][switch.gpio:031]:   Pin: GPIO33
[00:51:53][C][switch.gpio:068]: GPIO Switch 'Hoftor auf'
[00:51:53][C][switch.gpio:091]:   Restore Mode: always OFF
[00:51:53][C][switch.gpio:031]:   Pin: GPIO32
[00:51:53][C][switch.gpio:068]: GPIO Switch 'Hoftor speed'
[00:51:53][C][switch.gpio:091]:   Restore Mode: always OFF
[00:51:53][C][switch.gpio:031]:   Pin: GPIO22
[00:51:53][C][switch.gpio:068]: GPIO Switch 'Warnleuchte'
[00:51:53][C][switch.gpio:091]:   Restore Mode: always OFF
[00:51:53][C][switch.gpio:031]:   Pin: GPIO13
[00:51:53][C][template.binary_sensor:018]: Template Binary Sensor 'Tor oeffnet'
[00:51:54][C][template.binary_sensor:018]: Template Binary Sensor 'Tor schliesst'
[00:51:54][C][gpio.binary_sensor:015]: GPIO Binary Sensor 'Lichtschranke'
[00:51:54][C][gpio.binary_sensor:016]:   Pin: GPIO27
[00:51:54][C][gpio.binary_sensor:015]: GPIO Binary Sensor 'Endschalter_S'
[00:51:54][C][gpio.binary_sensor:016]:   Pin: GPIO25
[00:51:54][C][gpio.binary_sensor:015]: GPIO Binary Sensor 'Endschalter_N'
[00:51:54][C][gpio.binary_sensor:016]:   Pin: GPIO26
[00:51:54][C][feedback.cover:052]: Endstop Cover 'Hoftor'
[00:51:54][C][feedback.cover:052]:   Assumed State: YES
[00:51:54][C][feedback.cover:052]:   Device Class: 'gate'
[00:51:54][C][feedback.cover:053]:   Open Duration: 55.0s
[00:51:54][C][feedback.cover:055]:   Open Endstop 'Endschalter_N'
[00:51:54][C][feedback.cover:057]:   Open Obstacle 'Lichtschranke'
[00:51:54][C][feedback.cover:059]:   Close Duration: 30.0s
[00:51:54][C][feedback.cover:061]:   Close Endstop 'Endschalter_S'
[00:51:54][C][feedback.cover:063]:   Close Obstacle 'Lichtschranke'
[00:51:54][C][feedback.cover:072]:   Max Duration: 60.0s
[00:51:54][C][feedback.cover:075]:   Direction change wait time: 0.5s
[00:51:54][C][feedback.cover:082]:   Obstacle rollback: 5.0%
[00:51:54][C][captive_portal:088]: Captive Portal:
[00:51:54][C][mdns:108]: mDNS:
[00:51:54][C][mdns:109]:   Hostname: hoftor
[00:51:54][V][mdns:110]:   Services:
[00:51:54][V][mdns:112]:   - _esphomelib, _tcp, 6053
[00:51:54][V][mdns:114]:     TXT: friendly_name = Hoftor
[00:51:54][V][mdns:114]:     TXT: version = 2023.5.5
[00:51:54][V][mdns:114]:     TXT: mac = c8f09ef45204
[00:51:54][V][mdns:114]:     TXT: platform = ESP32
[00:51:54][V][mdns:114]:     TXT: board = esp32dev
[00:51:54][V][mdns:114]:     TXT: network = wifi
[00:51:54][C][ota:093]: Over-The-Air Updates:
[00:51:54][C][ota:094]:   Address: 192.168.188.103:3232
[00:51:54][C][ota:097]:   Using Password.
[00:51:54][C][api:138]: API Server:
[00:51:54][C][api:139]:   Address: 192.168.188.103:6053
[00:51:54][C][api:141]:   Using noise encryption: YES
[00:52:17][D][cover:076]: 'Hoftor' - Setting
[00:52:17][D][cover:080]:   Command: STOP
[00:52:17][D][cover:170]: 'Hoftor' - Publishing:
[00:52:17][D][cover:173]:   Position: 100%
[00:52:17][D][cover:186]:   Current Operation: IDLE
[00:52:17][D][feedback.cover:385]: 'Hoftor' - Firing 'STOP' trigger.
[00:52:17][D][switch:016]: 'Hoftor ein' Turning OFF.
[00:52:17][D][switch:016]: 'Hoftor auf' Turning OFF.
[00:52:17][D][switch:016]: 'Hoftor speed' Turning OFF.
[00:52:18][D][switch:016]: 'Warnleuchte' Turning OFF.
[00:52:23][D][switch:016]: 'Hoftor auf' Turning OFF.
[00:52:25][D][switch:016]: 'Hoftor ein' Turning OFF.
[00:52:26][D][switch:012]: 'Hoftor speed' Turning ON.
[00:52:26][D][switch:055]: 'Hoftor speed': Sending state ON
[00:52:28][D][switch:012]: 'Warnleuchte' Turning ON.
[00:52:28][D][switch:055]: 'Warnleuchte': Sending state ON
[00:52:30][D][switch:016]: 'Warnleuchte' Turning OFF.
[00:52:30][D][switch:055]: 'Warnleuchte': Sending state OFF
[00:52:32][D][switch:016]: 'Hoftor speed' Turning OFF.
[00:52:32][D][switch:055]: 'Hoftor speed': Sending state OFF
[00:52:33][D][switch:016]: 'Hoftor ein' Turning OFF.
[00:52:35][D][switch:016]: 'Hoftor auf' Turning OFF.
[00:52:57][D][esp32.preferences:114]: Saving 1 preferences to flash...
[00:52:57][V][esp32.preferences:137]: NVS data not changed skipping 2617425443  len=8
[00:52:58][D][esp32.preferences:143]: Saving 1 preferences to flash: 1 cached, 0 written, 0 failed

The differences between the different switches are some sending feedback “Sending state …”, but the two switches ‘Hoftor auf’ and ‘Hoftor ein’ they do not send a feedback.

By the way the two template binary sensors “Tor schliesst” and “Tor oeffnet” are not giving the expected result. I think the lambda is correct. May be this has also to do with the same issue.

Hello nickrout,
do you have any ideas what causes this rare behavior of the switches? I appreciate any help, thank you in advance.