ESPHome Causing Connection Loss

This is the code:

substitutions:
  node_name: nspanel-workroom
  device_name: Workroom NSPanel
  conf_wifi_ssid: !secret wifi_ssid
  conf_password: !secret wifi_password
  conf_static_ip: xxx.xxx.xxx.xxx
  conf_gateway: yyy.yyy.yyy.yyy
  conf_subnet: zzz.zzz.zzz.zzz
  conf_current_temperature_ha_id: sensor.work_room_th_temperature
  conf_target_temperature_ha_id: sensor.termostato_studio_set_temp
  conf_thermostat_mode_ha_id: sensor.termostato_studio_hvac_mode

# Note: this may not be needed if the pull request has been merged.
# Check https://github.com/esphome/esphome/pull/2956 for current status.
external_components:
  - source: github://pr#2956
    components: [nextion]
    refresh: 0h

esphome:
  name: $node_name
  comment: $device_name
  on_loop:
    then:
      if:
        condition:
          - binary_sensor.is_on: alarm_sound
          - not:
             rtttl.is_playing
        then:
          - rtttl.play: "siren:d=8,o=5,b=100:d,e,d,e,d,e,d,e"

esp32:
  board: esp32dev

wifi:
  ssid: $conf_wifi_ssid
  password: $conf_password
#  power_save_mode: none
  fast_connect: true
  manual_ip:
    static_ip: $conf_static_ip
    gateway: $conf_gateway
    subnet: $conf_subnet

# Enable logging
logger:

# Enable wireless updates
ota:

# Enable Home Assistant API
api:
  services:
    # Service to play a song
    - service: play_rtttl
      variables:
        song_str: string
      then:
        - rtttl.play:
            rtttl: !lambda 'return song_str;'
    - service: upload_tft
      then:
        - lambda: 'id(disp)->upload_tft();'

# A reboot button is always useful
button:
  - platform: restart
    name: Restart $device_name
  - platform: template
    name: $device_name TFT Upload
    device_class: update
    on_press:
      - lambda: 'id(disp).upload_tft();'

sensor:
  - platform: wifi_signal
    name: $device_name WiFi Signal
    update_interval: 60s

  - platform: ntc
    id: temperature
    sensor: resistance_sensor
    calibration:
      b_constant: 3950
      reference_temperature: 25°C
      reference_resistance: 10kOhm
    name: $device_name Temperature

  - platform: resistance
    id: resistance_sensor
    sensor: ntc_source
    configuration: DOWNSTREAM
    resistor: 11.2kOhm

  - platform: adc
    id: ntc_source
    pin: 38
    update_interval: 60s
    attenuation: 11db

    ### ENERGY PRODUCED #######
  - platform: homeassistant
    id: power_produced
    entity_id: sensor.inverter_current_produced
    on_value:
      then:
        - lambda: 'id(disp).set_component_text_printf("energy.p_solar", "%.3f KW", x);'
    ### ENERGY EXPORTED #######
  - platform: homeassistant
    id: power_exported
    entity_id: sensor.inverter_power_meter_active_power
    on_value:
      then:
        - lambda: 'id(disp).set_component_text_printf("energy.p_export", "%.3f KW", x);'
    ### ENERGY LOAD #######
  - platform: homeassistant
    id: power_load
    entity_id: sensor.inverter_home_load
    on_value:
      then:
        - lambda: 'id(disp).set_component_text_printf("energy.p_load", "%.3f KW", x);'


  # Grab current temperature from Home Assistant
  - platform: homeassistant
    id: current_temperature_ha
    entity_id: $conf_current_temperature_ha_id
    on_value:
      then:
        - lambda: 'id(disp).set_component_text_printf("thermostat.temp", "%.1f ""\xb0""C", x);'
  # get target termperature from ha
  - platform: homeassistant
    id: target_temperature_ha
    entity_id: $conf_target_temperature_ha_id
    on_value:
      then:
        lambda: |-
          id(disp).set_component_text_printf("thermostat.t_temp", "%.1f ""\xb0""C", x);
          id(set_temp_dev) = x;
  # set target temperature to/for ha
  - platform: template
    id: target_temperature
    name: $device_name target_temperature
    lambda: |-
        return id(set_temp_dev);

text_sensor:
  # get thermostat mode from ha
  - platform: homeassistant
    id: thermostat_mode_ha
    entity_id: $conf_thermostat_mode_ha_id
    on_value:
      then:
        lambda: |-
          id(disp).set_component_text_printf("thermostat.t_mode", "%s", x.c_str());
          id(thermostat_mode).publish_state(id(thermostat_mode_ha).state);
  # set thermostat_mode to/for ha
  - platform: template
    id: thermostat_mode
    name: $device_name thermostat_mode
    lambda: |-
        return id(thermostat_mode_ha).state;
binary_sensor:
  # alarm sound
  - platform: homeassistant
    id: alarm_sound
    entity_id: input_boolean.alarm_sound
    on_state:
      then:
        if:
          condition:
            - binary_sensor.is_on: alarm_sound
          then:
            - lambda: |-
               id(disp).send_command_printf("sleep=0");
               id(disp).goto_page("alarm");
            - rtttl.play: "siren:d=8,o=5,b=100:d,e,d,e,d,e,d,e"
          else:
            - rtttl.stop
            - lambda: id(disp).goto_page("thermostat");
  # button t_mote
  - platform: nextion
    id: t_mode
    page_id: 0
    component_id: 8
    on_press:
      then:
        - lambda: |-
            if (strcmp(id(thermostat_mode_ha).state.c_str(), "heat") == 0) {
              id(thermostat_mode).publish_state("off");
            } else if (strcmp(id(thermostat_mode_ha).state.c_str(), "off") == 0) {
              id(thermostat_mode).publish_state("heat");
            }
  # button thermostat relay onoff
  - platform: nextion
    id: btn_onoff
    page_id: 0
    component_id: 9
    on_press:
      then:
        - switch.toggle: relay_1
  # button temperature up
  - platform: nextion
    id: t_up
    page_id: 0
    component_id: 3
    on_press:
      lambda: |-
         if( id(set_temp_dev) < 31){
          id(set_temp_dev) = id(set_temp_dev) + 0.1;
          id(target_temperature).publish_state(id(set_temp_dev));
         }
  # button temperature down
  - platform: nextion
    id: t_down
    page_id: 0
    component_id: 4
    on_press:
      lambda: |-
        if( id(set_temp_dev) > 16){
          id(set_temp_dev) = id(set_temp_dev) - 0.1;
          id(target_temperature).publish_state(id(set_temp_dev));
        }
  - platform: gpio
    name: $device_name Down Button
    pin:
      number: 14
      inverted: true
    on_click:
      - switch.toggle: relay_1
  - platform: gpio
    name: $device_name Up Button
    pin:
      number: 27
      inverted: true
    on_click:
      - switch.toggle: relay_2

# Define some outputs
switch:
  # The two relays
  - platform: gpio
    name: $device_name Relay 1
    id: relay_1
    pin:
      number: 22
    on_turn_on: 
      then:
        lambda: |-
          id(disp).send_command_printf("thermostat.btn_onoff.pic=2");
    on_turn_off: 
      then:
        lambda: |-
          id(disp).send_command_printf("thermostat.btn_onoff.pic=3");
  - platform: gpio
    name: $device_name Relay 2
    id: relay_2
    pin:
      number: 19

  # Pin 4 always needs to be on to power up the display
  - platform: gpio
    id: screen_power
    entity_category: config
    pin:
      number: 4
      inverted: true
    restore_mode: ALWAYS_ON

number:
  platform: template
  name: $device_name Brightness
  id: brightness
  entity_category: config
  unit_of_measurement: '%'
  min_value: 0
  max_value: 100
  step: 1
  initial_value: 30
  set_action:
    then:
      - lambda: 'id(disp).set_backlight_brightness(x/100);'

time:
  - platform: homeassistant
    id: homeassistant_time      
    timezone: Europe/Rome
    on_time:
      - seconds: 0
        minutes: '*'
        then:
          - lambda: |-
              auto time_text = id(homeassistant_time).now().strftime("%d/%m/%Y %H:%M");
              id(disp).set_component_text("thermostat.datetime", time_text.c_str());

# Configure the internal bleeper
output:
  - platform: ledc
    id: buzzer_out
    pin:
      number: 21

# Enable ringtone music support
rtttl:
  id: buzzer
  output: buzzer_out

# Configure UART for communicating with the screen
uart:
  id: tf_uart
  tx_pin: 16
  rx_pin: 17
  #baud_rate: 9600
  baud_rate: 115200

# Configure the screen itself
display:
  - platform: nextion
    id: disp
    uart_id: tf_uart
    tft_url: http://192.168.178.126:8123/local/nspanel/nspanel_custom.tft
    # A little fun...
    on_setup:
      then:
        - number.set:
            id: brightness
            value: 30
        - script.execute: update_data
#        - rtttl.play: "twobits:d=4,o=5,b=220:c6,8g,8g,a,g,p,b,c6"
    on_wake:
      then:
        - script.execute: update_data
script:
  - id: update_data
    then:
        - lambda: |-
              auto time_text = id(homeassistant_time).now().strftime("%d/%m/%Y %H:%M");
              id(disp).set_component_text("datetime", time_text.c_str());
              id(disp).set_component_text_printf("thermostat.temp", "%.1f ""\xb0""C", id(current_temperature_ha).state);
              id(disp).set_component_text_printf("thermostat.t_temp", "%.1f ""\xb0""C", id(target_temperature_ha).state);
              id(disp).set_component_text_printf("thermostat.t_mode", "%s", id(thermostat_mode_ha).state.c_str());
              id(set_temp_dev) = id(target_temperature_ha).state;
              id(disp).set_component_text_printf("energy.p_solar", "%.3f KW", id(power_produced).state);
              id(disp).set_component_text_printf("energy.p_export", "%.3f KW", id(power_exported).state);
              id(disp).set_component_text_printf("energy.p_load", "%.3f KW", id(power_load).state);
        - if:
            condition:
              - switch.is_on: relay_1
            then:
              - lambda: |-
                  id(disp).send_command_printf("thermostat.btn_onoff.pic=2");
            else:
              - lambda: |-
                  id(disp).send_command_printf("thermostat.btn_onoff.pic=3");


globals:
  - id: set_temp_dev
    type: float
    initial_value: id(target_temperature_ha).state
    restore_value: no