esphome / issues

Issue Tracker for ESPHome
https://esphome.io/
285 stars 34 forks source link

ESPHome device coming out of sleep mode exceedingly long connect time with good signal and does not update HomeAssistant sensors #5756

Open JustEnoughDucks opened 2 weeks ago

JustEnoughDucks commented 2 weeks ago

The problem

I am making a dumb doorbell. The only requirements is that it

The problem is that upon installing the config to the ESP32-C3, the device will consistently work until it goes to sleep for more than 10 minutes, then it will be extremely inconsistent and never work completely correctly again.

The behavior after the 10 minute period is as follows:

WARNING Can't connect to ESPHome API for dumb-doorbell @ 192.168.1.124: Timeout while connecting to [AddrInfo(family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_STREAM: 1>, proto=6, sockaddr=IPv4Sockaddr(address='192.168.1.124', port=6053))] (TimeoutAPIError)

*** I think this is an important because it indicates that it is a problem on the ESPhome end and not the device config. If ESPHome listens for a connection (or whatever it is doing when the log screen is up) then it connects right away. Then after the error message, it still connects, but ESPHome is the one that doesn't seem to relay the sensor values to Home Assistant

Maybe it would work better to simply broadcast an MQTT message instead of trying to couple with the API if the API doesn't seem to like handling very infrequent connections? It shouldn't be an networking issue because my other ESPHome devices that don't drop off the network have absolutely no problem sending simple sensor values and I have a DSMR reader that hands over values every 10 seconds.

Which version of ESPHome has the issue?

2024.4.2

What type of installation are you using?

Docker

Which version of Home Assistant has the issue?

<=2024.5.0

What platform are you using?

ESP32

Board

SEEED Xiao ESP32-C3

Component causing the issue

deep sleep or esphome api itself

Example YAML snippet

esphome:
  name: dumb-doorbell
  friendly_name: Dumb Doorbell
  # on_boot documentation and priorities: https://esphome.io/components/esphome.html#on-boot
  on_boot:
    - priority: -100
      then: # At this priority, pretty much everything should already be initialized.
        - light.turn_on:
            id: doorbell_light
            brightness: 100%
            transition_length: 100ms
        - deep_sleep.prevent: sleep_routine
        - wait_until: api.connected

        - logger.log: 
            format: "Starting sensor updates..."
            level: INFO
        - light.turn_off:
            id: doorbell_light
            transition_length: 2s
        - output.turn_on: meas_gnd
        - repeat:
            count: 1
            then:
              - binary_sensor.template.publish:
                  id: doorbell_pressed
                  state: ON
              #Sensor updates are turned off and manually requested on boot. 
              #Then the ESP goes back to sleep when they're done (unless told to stay awake)

              # Request sensor updates
              - component.update: LIION
              - delay: 100ms
              - component.update: bat_v
              - component.update: bat_percent
        - output.turn_off: meas_gnd
        - deep_sleep.allow: sleep_routine
        - delay: 2s
        - binary_sensor.template.publish:
            id: doorbell_pressed
            state: OFF
        - if:
            condition:
              - binary_sensor.is_on: battery_updated
            then:
              - logger.log: 
                  level: INFO
                  format: "Data from battery received"

            else:
              - logger.log: 
                  format: "Battery update missing"
                  level: ERROR

        # Reset data update sensors states
        - binary_sensor.template.publish:
            id: battery_updated
            state: OFF
        - deep_sleep.enter: sleep_routine

esp32:
  board: esp32-c3-devkitm-1
  framework:
    type: arduino

# Enable logging
logger:
    level: INFO

# Enable Home Assistant API
api:
  encryption:
    key: <REDACTED>

ota:
  password: "espUpdate32"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  manual_ip:
    static_ip: 192.168.1.124
    gateway: 192.168.0.1
    subnet: 255.255.255.0
  domain: ".web"
  fast_connect: true
  power_save_mode: none

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Dumb-Doorbell Fallback Hotspot"
    password: "eiURjgTxvkLk"

captive_portal:

# Sensing routine Setup
binary_sensor:
  - platform: template
    name: "Doorbell Pressed"
    id: doorbell_pressed
  - platform: template
    name: "Battery Read"
    id: battery_updated
    internal: true

# Battery read ADC
sensor:
  - platform: adc
    pin: GPIO4
    name: "Doorbell A2 Voltage"
    id: LIION
    update_interval: never
    attenuation: 2.5db
    internal: true
  - platform: template
    name: "Doorbell Battery Voltage"
    id: bat_v
    unit_of_measurement: 'V'
    update_interval: never
    accuracy_decimals: 2
    icon: "mdi:battery"
    lambda: |-
      return (id(LIION).state * 4);
  - platform: template
    name: "Doorbell Battery Percentage"
    id: bat_percent
    unit_of_measurement: '%'
    update_interval: never
    accuracy_decimals: 0
    device_class: battery
    lambda: |-
      if(id(bat_v).state < 1)
      {return 0;}
      return ((id(bat_v).state-3) /1.2 * 100.00);
    on_value:
      then:
        - binary_sensor.template.publish:
            id: battery_updated
            state: ON

output:
  - platform: ledc
    pin: GPIO7
    id: led_out
    frequency: "1000Hz"
# Measurement GND active low to save leakage power
  - platform: gpio
    pin: GPIO5
    id: meas_gnd
    inverted: True
# LED setup
light:
  - platform: monochromatic
    output: led_out
    name: ${friendly_name} LED
    id: doorbell_light
    internal: true
    default_transition_length: 1s

# Deep sleep configuration
deep_sleep:
  #run_duration: 4s
  #sleep_duration: 3s
  wakeup_pin: 
    number: GPIO2
    mode: 
      input: True
      open_drain: False
      pulldown: True
    inverted: False
  id: sleep_routine

Anything in the logs that might be useful for us?

: Running through setup()...<0x1b>[0m

<0x1b>[0;32m[I][wifi:303]: WiFi Connecting to <REDACTED>...<0x1b>[0m

<0x1b>[0;33m[W][component:157]: Component wifi set Warning flag: unspecified<0x1b>[0m
ESP-ROM:esp32c3-api1-20210207
Build:Feb  7 2021
rst:0x15 (USB_UART_CHIP_RESET),boot:0x8 (SPI_FAST_FLASH_BOOT)
Saved PC:0x4038b254
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fcd6100,len:0x438
load:0x403ce000,len:0x918
load:0x403d0000,len:0x24e4
entry 0x403ce000
<0x1b>[0;32m[I][logger:158]: Log initialized<0x1b>[0m
<0x1b>[0;32m[I][app:029]: Running through setup()...<0x1b>[0m
<0x1b>[0;32m[I][wifi:303]: WiFi Connecting to <REDACTED>...<0x1b>[0m
<0x1b>[0;33m[W][component:157]: Component wifi set Warning flag: unspecified<0x1b>[0m
**1s delay**
<0x1b>[0;32m[I][wifi:597]: WiFi Connected!<0x1b>[0m
<0x1b>[0;32m[I][app:062]: setup() finished successfully!<0x1b>[0m
<0x1b>[0;33m[W][component:170]: Component wifi cleared Warning flag<0x1b>[0m
<0x1b>[0;33m[W][component:157]: Component api set Warning flag: unspecified<0x1b>[0m
<0x1b>[0;32m[I][app:100]: ESPHome version 2024.4.2 compiled on May  2 2024, 14:12:20<0x1b>[0m
**59 second delay**
<0x1b>[0;33m[W][component:170]: Component api cleared Warning flag<0x1b>[0m
<0x1b>[0;32m[I][main:259]: Starting sensor updates...<0x1b>[0m
<0x1b>[0;32m[I][main:290]: Data from battery received<0x1b>[0m
<0x1b>[0;32m[I][deep_sleep:117]: Beginning Deep Sleep<0x1b>[0m

Additional information

No response

ssieb commented 2 weeks ago

The device does not do the connecting. HA connects to the device. If it doesn't know that the device is sleeping, it will keep attempting to connect with increasing back-off. The dashboard logs use the api as well, so the device doesn't know the difference between that and HA connecting, which is why it immediately thinks it has sent the data when you connected for logs. Is MDNS working? Did you add the device before or after you included the deep sleep option? There are several things you could improve with the config and you didn't include enough logs. Come ask on discord.