Binary sensor - flashing status from ON to OFF

Hi Folks,
i would like to ask you for help with binary sensors for my heating valves. Valve have three way endstop where i am able to get change status from Open valve to Close valve or vice versa (i can use even just one contact from endstop). The end stop is connected to mcp23017. In code is also part with Cover control. If i make manual change (open to close or close to open) everything work fine. Even automation working fine but just while (random time from 1hour to 3hours) and then ESP just freez and binary sensotrs start switching from active to inactiv status. Logs from EPS works and i can see even data from temperature sensors, but i am not able to control ESP from HA and need reboot ESP. Could be cause with some interference? Valces are under 220V … power cable and data cable (not shielded) are adjacent. If somebody hade same issue or know how to help, i will be glad for any idea

thank you

ESP

esphome:
  name: sh-newtest
  platform: ESP32
  board: lolin_d32

i2c:
  sda: 21
  scl: 22
  scan: True
 
mcp23017:
  - id: 'mcp23017_hub'
    address: 0x20

binary_sensor:
  - platform: gpio
    #endstop for heating bathroom ON status
    name: "koncak_topeni_koupelna ON"
    id: koncak_topeni_koupelna_ON
    pin:
      mcp23xxx: mcp23017_hub
      number: 12
      mode: INPUT_PULLUP
      
  - platform: gpio
    #endstop for heating bathroom OFF status
    name: "koncak_topeni_koupelna OFF"
    id: koncak_topeni_koupelna_OFF
    pin:
      mcp23xxx: mcp23017_hub
      number: 13
      mode: INPUT_PULLUP
 
  - platform: gpio
    #endstop for heating kitchen ON status
    name: "koncak_topeni_kuchyne ON"
    id: koncak_topeni_kuchyne_ON
    pin:
      mcp23xxx: mcp23017_hub
      number: 14
      mode: INPUT_PULLUP
      
  - platform: gpio
    #endstop for heating kitchen OFF status  
    name: "koncak_topeni_kuchyne OFF"
    id: koncak_topeni_kuchyne_OFF
    pin:
      mcp23xxx: mcp23017_hub
      number: 15
      mode: INPUT_PULLUP 
      
cover:
  - platform: endstop
    #cover for bathroom
    name: "Topeni Koupelna"
    id: topeni_koupelna
    open_action:
      - switch.turn_on: kohout_topeni_koupelna
    open_duration: 15s
    open_endstop: koncak_topeni_koupelna_ON

    close_action:
      - switch.turn_on: kohout_topeni_koupelna
    close_duration: 15s
    close_endstop: koncak_topeni_koupelna_OFF
    stop_action:
     # - switch.turn_off: kohout_topeni_koupelna
      - delay: 1.5s
      - switch.turn_off: kohout_topeni_koupelna
    max_duration: 20s

automation

alias: test
description: test
trigger:
  - platform: time_pattern
    minutes: /1
condition:
  - condition: device
    device_id: 4d7664d5c32040875cc882f7ffb3c548
    domain: cover
    entity_id: cover.topeni_koupelna
    type: is_closed
action:
  - device_id: 4d7664d5c32040875cc882f7ffb3c548
    domain: cover
    entity_id: cover.topeni_koupelna
    type: open
mode: single


alias: test2
description: test2
trigger:
  - platform: time_pattern
    minutes: /1
condition:
  - condition: device
    device_id: 4d7664d5c32040875cc882f7ffb3c548
    domain: cover
    entity_id: cover.topeni_koupelna
    type: is_open
action:
  - device_id: 4d7664d5c32040875cc882f7ffb3c548
    domain: cover
    entity_id: cover.topeni_koupelna
    type: close
mode: single

result of blinking binary sensor from active to inactive status

binary_sensor