[esphome] Association refused temporarily error when trying to connect to wifi

I am getting the following error in the logs when ESPHome tries to connect to wifi. The problem started appearing after I switched the network my ESP32-dev is connected to. When the device tries to connect for the first time it throws the ‘Association refused temporarily’ error. It then waits for around 26 seconds before trying to connect to wifi again. The problem seems to be the wifi adapter on the ESP. Since I use the device with deep sleep to save power, I cannot afford to have to wait for 26 seconds just to connect to wifi.

Here is a piece of the log that shows the error:

[I][logger:258]: Log initialized
[C][ota:469]: There have been 0 suspected unsuccessful boot attempts.
[D][esp32.preferences:113]: Saving 1 preferences to flash...
[D][esp32.preferences:142]: Saving 1 preferences to flash: 0 cached, 1 written, 0 failed
[I][app:029]: Running through setup()...
[I][i2c.arduino:175]: Performing I2C bus recovery
[C][dht:011]: Setting up DHT...
[C][bmp280.sensor:060]: Setting up BMP280...
[C][wifi:037]: Setting up WiFi...
[C][wifi:038]:   Local MAC: C8:F0:9E:F1:C8:84
[I][wifi:257]: WiFi Connecting to 'T-2_E799B9'...
[D][esp-idf:000]: E (6963) wifi:
[D][esp-idf:000]: Association refused temporarily, comeback time 268435 mSec
[D][esp-idf:000]: 

[W][wifi:543]: Timeout while connecting to WiFi.
[I][wifi:257]: WiFi Connecting to 'T-2_E799B9'...
[W][wifi_esp32:494]: Event: Disconnected ssid='T-2_E799B9' bssid=[redacted] reason='Association Failed'
[I][wifi:257]: WiFi Connecting to 'T-2_E799B9'...
[W][wifi:549]: Error while connecting to network.
[W][wifi:585]: Restarting WiFi adapter...
[I][wifi:257]: WiFi Connecting to 'T-2_E799B9'...
[I][wifi:518]: WiFi Connected!

And this is the config I use. I removed some unnecessary code I use otherwise, to make it more clear, but the error is present no matter the code.

substitutions:
  devicename: esp32-inside
  max_run_duration: 60s
  sleep_duration: 60s

esp32:
  board: esp32dev
  framework:
    type: arduino

esphome:
  name: $devicename

# Enable logging
logger:
  level: DEBUG

# Enable Home Assistant API
api:

ota:


wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  fast_connect: true
  manual_ip:
    static_ip: 192.168.64.51
    gateway: 192.168.64.1
    subnet: 255.255.255.0

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esphome-Web-F1C884"
    password: "SioVN9kp2gvp"

captive_portal:
    
i2c:
  sda: 21
  scl: 22
  scan: true
  id: bus_a

deep_sleep:
  id: deep_sleep_1
  run_duration: $max_run_duration
  sleep_duration: $sleep_duration

sensor:
# DHT11 - temperature, humidity
  - platform: dht
    model: DHT11
    pin: 15
    id: dht_inside
    update_interval: never
    temperature:
      name: "Inside temperature DHT"
    humidity:
      name: "Inside humidity DHT"
      
  # BMP280 - temperature, pressure
  - platform: bmp280
    address: 0x76
    id: bmp_inside
    update_interval: never
    temperature:
      name: "Inside temperature BMP"
      oversampling: 16x
      filters:
        - offset: -1.5
      
    pressure:
      name: "Inside pressure BMP"

Also what I find interesting is that once wifi is connected I am getting the following output in the logs:

[D][api:102]: Accepted ::FFFF:192.168.64.50
[D][api:102]: Accepted ::FFFF:192.168.64.50
[D][api:102]: Accepted ::FFFF:192.168.64.50
[D][api:102]: Accepted ::FFFF:192.168.64.50
[W][api.connection:081]: Home Assistant 2023.2.5 (::FFFF:192.168.64.50): Connection reset
[W][api.connection:081]: Home Assistant 2023.2.5 (::FFFF:192.168.64.50): Connection reset
[W][api.connection:081]: Home Assistant 2023.2.5 (::FFFF:192.168.64.50): Connection reset
[W][api.connection:081]: Home Assistant 2023.2.5 (::FFFF:192.168.64.50): Connection reset
[D][api.connection:918]: Home Assistant 2023.2.5 (::FFFF:192.168.64.50): Connected successfully
[D][api:102]: Accepted ::FFFF:192.168.64.50
[D][api:102]: Accepted ::FFFF:192.168.64.50
[D][api.connection:918]: Home Assistant 2023.2.5 (::FFFF:192.168.64.50): Connected successfully
[D][api.connection:918]: Home Assistant 2023.2.5 (::FFFF:192.168.64.50): Connected successfully
[D][api.connection:918]: Home Assistant 2023.2.5 (::FFFF:192.168.64.50): Connected successfully
[D][api.connection:918]: Home Assistant 2023.2.5 (::FFFF:192.168.64.50): Connected successfully
[D][api.connection:918]: Home Assistant 2023.2.5 (::FFFF:192.168.64.50): Connected successfully
[D][api.connection:918]: Home Assistant 2023.2.5 (::FFFF:192.168.64.50): Connected successfully

Looks like HomeAssistant connects and then disconnects and that process keeps repeating. Is this something that is considered problematic?

Did anyone have a similar problem in the past?
Thank you!

Solved! I added a lambda code before entering deep sleep to disconnect from wifi. The problem might be that when entering deep sleep the ESP doesn’t properly disconnect from wifi. In that case, the error appears at next boot.

This is a script I use to control deep sleep. The lambda call is just before entering deep sleep.

script:
  - id: check_ota_mode
    then:
      - logger.log: "Checking for OTA mode"
      - logger.log:
          format: "State of binary sensor is %d"
          args: ['id(esp32_inside_ota_mode_esphome).state']
      - if:
          condition:
            - binary_sensor.is_on: esp32_inside_ota_mode_esphome
          then:
            - logger.log: "OTA mode enabled for esp-32-inside; preventing deep sleep"
            - deep_sleep.prevent: deep_sleep_1
          else:
            - logger.log: "OTA mode is disabled; entering deep sleep"
            # fixed Association refused error
            # https://github.com/espressif/arduino-esp32/pull/6359
            # just disconnect wifi before entering deep sleep
            - lambda: |-
                WiFi.disconnect(true);
            - deep_sleep.allow: deep_sleep_1
            - deep_sleep.enter: deep_sleep_1