ESPHome Causing Connection Loss

This is the point where it loses connection. Logs from compile below:

INFO Reading configuration /config/esphome/nspanel-garage.yaml...
INFO Updating https://github.com/esphome/esphome.git@pull/2956/head
INFO Detected timezone 'Europe/London'
WARNING GPIO4 is a Strapping PIN and should be avoided.
Attaching external pullup/down resistors to strapping pins can cause unexpected failures.
See https://esphome.io/guides/faq.html#why-am-i-getting-a-warning-about-strapping-pins
INFO Generating C++ source...
INFO Compiling app...
Processing nspanel (board: esp32dev; framework: arduino; platform: platformio/espressif32 @ 3.5.0)
--------------------------------------------------------------------------------
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
Dependency Graph
|-- <WiFi> 1.0
|-- <ESPmDNS> 1.0
|-- <Update> 1.0
|-- <WiFiClientSecure> 1.0
|-- <HTTPClient> 1.2
Compiling /data/nspanel/.pioenvs/nspanel/src/esphome/components/adc/adc_sensor.cpp.o
Compiling /data/nspanel/.pioenvs/nspanel/src/esphome/components/api/api_connection.cpp.o
Compiling /data/nspanel/.pioenvs/nspanel/src/esphome/components/api/api_frame_helper.cpp.o
Compiling /data/nspanel/.pioenvs/nspanel/src/esphome/components/api/api_pb2.cpp.o

Anyone know how I could program this device without using Home Assistant? Is there a standalone compiler and flashing tool?

How do you know? I don’t see anything in the logs that indicates a problem. (But I would be the last person on these forums to trust to interpret the log files).

Have you successfully flashed any other devices, or is this the first?

Thats the point at which “Connection Lost. Reconnecting…” appears in the bottom left (sreenshot).

This is my first foray in to ESP32

That is the least of your problem, are you losing connection to HA only while compiling in esphome?

ConfigurationSettingsLogs

You can use Python on a PC, see Installing ESPHome Manually, then upload with ESPHome Web, or esphome-flasher.

Yep. Only when compiling. And happens every time without fail at the same point.

Power or resource issue?

Again.
Have you successfully flashed any other devices, or is this the first?

No - first ESP32 experience

Resource is my first instinct too, but Rasp Pi 3 should be ok for this right?

I am the wrong person to ask this. I haven’t used a Raspberry Pi for years.

Compiling is generally CPU intensive, so a marginal power supply could cause it to brownout or reboot. Which would disconnect the browser.

Do you have any other ESP boards? My go-to board is the Wemos D1 Mini ($5 at Amazon). I have a couple of ESP32 boards in my workshop, but I haven’t put any of them into a project.

I would get another board and create a “new device” in ESPHome. Don’t add any code, just install it. Does the problem repeat?

Not necessarily, as @stevemann mentioned “Compiling is generally CPU intensive”, it’s been quite a few years since I upgraded from a Pi3, but I do remember I it had countless issues, what is the PSU power rating used for the Pi?

Have you had a look at your logs, or the CPU usage history for the time frame during compiling?

You can try a new yaml file with just the basics and see if it compiles, in Esphome, click “New Device”, enter any name, choose your board type, and install.

Another thing to try is, keep a constant ping on the Pi and see if it times out while compiling, this will tell you if the Pi is rebooting or if HA is just hanging, also, you should be able to tell by checking your “last boot /uptime sensors”, assuming you have integrated your system sensors.

Which ESP32 board are you using specifically?

Thanks for advice guys. That gives me a few things to check and try this evening.
If this turns out to be an under-sized PSU issue that would be a nice easy fix. If its CPU… well… time to try something new.

Whats annoying is the Pi3 was my back-up choice. I had already built a HA system on my server running as a virtual machine, but I couldn’t get my conbee stick to pass through and work on that!

This is the Sonoff NSPanel that I want to flash with ESP Home. But obviously I haven’t yet got to the point of connecting that to the PC yet!

So the ping never drops out during compiling even when the browser reports a connection loss.
I have also used a beefier PSU for the Pi and that has not helped.

Seems it is resource.
How can I compile this externally to HA?

Also logs:

Logger: homeassistant.components.recorder.util
Source: components/recorder/util.py:412
Integration: Recorder (documentation, issues)
First occurred: 10:09:30 AM (1 occurrences)
Last logged: 10:09:30 AM

Ended unfinished session (id=52 from 2022-04-08 09:05:31.693657)
Logger: homeassistant.components.recorder.util
Source: components/recorder/util.py:254
Integration: Recorder (documentation, issues)
First occurred: 10:09:28 AM (1 occurrences)
Last logged: 10:09:28 AM

The system could not validate that the sqlite3 database at //config/home-assistant_v2.db was shutdown cleanly

I had the same issues with my Pi3. It worked for a minimal YAML (but already took AGES), but once I started adding features the Pi always crashed during compiling. I’m now using the esphome commandline on a windows 10 laptop, I outlined my setup in I have ESP32 connected to my win10 via USB and connection works - now what? - #3 by Lakini

This looks like exactly what I need!
I’ll give this a whirl and report back!
Thank you!!!

hi. did you find the cause of the problem?

I have the similar problem.
I have six nspanel with the same problem… every about two hours, they disconnect from network and after 10 minutes they reboot… They are connect to different wifi network but the problem is same…

This is a verbose log by serial usb:

14:00:57.258 -> [6264794][V][WiFiGeneric.cpp:360] _arduino_event_cb(): STA Disconnected: SSID: wifiNetwork, BSSID: bssidNetwork, Reason: 200
14:00:57.258 -> [6264796][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 5 - STA_DISCONNECTED
14:00:57.305 -> [6264803][W][WiFiGeneric.cpp:950] _eventCallback(): Reason: 200 - BEACON_TIMEOUT
14:00:57.305 -> [6264810][D][WiFiGeneric.cpp:966] _eventCallback(): WiFi Reconnect Running
14:00:57.305 -> e[0;33m[W][wifi:125]: WiFi Connection lost... Reconnecting...e[0m[6264818][V][WiFiGeneric.cpp:97] set_esp_interface_ip(): Configuring Station static IP: 0.0.0.0, MASK: 0.0.0.0, GW: 0.0.0.0
14:00:57.305 -> 
14:00:57.305 -> e[0;33m[W][wifi:585]: Restarting WiFi adapter...e[0m
14:00:57.352 -> e[0;37m[V][wifi_esp32:041]: Disabling STA.e[0m
14:00:57.352 -> [6264856][V][WiFiGeneric.cpp:341] _arduino_event_cb(): STA Stopped
14:00:57.352 -> [6264858][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 3 - STA_STOP
14:00:57.352 -> e[0;37m[V][wifi_esp32:464]: Event: WiFi STA stope[0m
14:00:57.433 -> e[0;32m[I][wifi:257]: WiFi Connecting to 'wifiNetwork'...e[0m
14:00:57.433 -> e[0;37m[V][wifi:259]: Connection Params:e[0m
14:00:57.433 -> e[0;37m[V][wifi:260]:   SSID: 'wifiNetwork'e[0m
14:00:57.433 -> e[0;37m[V][wifi:265]:   BSSID: Not Sete[0m
14:00:57.433 -> e[0;37m[V][wifi:283]:   Password: e[5m'wifiPassword'e[6me[0m
14:00:57.480 -> e[0;37m[V][wifi:290]:   Channel: Not Sete[0m
14:00:57.480 -> e[0;37m[V][wifi:295]:   Manual IP: Static IP=192.168.178.186 Gateway=192.168.178.1 Subnet=255.255.255.0 DNS1=0.0.0.0 DNS2=0.0.0.0e[0m
14:00:57.480 -> e[0;37m[V][wifi:299]:   Hidden: NOe[0m
14:00:57.480 -> e[0;37m[V][wifi_esp32:039]: Enabling STA.e[0m
14:00:57.480 -> [6265015][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 0 - WIFI_READY
14:00:57.480 -> e[0;37m[V][wifi_esp32:444]: Event: WiFi readye[0m
14:00:57.480 -> [6265020][V][WiFiGeneric.cpp:338] _arduino_event_cb(): STA Started
14:00:57.480 -> e[0;37m[V][component:200]: Component wifi took a long time for an operation (0.21 s).e[0m[6265028][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 2 - STA_START
14:00:57.527 -> 
14:00:57.527 -> e[0;37m[V][component:201]: Components should block for at most 20-30ms.e[0m
14:00:57.527 -> e[0;33m[W][api.connection:058]: Home Assistant 2023.2.3 (::FFFF:192.168.178.126): Network unavailable, disconnectinge[0m
14:00:57.527 -> e[0;37m[V][api:114]: Removing connection to Home Assistant 2023.2.3 (::FFFF:192.168.178.126)e[0m
14:00:59.916 -> [6267439][V][WiFiGeneric.cpp:360] _arduino_event_cb(): STA Disconnected: SSID: wifiNetwork, BSSID: 00:00:00:00:00:00, Reason: 201
14:00:59.916 -> [6267440][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 5 - STA_DISCONNECTED
14:00:59.916 -> [6267448][W][WiFiGeneric.cpp:950] _eventCallback(): Reason: 201 - NO_AP_FOUND
14:00:59.916 -> e[0;33m[W][wifi_esp32:491]: Event: Disconnected ssid='wifiNetwork' reason='Probe Request Unsuccessful'e[0m
14:00:59.963 -> e[0;33m[W][wifi:559]: WiFi network can not be found anymore.e[0m
14:00:59.963 -> e[0;33m[W][wifi:585]: Restarting WiFi adapter...e[0m
14:00:59.963 -> e[0;37m[V][wifi_esp32:041]: Disabling STA.e[0m
14:00:59.963 -> [6267487][V][WiFiGeneric.cpp:341] _arduino_event_cb(): STA Stopped
14:00:59.963 -> [6267490][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 3 - STA_STOP
14:00:59.963 -> e[0;37m[V][wifi_esp32:464]: Event: WiFi STA stope[0m
14:01:00.056 -> e[0;37m[V][component:200]: Component wifi took a long time for an operation (0.13 s).e[0m
14:01:00.056 -> e[0;37m[V][component:201]: Components should block for at most 20-30ms.e[0m
14:01:04.844 -> e[0;37m[V][sensor:076]: 'Workroom NSPanel target_temperature': Received new state 20.600000e[0m
14:01:04.844 -> e[0;36m[D][sensor:127]: 'Workroom NSPanel target_temperature': Sending state 20.60000  with 1 decimals of accuracye[0m
14:01:05.078 -> e[0;32m[I][wifi:257]: WiFi Connecting to 'wifiNetwork'...e[0m
14:01:05.078 -> e[0;37m[V][wifi:259]: Connection Params:e[0m
14:01:05.078 -> e[0;37m[V][wifi:260]:   SSID: 'wifiNetwork'e[0m
14:01:05.078 -> e[0;37m[V][wifi:265]:   BSSID: Not Sete[0m
14:01:05.078 -> e[0;37m[V][wifi:283]:   Password: e[5m'wifiPassword'e[6me[0m
14:01:05.078 -> e[0;37m[V][wifi:290]:   Channel: Not Sete[0m
14:01:05.078 -> e[0;37m[V][wifi:295]:   Manual IP: Static IP=192.168.178.186 Gateway=192.168.178.1 Subnet=255.255.255.0 DNS1=0.0.0.0 DNS2=0.0.0.0e[0m
14:01:05.126 -> e[0;37m[V][wifi:299]:   Hidden: NOe[0m
14:01:05.126 -> e[0;37m[V][wifi_esp32:039]: Enabling STA.e[0m
14:01:05.126 -> [6272648][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 0 - WIFI_READY
14:01:05.126 -> e[0;37m[V][wifi_esp32:444]: Event: WiFi readye[0m
14:01:05.126 -> [6272653][V][WiFiGeneric.cpp:338] _arduino_event_cb(): STA Started
14:01:05.126 -> [6272660][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 2 - STA_START
14:01:05.126 -> e[0;37m[V][wifi_esp32:459]: Event: WiFi STA starte[0m
14:01:07.525 -> [6275069][V][WiFiGeneric.cpp:360] _arduino_event_cb(): STA Disconnected: SSID: wifiNetwork, BSSID: 00:00:00:00:00:00, Reason: 201
14:01:07.559 -> [6275070][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 5 - STA_DISCONNECTED
14:01:07.559 -> [6275077][W][WiFiGeneric.cpp:950] _eventCallback(): Reason: 201 - NO_AP_FOUND
14:01:07.559 -> e[0;33m[W][wifi_esp32:491]: Event: Disconnected ssid='wifiNetwork' reason='Probe Request Unsuccessful'e[0m
14:01:07.559 -> e[0;33m[W][wifi:559]: WiFi network can not be found anymore.e[0m
14:01:07.606 -> e[0;33m[W][wifi:585]: Restarting WiFi adapter...e[0m
14:01:07.606 -> e[0;37m[V][wifi_esp32:041]: Disabling STA.e[0m
14:01:07.606 -> [6275117][V][WiFiGeneric.cpp:341] _arduino_event_cb(): STA Stopped
14:01:07.606 -> [6275119][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 3 - STA_STOP
14:01:07.606 -> e[0;37m[V][wifi_esp32:464]: Event: WiFi STA stope[0m
14:01:07.687 -> e[0;32m[I][wifi:257]: WiFi Connecting to 'wifiNetwork'...e[0m
14:01:07.687 -> e[0;37m[V][wifi:259]: Connection Params:e[0m
14:01:07.687 -> e[0;37m[V][wifi:260]:   SSID: 'wifiNetwork'e[0m
14:01:07.687 -> e[0;37m[V][wifi:265]:   BSSID: Not Sete[0m
14:01:07.720 -> e[0;37m[V][wifi:283]:   Password: e[5m'wifiPassword'e[6me[0m
14:01:07.720 -> e[0;37m[V][wifi:290]:   Channel: Not Sete[0m
14:01:07.720 -> e[0;37m[V][wifi:295]:   Manual IP: Static IP=192.168.178.186 Gateway=192.168.178.1 Subnet=255.255.255.0 DNS1=0.0.0.0 DNS2=0.0.0.0e[0m
14:01:07.720 -> e[0;37m[V][wifi:299]:   Hidden: NOe[0m
14:01:07.720 -> e[0;37m[V][wifi_esp32:039]: Enabling STA.e[0m
14:01:07.767 -> [6275275][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 0 - WIFI_READY
14:01:07.767 -> e[0;37m[V][wifi_esp32:444]: Event: WiFi readye[0m
14:01:07.767 -> [6275280][V][WiFiGeneric.cpp:338] _arduino_event_cb(): STA Started
14:01:07.767 -> [6275287][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 2 - STA_START
14:01:07.767 -> e[0;37m[V][wifi_esp32:459]: Event: WiFi STA starte[0m
14:01:10.160 -> [6277696][V][WiFiGeneric.cpp:360] _arduino_event_cb(): STA Disconnected: SSID: wifiNetwork, BSSID: 00:00:00:00:00:00, Reason: 201
14:01:10.160 -> [6277696][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 5 - STA_DISCONNECTED
14:01:10.206 -> [6277704][W][WiFiGeneric.cpp:950] _eventCallback(): Reason: 201 - NO_AP_FOUND
14:01:10.206 -> e[0;33m[W][wifi_esp32:491]: Event: Disconnected ssid='wifiNetwork' reason='Probe Request Unsuccessful'e[0m
14:01:10.206 -> e[0;33m[W][wifi:559]: WiFi network can not be found anymore.e[0m
14:01:10.206 -> e[0;33m[W][wifi:585]: Restarting WiFi adapter...e[0m
14:01:10.206 -> e[0;37m[V][wifi_esp32:041]: Disabling STA.e[0m
14:01:10.206 -> [6277743][V][WiFiGeneric.cpp:341] _arduino_event_cb(): STA Stopped
14:01:10.206 -> [6277746][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 3 - STA_STOP
14:01:10.253 -> e[0;37m[V][wifi_esp32:464]: Event: WiFi STA stope[0m
14:01:10.346 -> e[0;37m[V][component:200]: Component wifi took a long time for an operation (0.13 s).e[0m
14:01:10.346 -> e[0;37m[V][component:201]: Components should block for at most 20-30ms.e[0m
14:01:15.330 -> e[0;32m[I][wifi:257]: WiFi Connecting to 'wifiNetwork'...e[0m
14:01:15.330 -> e[0;37m[V][wifi:259]: Connection Params:e[0m
14:01:15.330 -> e[0;37m[V][wifi:260]:   SSID: 'wifiNetwork'e[0m
14:01:15.330 -> e[0;37m[V][wifi:265]:   BSSID: Not Sete[0m
14:01:15.330 -> e[0;37m[V][wifi:283]:   Password: e[5m'wifiPassword'e[6me[0m
14:01:15.330 -> e[0;37m[V][wifi:290]:   Channel: Not Sete[0m
14:01:15.330 -> e[0;37m[V][wifi:295]:   Manual IP: Static IP=192.168.178.186 Gateway=192.168.178.1 Subnet=255.255.255.0 DNS1=0.0.0.0 DNS2=0.0.0.0e[0m
14:01:15.377 -> e[0;37m[V][wifi:299]:   Hidden: NOe[0m
14:01:15.377 -> e[0;37m[V][wifi_esp32:039]: Enabling STA.e[0m
14:01:15.377 -> [6282904][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 0 - WIFI_READY
14:01:15.377 -> e[0;37m[V][wifi_esp32:444]: Event: WiFi readye[0m
14:01:15.377 -> [6282909][V][WiFiGeneric.cpp:338] _arduino_event_cb(): STA Started
14:01:15.377 -> [6282916][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 2 - STA_START
14:01:15.377 -> e[0;37m[V][wifi_esp32:459]: Event: WiFi STA starte[0m
14:01:17.796 -> [6285325][V][WiFiGeneric.cpp:360] _arduino_event_cb(): STA Disconnected: SSID: wifiNetwork, BSSID: 00:00:00:00:00:00, Reason: 201
14:01:17.796 -> [6285325][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 5 - STA_DISCONNECTED
14:01:17.796 -> [6285333][W][WiFiGeneric.cpp:950] _eventCallback(): Reason: 201 - NO_AP_FOUND
14:01:17.844 -> e[0;33m[W][wifi_esp32:491]: Event: Disconnected ssid='wifiNetwork' reason='Probe Request Unsuccessful'e[0m
14:01:17.844 -> e[0;33m[W][wifi:549]: Error while connecting to network.e[0m
14:01:17.844 -> e[0;33m[W][wifi:585]: Restarting WiFi adapter...e[0m
14:01:17.844 -> e[0;37m[V][wifi_esp32:041]: Disabling STA.e[0m
14:01:17.844 -> [6285369][V][WiFiGeneric.cpp:341] _arduino_event_cb(): STA Stopped
14:01:17.844 -> [6285371][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 3 - STA_STOP
14:01:17.844 -> e[0;37m[V][wifi_esp32:464]: Event: WiFi STA stope[0m
14:01:17.932 -> e[0;32m[I][wifi:257]: WiFi Connecting to 'wifiNetwork'...e[0m
14:01:17.932 -> e[0;37m[V][wifi:259]: Connection Params:e[0m
14:01:17.978 -> e[0;37m[V][wifi:260]:   SSID: 'wifiNetwork'e[0m
14:01:17.978 -> e[0;37m[V][wifi:265]:   BSSID: Not Sete[0m
14:01:17.978 -> e[0;37m[V][wifi:283]:   Password: e[5m'wifiPassword'e[6me[0m
14:01:17.978 -> e[0;37m[V][wifi:290]:   Channel: Not Sete[0m
14:01:17.978 -> e[0;37m[V][wifi:295]:   Manual IP: Static IP=192.168.178.186 Gateway=192.168.178.1 Subnet=255.255.255.0 DNS1=0.0.0.0 DNS2=0.0.0.0e[0m
14:01:17.978 -> e[0;37m[V][wifi:299]:   Hidden: NOe[0m
14:01:17.978 -> e[0;37m[V][wifi_esp32:039]: Enabling STA.e[0m
14:01:17.978 -> [6285527][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 0 - WIFI_READY
14:01:18.025 -> e[0;37m[V][wifi_esp32:444]: Event: WiFi readye[0m
14:01:18.025 -> [6285532][V][WiFiGeneric.cpp:338] _arduino_event_cb(): STA Started
14:01:18.025 -> [6285539][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 2 - STA_START
14:01:18.025 -> e[0;37m[V][wifi_esp32:459]: Event: WiFi STA starte[0m
14:01:20.397 -> [6287948][V][WiFiGeneric.cpp:360] _arduino_event_cb(): STA Disconnected: SSID: wifiNetwork, BSSID: 00:00:00:00:00:00, Reason: 201
14:01:20.431 -> [6287948][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 5 - STA_DISCONNECTED
14:01:20.431 -> [6287956][W][WiFiGeneric.cpp:950] _eventCallback(): Reason: 201 - NO_AP_FOUND
14:01:20.431 -> e[0;33m[W][wifi_esp32:491]: Event: Disconnected ssid='wifiNetwork' reason='Probe Request Unsuccessful'e[0m
14:01:20.431 -> e[0;33m[W][wifi:549]: Error while connecting to network.e[0m
14:01:20.479 -> e[0;33m[W][wifi:585]: Restarting WiFi adapter...e[0m
14:01:20.479 -> e[0;37m[V][wifi_esp32:041]: Disabling STA.e[0m
14:01:20.479 -> [6287992][V][WiFiGeneric.cpp:341] _arduino_event_cb(): STA Stopped
14:01:20.479 -> [6287995][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 3 - STA_STOP
14:01:20.479 -> e[0;37m[V][wifi_esp32:464]: Event: WiFi STA stope[0m
14:01:20.572 -> e[0;37m[V][component:200]: Component wifi took a long time for an operation (0.12 s).e[0m
14:01:20.572 -> e[0;37m[V][component:201]: Components should block for at most 20-30ms.e[0m
14:01:20.572 -> e[0;37m[V][text_sensor:016]: 'Workroom NSPanel thermostat_mode': Received new state heate[0m
14:01:20.619 -> e[0;36m[D][text_sensor:067]: 'Workroom NSPanel thermostat_mode': Sending state 'heat'e[0m
14:01:25.564 -> e[0;32m[I][wifi:257]: WiFi Connecting to 'wifiNetwork'...e[0m
14:01:25.564 -> e[0;37m[V][wifi:259]: Connection Params:e[0m
14:01:25.610 -> e[0;37m[V][wifi:260]:   SSID: 'wifiNetwork'e[0m
14:01:25.610 -> e[0;37m[V][wifi:265]:   BSSID: Not Sete[0m
14:01:25.610 -> e[0;37m[V][wifi:283]:   Password: e[5m'wifiPassword'e[6me[0m
14:01:25.610 -> e[0;37m[V][wifi:290]:   Channel: Not Sete[0m
14:01:25.610 -> e[0;37m[V][wifi:295]:   Manual IP: Static IP=192.168.178.186 Gateway=192.168.178.1 Subnet=255.255.255.0 DNS1=0.0.0.0 DNS2=0.0.0.0e[0m
14:01:25.610 -> e[0;37m[V][wifi:299]:   Hidden: NOe[0m
14:01:25.610 -> e[0;37m[V][wifi_esp32:039]: Enabling STA.e[0m
14:01:25.610 -> [6293158][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 0 - WIFI_READY
14:01:25.657 -> e[0;37m[V][wifi_esp32:444]: Event: WiFi readye[0m
14:01:25.657 -> [6293163][V][WiFiGeneric.cpp:338] _arduino_event_cb(): STA Started
14:01:25.657 -> [6293170][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 2 - STA_START
14:01:25.657 -> e[0;37m[V][wifi_esp32:459]: Event: WiFi STA starte[0m
14:01:25.892 -> e[0;37m[V][sensor:076]: 'Workroom NSPanel WiFi Signal': Received new state 0.000000e[0m
14:01:25.892 -> e[0;36m[D][sensor:127]: 'Workroom NSPanel WiFi Signal': Sending state 0.00000 dBm with 0 decimals of accuracye[0m
14:01:26.590 -> e[0;37m[V][adc:132]: 'ntc_source': Got voltage=1.7950Ve[0m
14:01:26.590 -> e[0;37m[V][sensor:076]: 'ntc_source': Received new state 1.795000e[0m
14:01:26.590 -> e[0;36m[D][sensor:127]: 'ntc_source': Sending state 1.79500 V with 2 decimals of accuracye[0m
14:01:26.590 -> e[0;36m[D][resistance:039]: 'resistance_sensor' - Resistance 13358.1Ωe[0m
14:01:26.590 -> e[0;37m[V][sensor:076]: 'resistance_sensor': Received new state 13358.138672e[0m
14:01:26.638 -> e[0;36m[D][sensor:127]: 'resistance_sensor': Sending state 13358.13867 Ω with 1 decimals of accuracye[0m
14:01:26.638 -> e[0;36m[D][ntc:026]: 'Workroom NSPanel Temperature' - Temperature: 18.6°Ce[0m
14:01:26.638 -> e[0;37m[V][sensor:076]: 'Workroom NSPanel Temperature': Received new state 18.623339e[0m
14:01:26.638 -> e[0;36m[D][sensor:127]: 'Workroom NSPanel Temperature': Sending state 18.62334 °C with 1 decimals of accuracye[0m
14:01:26.638 -> e[0;37m[V][component:200]: Component adc.sensor took a long time for an operation (0.06 s).e[0m
14:01:26.673 -> e[0;37m[V][component:201]: Components should block for at most 20-30ms.e[0m
14:01:28.035 -> [6295579][V][WiFiGeneric.cpp:360] _arduino_event_cb(): STA Disconnected: SSID: wifiNetwork, BSSID: 00:00:00:00:00:00, Reason: 201
14:01:28.082 -> [6295579][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 5 - STA_DISCONNECTED
14:01:28.082 -> [6295587][W][WiFiGeneric.cpp:950] _eventCallback(): Reason: 201 - NO_AP_FOUND
14:01:28.082 -> e[0;33m[W][wifi_esp32:491]: Event: Disconnected ssid='wifiNetwork' reason='Probe Request Unsuccessful'e[0m
14:01:28.082 -> e[0;37m[V][wifi:295]:   Manual IP: Static IP=192.168.178.186 Gateway=192.168.178.1 Subnet=255.255.255.0 DNS1=0.0.0.0 DNS2=0.0.0.0e[0m
14:01:28.082 -> e[0;37m[V][wifi:299]:   Hidden: NOe[0m
14:01:28.129 -> e[0;33m[W][wifi:549]: Error while connecting to network.e[0m
14:01:28.129 -> e[0;33m[W][wifi:585]: Restarting WiFi adapter...e[0m
14:01:28.129 -> e[0;37m[V][wifi_esp32:041]: Disabling STA.e[0m
14:01:28.129 -> [6295658][V][WiFiGeneric.cpp:341] _arduino_event_cb(): STA Stopped
14:01:28.129 -> [6295659][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 3 - STA_STOP
14:01:28.129 -> e[0;37m[V][wifi_esp32:464]: Event: WiFi STA stope[0m
14:01:28.223 -> e[0;37m[V][component:200]: Component wifi took a long time for an operation (0.12 s).e[0m
14:01:28.223 -> e[0;37m[V][component:201]: Components should block for at most 20-30ms.e[0m
14:01:33.224 -> e[0;32m[I][wifi:257]: WiFi Connecting to 'wifiNetwork'...e[0m
14:01:33.271 -> e[0;37m[V][wifi:259]: Connection Params:e[0m
14:01:33.271 -> e[0;37m[V][wifi:260]:   SSID: 'wifiNetwork'e[0m
14:01:33.271 -> e[0;37m[V][wifi:265]:   BSSID: Not Sete[0m
14:01:33.271 -> e[0;37m[V][wifi:283]:   Password: e[5m'wifiPassword'e[6me[0m
14:01:33.271 -> e[0;37m[V][wifi:290]:   Channel: Not Sete[0m
14:01:33.271 -> e[0;37m[V][wifi:295]:   Manual IP: Static IP=192.168.178.186 Gateway=192.168.178.1 Subnet=255.255.255.0 DNS1=0.0.0.0 DNS2=0.0.0.0e[0m
14:01:33.271 -> e[0;37m[V][wifi:299]:   Hidden: NOe[0m
14:01:33.271 -> e[0;37m[V][wifi_esp32:039]: Enabling STA.e[0m
14:01:33.271 -> [6300823][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 0 - WIFI_READY
14:01:33.318 -> e[0;37m[V][wifi_esp32:444]: Event: WiFi readye[0m
14:01:33.318 -> [6300828][V][WiFiGeneric.cpp:338] _arduino_event_cb(): STA Started
14:01:33.318 -> [6300835][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 2 - STA_START
14:01:33.318 -> e[0;37m[V][wifi_esp32:459]: Event: WiFi STA starte[0m
14:01:35.705 -> [6303243][V][WiFiGeneric.cpp:360] _arduino_event_cb(): STA Disconnected: SSID: wifiNetwork, BSSID: 00:00:00:00:00:00, Reason: 201
14:01:35.705 -> [6303244][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 5 - STA_DISCONNECTED
14:01:35.740 -> [6303252][W][WiFiGeneric.cpp:950] _eventCallback(): Reason: 201 - NO_AP_FOUND
14:01:35.740 -> e[0;33m[W][wifi_esp32:491]: Event: Disconnected ssid='wifiNetwork' reason='Probe Request Unsuccessful'e[0m
14:01:35.740 -> e[0;33m[W][wifi:559]: WiFi network can not be found anymore.e[0m
14:01:35.740 -> e[0;33m[W][wifi:585]: Restarting WiFi adapter...e[0m
14:01:35.787 -> e[0;37m[V][wifi_esp32:041]: Disabling STA.e[0m
14:01:35.787 -> [6303291][V][WiFiGeneric.cpp:341] _arduino_event_cb(): STA Stopped
14:01:35.787 -> [6303294][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 3 - STA_STOP
14:01:35.787 -> e[0;37m[V][wifi_esp32:464]: Event: WiFi STA stope[0m
14:01:35.868 -> e[0;32m[I][wifi:257]: WiFi Connecting to 'wifiNetwork'...e[0m
14:01:35.868 -> e[0;37m[V][wifi:259]: Connection Params:e[0m
14:01:35.868 -> e[0;37m[V][wifi:260]:   SSID: 'wifiNetwork'e[0m
14:01:35.868 -> e[0;37m[V][wifi:265]:   BSSID: Not Sete[0m
14:01:35.868 -> e[0;37m[V][wifi:283]:   Password: e[5m'wifiPassword'e[6me[0m
14:01:35.915 -> e[0;37m[V][wifi:290]:   Channel: Not Sete[0m
14:01:35.915 -> e[0;37m[V][wifi:295]:   Manual IP: Static IP=192.168.178.186 Gateway=192.168.178.1 Subnet=255.255.255.0 DNS1=0.0.0.0 DNS2=0.0.0.0e[0m
14:01:35.915 -> e[0;37m[V][wifi:299]:   Hidden: NOe[0m
14:01:35.915 -> e[0;37m[V][wifi_esp32:039]: Enabling STA.e[0m
14:01:35.915 -> [6303450][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 0 - WIFI_READY
14:01:35.915 -> e[0;37m[V][wifi_esp32:444]: Event: WiFi readye[0m
14:01:35.915 -> [6303455][V][WiFiGeneric.cpp:338] _arduino_event_cb(): STA Started
14:01:35.915 -> [6303462][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 2 - STA_START
14:01:35.962 -> e[0;37m[V][wifi_esp32:459]: Event: WiFi STA starte[0m
14:01:38.323 -> [6305870][V][WiFiGeneric.cpp:360] _arduino_event_cb(): STA Disconnected: SSID: wifiNetwork, BSSID: 00:00:00:00:00:00, Reason: 201
14:01:38.369 -> [6305871][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 5 - STA_DISCONNECTED
14:01:38.369 -> [6305879][W][WiFiGeneric.cpp:950] _eventCallback(): Reason: 201 - NO_AP_FOUND
14:01:38.369 -> e[0;33m[W][wifi_esp32:491]: Event: Disconnected ssid='wifiNetwork' reason='Probe Request Unsuccessful'e[0m
14:01:38.369 -> e[0;33m[W][wifi:559]: WiFi network can not be found anymore.e[0m
14:01:38.369 -> e[0;33m[W][wifi:585]: Restarting WiFi adapter...e[0m
14:01:38.369 -> e[0;37m[V][wifi_esp32:041]: Disabling STA.e[0m
14:01:38.416 -> [6305919][V][WiFiGeneric.cpp:341] _arduino_event_cb(): STA Stopped
14:01:38.416 -> [6305921][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 3 - STA_STOP
14:01:38.416 -> e[0;37m[V][wifi_esp32:464]: Event: WiFi STA stope[0m
14:01:38.510 -> e[0;37m[V][component:200]: Component wifi took a long time for an operation (0.13 s).e[0m
14:01:38.510 -> e[0;37m[V][component:201]: Components should block for at most 20-30ms.e[0m
14:01:43.488 -> e[0;32m[I][wifi:257]: WiFi Connecting to 'wifiNetwork'...e[0m
14:01:43.488 -> e[0;37m[V][wifi:259]: Connection Params:e[0m
14:01:43.535 -> e[0;37m[V][wifi:260]:   SSID: 'wifiNetwork'e[0m
14:01:43.535 -> e[0;37m[V][wifi:265]:   BSSID: Not Sete[0m
14:01:43.535 -> e[0;37m[V][wifi:283]:   Password: e[5m'wifiPassword'e[6me[0m
14:01:43.535 -> e[0;37m[V][wifi:290]:   Channel: Not Sete[0m
14:01:43.535 -> e[0;37m[V][wifi:295]:   Manual IP: Static IP=192.168.178.186 Gateway=192.168.178.1 Subnet=255.255.255.0 DNS1=0.0.0.0 DNS2=0.0.0.0e[0m
14:01:43.535 -> e[0;37m[V][wifi:299]:   Hidden: NOe[0m
14:01:43.535 -> e[0;37m[V][wifi_esp32:039]: Enabling STA.e[0m
14:01:43.535 -> [6311084][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 0 - WIFI_READY
14:01:43.582 -> e[0;37m[V][wifi_esp32:444]: Event: WiFi readye[0m
14:01:43.582 -> [6311089][V][WiFiGeneric.cpp:338] _arduino_event_cb(): STA Started
14:01:43.582 -> [6311096][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 2 - STA_START
14:01:43.582 -> e[0;37m[V][wifi_esp32:459]: Event: WiFi STA starte[0m
14:01:45.954 -> [6313505][V][WiFiGeneric.cpp:360] _arduino_event_cb(): STA Disconnected: SSID: wifiNetwork, BSSID: 00:00:00:00:00:00, Reason: 201
14:01:46.000 -> [6313506][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 5 - STA_DISCONNECTED
14:01:46.000 -> [6313513][W][WiFiGeneric.cpp:950] _eventCallback(): Reason: 201 - NO_AP_FOUND
14:01:46.000 -> e[0;33m[W][wifi_esp32:491]: Event: Disconnected ssid='wifiNetwork' reason='Probe Request Unsuccessful'e[0m
14:01:46.000 -> e[0;33m[W][wifi:559]: WiFi network can not be found anymore.e[0m
14:01:46.000 -> e[0;33m[W][wifi:585]: Restarting WiFi adapter...e[0m
14:01:46.047 -> e[0;37m[V][wifi_esp32:041]: Disabling STA.e[0m
14:01:46.047 -> [6313553][V][WiFiGeneric.cpp:341] _arduino_event_cb(): STA Stopped
14:01:46.047 -> [6313555][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 3 - STA_STOP
14:01:46.047 -> e[0;37m[V][wifi_esp32:464]: Event: WiFi STA stope[0m
14:01:46.141 -> e[0;32m[I][wifi:257]: WiFi Connecting to 'wifiNetwork'...e[0m
14:01:46.141 -> e[0;37m[V][wifi:259]: Connection Params:e[0m
14:01:46.141 -> e[0;37m[V][wifi:260]:   SSID: 'wifiNetwork'e[0m
14:01:46.141 -> e[0;37m[V][wifi:265]:   BSSID: Not Sete[0m
14:01:46.141 -> e[0;37m[V][wifi:283]:   Password: e[5m'wifiPassword'e[6me[0m
14:01:46.141 -> e[0;37m[V][wifi:290]:   Channel: Not Sete[0m
14:01:46.141 -> e[0;37m[V][wifi:295]:   Manual IP: Static IP=192.168.178.186 Gateway=192.168.178.1 Subnet=255.255.255.0 DNS1=0.0.0.0 DNS2=0.0.0.0e[0m
14:01:46.188 -> e[0;37m[V][wifi:299]:   Hidden: NOe[0m
14:01:46.188 -> e[0;37m[V][wifi_esp32:039]: Enabling STA.e[0m
14:01:46.188 -> [6313711][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 0 - WIFI_READY
14:01:46.188 -> e[0;37m[V][wifi_esp32:444]: Event: WiFi readye[0m
14:01:46.188 -> [6313716][V][WiFiGeneric.cpp:338] _arduino_event_cb(): STA Started
14:01:46.188 -> [6313723][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 2 - STA_START
14:01:46.188 -> e[0;37m[V][wifi_esp32:459]: Event: WiFi STA starte[0m
14:01:48.595 -> [6316131][V][WiFiGeneric.cpp:360] _arduino_event_cb(): STA Disconnected: SSID: wifiNetwork, BSSID: 00:00:00:00:00:00, Reason: 201
14:01:48.595 -> [6316132][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 5 - STA_DISCONNECTED
14:01:48.641 -> [6316140][W][WiFiGeneric.cpp:950] _eventCallback(): Reason: 201 - NO_AP_FOUND
14:01:48.641 -> e[0;33m[W][wifi_esp32:491]: Event: Disconnected ssid='wifiNetwork' reason='Probe Request Unsuccessful'e[0m
14:01:48.641 -> e[0;33m[W][wifi:549]: Error while connecting to network.e[0m
14:01:48.641 -> e[0;33m[W][wifi:585]: Restarting WiFi adapter...e[0m
14:01:48.641 -> e[0;37m[V][wifi_esp32:041]: Disabling STA.e[0m
14:01:48.641 -> [6316175][V][WiFiGeneric.cpp:341] _arduino_event_cb(): STA Stopped
14:01:48.641 -> [6316178][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 3 - STA_STOP
14:01:48.688 -> e[0;37m[V][wifi_esp32:464]: Event: WiFi STA stope[0m
14:01:48.735 -> e[0;37m[V][component:200]: Component wifi took a long time for an operation (0.12 s).e[0m
14:01:48.782 -> e[0;37m[V][component:201]: Components should block for at most 20-30ms.e[0m
14:01:53.770 -> e[0;32m[I][wifi:257]: WiFi Connecting to 'wifiNetwork'...e[0m
14:01:53.770 -> e[0;37m[V][wifi:259]: Connection Params:e[0m
14:01:53.770 -> e[0;37m[V][wifi:260]:   SSID: 'wifiNetwork'e[0m
14:01:53.770 -> e[0;37m[V][wifi:265]:   BSSID: Not Sete[0m
14:01:53.770 -> e[0;37m[V][wifi:283]:   Password: e[5m'wifiPassword'e[6me[0m
14:01:53.770 -> e[0;37m[V][wifi:290]:   Channel: Not Sete[0m
14:01:53.770 -> e[0;37m[V][wifi:295]:   Manual IP: Static IP=192.168.178.186 Gateway=192.168.178.1 Subnet=255.255.255.0 DNS1=0.0.0.0 DNS2=0.0.0.0e[0m
14:01:53.817 -> e[0;37m[V][wifi:299]:   Hidden: NOe[0m
14:01:53.817 -> e[0;37m[V][wifi_esp32:039]: Enabling STA.e[0m
14:01:53.817 -> [6321340][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 0 - WIFI_READY
14:01:53.817 -> e[0;37m[V][wifi_esp32:444]: Event: WiFi readye[0m
14:01:53.817 -> [6321345][V][WiFiGeneric.cpp:338] _arduino_event_cb(): STA Started
14:01:53.817 -> [6321352][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 2 - STA_START
14:01:53.817 -> e[0;37m[V][wifi_esp32:459]: Event: WiFi STA starte[0m
14:01:56.216 -> [6323760][V][WiFiGeneric.cpp:360] _arduino_event_cb(): STA Disconnected: SSID: wifiNetwork, BSSID: 00:00:00:00:00:00, Reason: 201
14:01:56.263 -> [6323761][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 5 - STA_DISCONNECTED
14:01:56.263 -> [6323769][W][WiFiGeneric.cpp:950] _eventCallback(): Reason: 201 - NO_AP_FOUND
14:01:56.263 -> e[0;33m[W][wifi_esp32:491]: Event: Disconnected ssid='wifiNetwork' reason='Probe Request Unsuccessful'e[0m
14:01:56.263 -> e[0;33m[W][wifi:559]: WiFi network can not be found anymore.e[0m
14:01:56.263 -> e[0;33m[W][wifi:585]: Restarting WiFi adapter...e[0m
14:01:56.263 -> e[0;37m[V][wifi_esp32:041]: Disabling STA.e[0m
14:01:56.263 -> [6323808][V][WiFiGeneric.cpp:341] _arduino_event_cb(): STA Stopped
14:01:56.310 -> [6323811][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 3 - STA_STOP
14:01:56.310 -> e[0;37m[V][wifi_esp32:464]: Event: WiFi STA stope[0m
14:01:56.404 -> e[0;32m[I][wifi:257]: WiFi Connecting to 'wifiNetwork'...e[0m
14:01:56.404 -> e[0;37m[V][wifi:259]: Connection Params:e[0m
14:01:56.404 -> e[0;37m[V][wifi:260]:   SSID: 'wifiNetwork'e[0m
14:01:56.404 -> e[0;37m[V][wifi:265]:   BSSID: Not Sete[0m
14:01:56.404 -> e[0;37m[V][wifi:283]:   Password: e[5m'wifiPassword'e[6me[0m
14:01:56.404 -> e[0;37m[V][wifi:290]:   Channel: Not Sete[0m
14:01:56.404 -> e[0;37m[V][wifi:295]:   Manual IP: Static IP=192.168.178.186 Gateway=192.168.178.1 Subnet=255.255.255.0 DNS1=0.0.0.0 DNS2=0.0.0.0e[0m
14:01:56.451 -> e[0;37m[V][wifi:299]:   Hidden: NOe[0m
14:01:56.451 -> e[0;37m[V][wifi_esp32:039]: Enabling STA.e[0m
14:01:56.451 -> [6323967][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 0 - WIFI_READY
14:01:56.451 -> e[0;37m[V][wifi_esp32:444]: Event: WiFi readye[0m
14:01:56.451 -> [6323973][V][WiFiGeneric.cpp:338] _arduino_event_cb(): STA Started
14:01:56.451 -> [6323979][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 2 - STA_START
14:01:56.451 -> e[0;37m[V][wifi_esp32:459]: Event: WiFi STA starte[0m
14:01:58.847 -> [6326388][V][WiFiGeneric.cpp:360] _arduino_event_cb(): STA Disconnected: SSID: wifiNetwork, BSSID: 00:00:00:00:00:00, Reason: 201
14:01:58.847 -> [6326389][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 5 - STA_DISCONNECTED
14:01:58.893 -> [6326396][W][WiFiGeneric.cpp:950] _eventCallback(): Reason: 201 - NO_AP_FOUND
14:01:58.893 -> e[0;33m[W][wifi_esp32:491]: Event: Disconnected ssid='wifiNetwork' reason='Probe Request Unsuccessful'e[0m
14:01:58.893 -> e[0;33m[W][wifi:559]: WiFi network can not be found anymore.e[0m
14:01:58.893 -> e[0;33m[W][wifi:585]: Restarting WiFi adapter...e[0m

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