ESPHome ESP32 device with uptime text_sensor quickly disconnects and reconnects

I’ve been using ESPHome for a while and I started to use it with ESP32 devices and I’ve used my boilerplate yaml (including a uptime sensor and a wifi strength sensor) but I found that when hooking Home Assistant up to it, it would disconnect and reconnect.

[11:39:41][D][api.connection:604]: Client 'Home Assistant 0.116.4 (192.168.1.5)' connected successfully!
[11:39:41][D][api:067]: Disconnecting Home Assistant 0.116.4 (192.168.1.5)
[11:39:41][D][api.connection:604]: Client 'Home Assistant 0.116.4 (192.168.1.5)' connected successfully!
[11:39:42][D][api:067]: Disconnecting Home Assistant 0.116.4 (192.168.1.5)
[11:39:42][D][api.connection:604]: Client 'Home Assistant 0.116.4 (192.168.1.5)' connected successfully!
[11:39:42][D][api:067]: Disconnecting Home Assistant 0.116.4 (192.168.1.5)
[11:39:43][D][api.connection:604]: Client 'Home Assistant 0.116.4 (192.168.1.5)' connected successfully!
[11:39:43][D][api:067]: Disconnecting Home Assistant 0.116.4 (192.168.1.5)
[11:39:43][D][api.connection:604]: Client 'Home Assistant 0.116.4 (192.168.1.5)' connected successfully!
[11:39:44][D][api:067]: Disconnecting Home Assistant 0.116.4 (192.168.1.5)
[11:39:44][D][api.connection:604]: Client 'Home Assistant 0.116.4 (192.168.1.5)' connected successfully!
[11:39:44][D][api:067]: Disconnecting Home Assistant 0.116.4 (192.168.1.5)
[11:39:44][D][api.connection:604]: Client 'Home Assistant 0.116.4 (192.168.1.5)' connected successfully!
[11:39:45][D][api:067]: Disconnecting Home Assistant 0.116.4 (192.168.1.5)
[11:39:45][D][api.connection:604]: Client 'Home Assistant 0.116.4 (192.168.1.5)' connected successfully!
[11:39:45][D][api:067]: Disconnecting Home Assistant 0.116.4 (192.168.1.5)

By trial and error I found that the uptime text_sensor was to blame. If I commented it out, the behavior stopped and everything worked great. Maybe this is a bug in the ESP32/ESPHome implementation?

Here’s the uptime sensor details:

sensor:
  - platform: uptime
    name: ${friendly_name} Uptime Raw
    id: ${node_name}_uptime_raw
    internal: true
    update_interval: 30s
text_sensor:
  - platform: template
    name: ${friendly_name} Uptime
    lambda: |-
      uint32_t dur = id(${node_name}_uptime_raw).state;
      int dys = 0;
      int hrs = 0;
      int mnts = 0;
      if (dur > 86399) {
        dys = trunc(dur / 86400);
        dur = dur - (dys * 86400);
      }
      if (dur > 3599) {
        hrs = trunc(dur / 3600);
        dur = dur - (hrs * 3600);
      }
      if (dur > 59) {
        mnts = trunc(dur / 60);
        dur = dur - (mnts * 60);
      }
      char buffer[17];
      sprintf(buffer, "%ud %02uh %02um %02us", dys, hrs, mnts, dur);
      return {buffer};
    icon: mdi:clock-start
    update_interval: 600s

Anyway, it was driving me nuts until I figured it out. I thought I’d post just in case someone else experiences the same thing…

1 Like

I have a NodeMcu ESP8266 (CP2102) and a PN532
I use these to read nfc tags and i can say that i also have this problem
Here’s my yaml:

esphome:
  name: nfcreader
  platform: ESP8266
  board: nodemcuv2

# Insert your SSID and Your PWD after inital setup
wifi:
  networks:
    - ssid: !SECRET ssid
      password: !SECRET SSIDPASSWORD
  power_save_mode: none
      
api:

#ota:

# Enable SPI interface
spi:
  clk_pin: D0
  miso_pin: D1
  mosi_pin: D2

# Configure the PN532 module
pn532:
  cs_pin: D3
  update_interval: 2s

binary_sensor:
  - platform: pn532
    uid: #SOMEUID
    name: "Chip0"
  - platform: pn532
    uid: #SOMEUID
    name: "Chip1"
  - platform: pn532
    uid: #SOMEUID
    name: "Card0"

As you can see nothing special , but still my esphome disconnects randomly and a lot to be fair…!
[api:067]: Disconnecting Home Assistant 0.116.4

Signed up to add a comment here - I have the same issue with a PN532 - I do recall this started happening with an ESPHome update, so I downgraded, but one of the esphome updates caused an auto-update to happen, and now I’m in a broken state again (with a newer ESPHome)

I have fixed this problem. I think it was caused by the lamba and I’m thinking it was the line uint32_t dur = id(${node_name}_uptime_raw).state; which on ESP8266 results in a very large number when the ${node_name}_uptime_raw value is not yet defined. I’m guessing it causes a crash on the ESP32. As seen in this post, there is another example of this human readable uptime clock that does a better job of handling this case and doesn’t cause the problem on ESP32s.

@SpikeyGG - would you mind sharing your yaml, and the connections between the PN532 and ESP8266? I suspect, i may be messing something else up.

Sorry @vk-hass, I do not have a PN532 device. All my ESP8266 devices are hooked up to Arduino compatible sensors. I did google it to learn more… Sounds like a way to communicate with NFC and other wireless devices. Very interesting, best of luck!

Yeah, it’s basically a NFC reader hooked up to wifi. Tap your keychain to make it to cool things in your house :slight_smile: