ESPHome device can't connect after HA restart

Hey y’all

I have a sensor running esphome and each time home assistant restarts, the sensor fails to connect to the esphome integration. I’m having to manually delete the sensor and re-add it on esphome using the correct IP. It seems to me as though the integration is checking the wrong IP.

Here’s my yaml for the sensor:

substitutions:
  name: "mmwave1"
  friendly_name: "mmwave1"

esphome:
  name: "${name}"
  friendly_name: "${friendly_name}"
  name_add_mac_suffix: true
  project:
    name: "mmwave.1"
    version: "3.0"
  platformio_options:
    board_build.flash_mode: dio
    board_build.mcu: esp32c3

# external_components:
#   - source: github://limengdu/mmwave-kit-external-components@main
#     refresh: 0s

esp32:
  board: esp32-c3-devkitm-1
  variant: esp32c3
  framework:
    type: esp-idf

api:
  reboot_timeout: 5s
    
# Enable logging
logger:
  hardware_uart: USB_SERIAL_JTAG
  level: DEBUG

ota:
  - platform: esphome

wifi:
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "mmwave1"
  reboot_timeout: 10s
  use_address: 192.168.0.243
    
captive_portal:

# Sets up Bluetooth LE (Only on ESP32) to allow the user
# to provision wifi credentials to the device.
esp32_improv:
  authorizer: none

# Sets up the improv via serial client for Wi-Fi provisioning.
# Handy if your device has a usb port for the user to add credentials when they first get it.
# improv_serial: # Commented until improv works with usb-jtag on idf

uart:
  id: uart_bus
  baud_rate: 115200
  rx_pin: 4
  tx_pin: 5
  parity: NONE
  stop_bits: 1

seeed_mr24hpc1:
  id: my_seeed_mr24hpc1

text_sensor:
  - platform: seeed_mr24hpc1
    heart_beat:
      name: "Heartbeat"
    product_model:
      name: "Product Model"
    product_id:
      name: "Product ID"
    hardware_model:
      name: "Hardware Model"
    hardware_version:
      name: "Hardware Version"
    keep_away:
      name: "Active Reporting Of Proximity"
    motion_status:
      name: "Motion Information"
    custom_mode_end:
      name: "Custom Mode Status"

binary_sensor:
  - platform: seeed_mr24hpc1
    has_target:
      name: "Presence Information"

sensor:
  - platform: seeed_mr24hpc1
    custom_presence_of_detection:
      name: "Static Distance"
    movement_signs:
      name: "Body Movement Parameter"
    custom_motion_distance:
      name: "Motion Distance"
    custom_spatial_static_value:
      name: "Existence Energy"
      id: eval
    custom_spatial_motion_value:
      name: "Motion Energy"
      id: mval
    custom_motion_speed:
      name: "Motion Speed"
    custom_mode_num:
      name: "Current Custom Mode"

switch:
  - platform: seeed_mr24hpc1
    underlying_open_function:
      name: Underlying Open Function Info Output Switch
      
button:
  - platform: seeed_mr24hpc1
    restart:
      name: "Module Restart"
    custom_set_end:
      name: "End Of Custom Mode Settings"

select:
  - platform: seeed_mr24hpc1
    scene_mode:
      name: "Scene"
    unman_time:
      name: "Time For Entering No Person State (Standard Function)"
    existence_boundary:
      name: "Existence Boundary"
    motion_boundary:
      name: "Motion Boundary"

number:
  - platform: seeed_mr24hpc1
    sensitivity:
      name: "Sensitivity"
    custom_mode:
      name: "Custom Mode"
    existence_threshold:
      name: "Existence Energy Threshold"
    motion_threshold:
      name: "Motion Energy Threshold"
    motion_trigger:
      name: "Motion Trigger Time"
    motion_to_rest:
      name: "Motion To Rest Time"
    custom_unman_time:
      name: "Time For Entering No Person State (Underlying Open Function)"

Here’s the error log after the restart:

Logger: aioesphomeapi.reconnect_logic
Source: runner.py:154
First occurred: 3:14:52 PM (2 occurrences)
Last logged: 3:14:52 PM

Can't connect to ESPHome API for mmwave1-c9a120 @ 192.168.0.243: Error while starting connection: Starting connection cancelled (APIConnectionCancelledError)
Can't connect to ESPHome API for mmwave1-c9a120 @ 192.168.0.205: Error connecting to [AddrInfo(family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_STREAM: 1>, proto=6, sockaddr=IPv4Sockaddr(address='192.168.0.205', port=6053))]: [Errno 111] Connect call failed ('192.168.0.205', 6053) (SocketAPIError)

The sensor is connected with IP 192.168.0.243 on my router [which is a access point router with dhcp disabled - so the main parent router to which my access point router is connected assigns the ip]. After HA restarts - it fails to reconnect. So I delete the device. It then shows up under discovered devices. I click on Add - it throws me the error that you see in the picture.

Why on earth is it checking 192.168.0.205??

When I replace the host with the correct ip ie. 192.168.0.243, it connects right away. Also, on the esphome builder add-on, the sensor shows as Online after HA restarts - which is correct. It’s weird the integration’s failing.

Thanks for your help.

Maybe because you wifi component is setup wrong.

# Example configuration entry
wifi:
  ssid: MyHomeNetwork
  password: VerySafePassword

  # Optional manual IP
  manual_ip:
    static_ip: 192.168.0.123
    gateway: 192.168.0.1
    subnet: 255.255.255.0

Static address should be set like this.

use_address: 192.168.0.243

is used for flasing if you need to change the static address.

I did try that as well - setting up static up like this :

wifi:
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "mmwave1"
  reboot_timeout: 10s
  use_address: 192.168.0.243
  manual_ip:
    static_ip: 192.168.0.243
    gateway: 192.168.0.1
    subnet: 255.255.255.0

Still no luck.

DO NOT use the use_address: option - it is specifically for changing the node name of the ESPHome device - not to define the devices permanent IP address.

I’m using that to flash the device though. If I don’t include use_address, esphome builder won’t flash OTA since it won’t know which ip to connect to

This is my static ip esphome config

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  manual_ip:
    static_ip: 192.168.20.158
    gateway: 192.168.20.1
    subnet: 255.255.255.0
    dns1: 192.168.20.1

You appear to have mucked up you mDNS cache then. You would be better to flash the device via USB, remove the use_address option.

It’s unclear from your post - is there any particular reason you are provisioning the wifi via AP/BLE - do you have multiple networks? Or have you replaced one network with a different SSID? Why not provide the SSID and password in the yaml - the ap: and captive_portal: config will still let you reconfigure the device if the network changes, including allowing you to wirelessly update the firmware with the new network credentials.

Remove the static option then to flash it, then set the static IP via you DHCP server, it is far more reliable that way.

not sure what I should be using for dns1. Where would I need to lookup this value? Thanks

Device is in client’s office with multiple networks. So it made sense to use ap to configure wifi vs define ssid and password in the code [so we could connect to a network with good strength]. I have remote access to HA and hence I’m doing this OTA - I want to avoid visiting client’s office to set it up via USB

You should be using whatever your dns instance is. Probably your main router ip. I’m running esphome devices on vlan ssid so this is what i use in my local setup.

Ap timeout is very short. Set it to much longer eg 30 mins as a test and see if it reconnects after a restart of HA.
Set api timeout to 15mins.
It would be best if you showed what you have redacted so we know you havent just left things out of your full yaml. Eg api encryption

Thank you - I shall try that. I actually haven’t redacted anything. I always used api without any encryption. Pardon my naivety but I assumed that was optional? I could include it if necessary [or if it’ll help solve this at least].

As a temporary workaround I configured the device to transmit over HA’s mqtt.

I only used it as an example of something you may have redacted.