ESPHome Device keeps reverting to old IP address in home assistant after reboot, even after deleting and re-adding it

I have a very strange issue for the esphome integration.

I have a m5stack atom device (ESP32 based) flashed with esphome and connected to home assistant. The device is named m5stack-atom-speaker-kit-af4e34. Previously it got an IP address 10.151.30.102. Some day I decided to reserve a new DHCP IP address 10.151.30.156 for this device with the FQDN m5stack-atom-speaker-kit-af4e34.iot.pitwinken.com from my DNS server. However, I encountered a strange problem. No matter the way I tried to let home assistant use the new IP address for the esphome device, after restart of home assistant, the configuration reverted to the old IP address on its own, making connection to the esphome device fail.

I have tried:

  • Deleting the device from the esphome integration page and re-adding it with the FQDN. The connection is successful afterwards. But the problem appears again after restarting home assistant
  • Stopping the HA container, manually editing the core.config_entries to change the old IP address to the new one, and starting the HA container again, as described in the thread titled “Device IP change - how to update in HA without having to re-add device and reconfigure scripts” (sorry can’t post too many links for a new user). The manual changes get reverted after starting the HA container.

In both cases, I can watch the relevant entry in the core.config_entries get reverted right after home assistant start-up with the linux watch command.

I am out of ideas. Anyone knows how to solve this? Thanks

System set-up

Proxmox running virtual machine with docker installed. The docker-compose file is as follows:

---
version: "3.7"

services:
  postdb1:
    container_name: postdb1
    #network_mode: host
    restart: unless-stopped
    image: postgres:13
    volumes:
      - "/opt/appdata/homeassistant/postdb:/var/lib/postgresql/data"
      - /etc/localtime:/etc/localtime:ro
      - /etc/passwd:/etc/passwd:ro
      - /etc/keys/hass_postgres_password:/etc/keys/hass_postgres_password:ro
    environment:
      - "POSTGRES_USER=homeassistant"
      - "POSTGRES_PASSWORD_FILE=/etc/keys/hass_postgres_password"
    ports:
      - 127.0.0.1:5432:5432
    user: "1000:1000"
  homeassistant:
    image: lscr.io/linuxserver/homeassistant:latest
    container_name: homeassistant
    network_mode: host
    depends_on:
      - postdb1
    environment:
      - "PUID=1000"
      - "PGID=1000"
      - "TZ=Asia/Hong_Kong"
    volumes:
      - "/opt/appdata/homeassistant/config:/config"
      - "/opt/appdata/homeassistant/media:/media"
    #ports:
    #  - "8123:8123"
    restart: unless-stopped

Esphome integration log after enabling debug logging, restarting home assistant and disabling debug logging

Home Assistant Log with debug mode enabled

Welcome to the forum, @thorn.unsure57 :wave:t3:

Maybe you have an entry like the following within m5stack-atom-speaker-kit-af4e34.yaml ?

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  use_address: 10.151.30.102

If so just change the entry to

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
use_address: 10.151.30.156

and reinstall m5stack-atom-speaker-kit-af4e34 thereafter.

Additionally, if you are using somerthing like Pi-Hole within your LAN check whether you have added an entry for that device within “Clients” and change the ip-address if so.

@Tamsy thanks a lot for your ideas. I have checked that my esphome config that use_address is absent. Also I use Technitium DNS instead of Pi-Hole, and I have checked that my DNS entries are correct there.

But I managed to solve this issue.

I found that a related issue ESPHome - Recorded IP address of ESP device changes on HASS reboot. · Issue #118239 · home-assistant/core · GitHub already existed on GitHub. There was another issue Android TV Remote re-authenticates the wrong device · Issue #109314 · home-assistant/core · GitHub linking back to this issue, with the hint that this might be related to zeroconf. I have tried to disable zeroconf by using the custom integration default_config_disabler as mentioned in that GitHub issue, but the issue still existed. I also had verified that the zeroconf entries are correct by spinning up a VM on the same subnet as the esphome device and run avahi-browse. Apparently zeroconf isn’t the cause.

I then browsed the code for configuring devices in esphome integration (relevant code on github dot com/home-assistant/core/blob/dev/homeassistant/components/esphome/config_flow.py, have to post it like this again due to the stupid two hyperlink limit for new users on this forum), trying to gain some insights. Without understanding much what’s going on (the home assistant codebase is definitely huge), it seemed that apart from zeroconf (async_step_zeroconf function), MQTT is also used for auto-discovery (async_step_mqtt function). I then checked my MQTT broker with MQTT explorer.

Boom, that’s it. There was a lingering retained topic esphome/discover/m5stack-atom-speaker-kit-af4e34, with the wrong old IP address. I cleared that topic, restarted home assistant, and home assistant still uses the correct new address afterwards.

I have added the following entry in the esphome m5stack-atom-speaker-kit-af4e34.yml as a workaround back then. It seems that my media player refuse to work without the log being sent to somewhere. I didn’t have much time to debug the issue back then so I didn’t report it to esphome. I believe that I didn’t put discovery: false in the section under mqtt at one time, causing it to put a discovery message on the MQTT broker. The message didn’t get clear afterwards and left as is, causing the trouble.

logger:

mqtt:
  broker: mqtt.iot.pitwinken.com
  username: ken
  password: !secret mqtt_password
  discovery: false
1 Like