ESP32 and relay abnormal behavior

Yesterday, wanting to optimize the management of my aquarium, I programmed an ESP32 with esphome to use together with a display (model ST7735S), two temperature probes (models DS18B20) and a board with four relays (model Relay Module HL-545). Everything works fine except the relays that turn on and off continuously and very quickly (they seem to have gone crazy - HERE A VIDEO). What could it depend on? Is there a remedy? I also attach the script I created:

esphome:
  name: acquario
  friendly_name: Acquario

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "ozvPV2wyKf+udUw/+d7mKFUWLqmGutMGeHqYo6MGDgI="

ota:
  - platform: esphome
    password: "77dcc783e1eff9029aa4bb80054339c7"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  manual_ip: 
    static_ip: 192.168.178.150
    gateway: 192.168.178.1
    subnet: 255.255.255.0
    dns1: 192.168.178.1
spi:
  clk_pin: GPIO18
  mosi_pin: GPIO23
  # MISO non è usato per il display ST7735S

color:
  - id: my_red
    red: 100%
    green: 3%
    blue: 5%
  - id: my_blu
    red: 5%
    green: 3%
    blue: 100%
  - id: my_green
    red: 5%
    green: 100%
    blue: 3%
  - id: my_yellow
    hex: ffff00
  - id: my_fucsia
    hex: f400a1
  - id: my_orange
    hex: b27300

display:
  - platform: ili9xxx
    model: ST7735
    cs_pin: GPIO5
    dc_pin: GPIO2
    reset_pin: GPIO4
    rotation: 180
    invert_colors: false
    # show_test_card: true
    update_interval: 1s
    


    
    lambda: |-
      
      it.print(25, 0, id(fonts1), id(my_fucsia), "ACQUARIO:");
      it.rectangle (0, 17.5, 128, 75, id(my_yellow));
      it.print(5, 19, id(fonts), id(my_blu), "Luci:");
      if (id(relay_1).state) {
        it.print(67, 19, id(fonts), id(my_green), "ON");
      } else {
        it.print(67, 19, id(fonts), id(my_red), "OFF");
      }
      it.print(5, 35, id(fonts), id(my_blu), "Filtro:");
      if (id(relay_2).state) {
        it.print(67, 35, id(fonts), id(my_green), "ON");
      } else {
        it.print(67, 35, id(fonts), id(my_red), "OFF");
      }
      it.print(5, 51, id(fonts), id(my_blu), "Aria:");
      if (id(relay_3).state) {
        it.print(67, 51, id(fonts), id(my_green), "ON");
      } else {
        it.print(67, 50, id(fonts), id(my_red), "OFF");
      }
      it.print(5, 67, id(fonts), id(my_blu), "Riscald.:");
      if (id(relay_4).state) {
        it.print(67, 67, id(fonts), id(my_green), "ON");
      } else {
        it.print(67, 67, id(fonts), id(my_red), "OFF");
      }
        it.rectangle (0, 90, 128, 25, id(my_yellow));
        it.rectangle (0, 90, 128, 70, id(my_yellow));

      if (id(water_temp).state > 23, id(water_temp).state < 27) {
        it.printf(5, 120, id(fonts1), id(my_green), "Temp: %.1f°", id(water_temp).state);
      }
       if (id(water_temp).state < 23) {
        it.printf(5, 120, id(fonts1), id(my_orange), "Temp: %.1f°", id(water_temp).state);
      }
       if (id(water_temp).state > 27) {
        it.printf(5, 120, id(fonts1), id(my_red), "Temp: %.1f°", id(water_temp).state);
      }
    
        
      


font:
  - file: "fonts/arial.ttf"
    id: fonts
    size: 16
  - file: "fonts/arial.ttf"
    id: fonts1
    size: 14

# Definizione dei relè e interruttori
switch:
  - platform: gpio
    pin: GPIO13
    id: relay_1
    name: "Relay 1"
  - platform: gpio
    pin: GPIO12
    id: relay_2
    name: "Relay 2"
  - platform: gpio
    pin: GPIO14
    id: relay_3
    name: "Relay 3"
  - platform: gpio
    pin: GPIO27
    id: relay_4
    name: "Relay 4"

binary_sensor:
  - platform: gpio
    pin: GPIO32
    id: switch_1
    name: "Interruttore 1"
    on_press:
      - switch.toggle: relay_1
  - platform: gpio
    pin: GPIO33
    id: switch_2
    name: "Interruttore 2"
    on_press:
      - switch.toggle: relay_2
  - platform: gpio
    pin: GPIO25
    id: switch_3
    name: "Interruttore 3"
    on_press:
      - switch.toggle: relay_3
  - platform: gpio
    pin: GPIO26
    id: switch_4
    name: "Interruttore 4"
    on_press:
      - switch.toggle: relay_4

sensor:
  - platform: dallas_temp
    address: 0x7d01131bc3749c28
    name: "Temperatura Acqua"
    id: water_temp
    update_interval: 5s
  - platform: dallas_temp
    address: 0x9401131bbc62f728
    name: "Temperatura Ambiente"
    id: temp_ambiente
    update_interval: 5s


one_wire:
  - platform: gpio
    pin: GPIO22

I hope you can help me.
Bye and thanks

What are these?
You need pullups/downs and some filtering there.

I wanted to read the status of the relays on the display (if you see the video you will understand better what I mean).
My intention is to read the status of each relay, the water temperature and the ambient temperature on the display.

Doesn’t make any sense to me.
If your relays are controlled by Esphome you already have the state.
But now you have gpios configured to change relay state every time they go high, either intentionally or just randomly as they are floating pins.

Edit:
You actually even correctly read the relay state:

Your binary sensors do nothing else than spam the relays.

Thank you very much to everyone who helped me, now thanks to your advice, everything works fine and I have even programmed four PINS to put 4 physical touch buttons and control the aquarium not necessarily from Home Assistant.
I write the code below:

esphome:
  name: acquario
  friendly_name: acquario

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

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

ota:
  - platform: esphome
    password: "84e5766bcd075403bba8c653b4cedaa9"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  manual_ip: 
    static_ip: 192.168.178.150
    gateway: 192.168.178.1
    subnet: 255.255.255.0
    dns1: 192.168.178.1

captive_portal:
  
spi:
  clk_pin: GPIO18
  mosi_pin: GPIO23
  # MISO non è usato per il display ST7735S

color:
  - id: my_red
    red: 100%
    green: 3%
    blue: 5%
  - id: my_blu
    red: 5%
    green: 3%
    blue: 100%
  - id: my_green
    red: 5%
    green: 100%
    blue: 3%
  - id: my_yellow
    hex: ffff00
  - id: my_fucsia
    hex: f400a1
  - id: my_orange
    hex: b27300

display:
  - platform: ili9xxx
    model: ST7735
    cs_pin: GPIO5
    dc_pin: GPIO2
    reset_pin: GPIO4
    rotation: 180
    invert_colors: false
    # show_test_card: true
    update_interval: 1s
    


    
    lambda: |-
      
      it.print(25, 0, id(fonts1), id(my_fucsia), "ACQUARIO:");
      it.rectangle (0, 17.5, 128, 75, id(my_yellow));
      it.print(5, 19, id(fonts), id(my_blu), "Luci:");
      if (id(relay_1).state) {
        it.print(67, 19, id(fonts), id(my_green), "ON");
      } else {
        it.print(67, 19, id(fonts), id(my_red), "OFF");
      }
      it.print(5, 35, id(fonts), id(my_blu), "Filtro:");
      if (id(relay_2).state) {
        it.print(67, 35, id(fonts), id(my_green), "ON");
      } else {
        it.print(67, 35, id(fonts), id(my_red), "OFF");
      }
      it.print(5, 51, id(fonts), id(my_blu), "Aria:");
      if (id(relay_3).state) {
        it.print(67, 51, id(fonts), id(my_green), "ON");
      } else {
        it.print(67, 50, id(fonts), id(my_red), "OFF");
      }
      it.print(5, 67, id(fonts), id(my_blu), "Riscald.:");
      if (id(relay_4).state) {
        it.print(67, 67, id(fonts), id(my_green), "ON");
      } else {
        it.print(67, 67, id(fonts), id(my_red), "OFF");
      }
        it.rectangle (0, 90, 128, 25, id(my_yellow));
        it.rectangle (0, 90, 128, 70, id(my_yellow));

        it.printf(5, 96, id(fonts1), "TDS: %.2f ppm", id(tds_sensor).state);


      if (id(water_temp).state > 23, id(water_temp).state < 27) {
        it.printf(5, 120, id(fonts1), id(my_green), "Temp: %.1f°", id(water_temp).state);
      }
       if (id(water_temp).state < 23) {
        it.printf(5, 120, id(fonts1), id(my_orange), "Temp: %.1f°", id(water_temp).state);
      }
       if (id(water_temp).state > 27) {
        it.printf(5, 120, id(fonts1), id(my_red), "T. Acqua: %.1f°", id(water_temp).state);
      }
       it.printf(5, 140, id(fonts1), id(my_green), "T. Ambiente: %.1f°", id(temp_ambiente).state);


    
        
      


font:
  - file: "fonts/arial.ttf"
    id: fonts
    size: 16
  - file: "fonts/arial.ttf"
    id: fonts1
    size: 14

# Definizione dei relè e interruttori
switch:
  - platform: gpio
    pin: GPIO13
    id: relay_1
    name: "Luci"
  - platform: gpio
    pin: GPIO12
    id: relay_2
    name: "Filtro"
  - platform: gpio
    pin: GPIO14
    id: relay_3
    name: "Aria"
  - platform: gpio
    pin: GPIO27
    id: relay_4
    name: "Riscaldatore"

binary_sensor:
  - platform: gpio
    name: "Interruttore Luci"
    pin: 
      number: GPIO32 # Interruttore Luci
      mode: 
        input: true
        pullup: true
      inverted: true
    on_press:
      then:
        - switch.turn_on: relay_1
    on_release: 
      then:
        - switch.turn_off: relay_1

  - platform: gpio
    pin: 
      number: GPIO33 #Interruttore Filtro
      mode: 
        input: true
        pullup: true
      inverted: true
    name: "Interruttore Filtro"
    on_press:
      then:
        - switch.turn_on: relay_2
    on_release: 
      then:
        - switch.turn_off: relay_2

  - platform: gpio
    pin: 
      number: GPIO25 #Interruttore Aria
      mode: 
        input: true
        pullup: true
      inverted: true
    name: "Interruttore Aria"
    on_press:
      then:
        - switch.turn_on: relay_3
    on_release: 
      then:
        - switch.turn_off: relay_3

  - platform: gpio
    pin: 
      number: GPIO26 #Interruttore Riscaldatore
      mode: 
        input: true
        pullup: true
      inverted: true
    name: "Interruttore Riscaldatore"
    on_press:
      then:
        - switch.turn_on: relay_4
    on_release: 
      then:
        - switch.turn_off: relay_4


sensor:
  - platform: dallas_temp
    address: 0x7d01131bc3749c28
    name: "Temperatura Acqua"
    id: water_temp
    update_interval: 5s
  - platform: dallas_temp
    address: 0x9401131bbc62f728
    name: "Temperatura Ambiente"
    id: temp_ambiente
    update_interval: 5s

  - platform: adc
    pin: 34
    name: "TDS Sensor"
    id: tds_sensor
    update_interval: 5s
    attenuation: 11db
    filters:
      - multiply: 1.0  # Regolare in base alla calibrazione

  - platform: template
    name: "TDS Value"
    lambda: "return id(tds_sensor).state * 500.0 / 1024.0;"
    unit_of_measurement: "ppm"
    update_interval: 5s
    accuracy_decimals: 2



one_wire:
  - platform: gpio
    pin: GPIO22
web_server:
  port: 80


You are welcome.
Add a small debounce to your buttons:

filters:
  - delayed_on_off: 50ms
1 Like