ESP8266 API connection closed

Hi!
I’m rather new on Home Assistant, and with ESPHome I started today.
I first added a Wemos D1 mini with a connected button and it work out really great. I got the status to be changed on the dash board and also trigger some notify automation to send e-mail and push notification to the Mobile app.
Next I tried to add another module I had laying. It is a 2 relay output unit for 230VAC and have a AI Thinker ESP8266MOD board on top. ESP Relay Board SPST - ElectroDragon
Revision 1.5.
It is no problem connecting to it and upload yaml config thru USB first time and OTA afterwords.
I also created the two relays as “switch”. But when upload is done I can’t find the entities and the log says that the Home Assistant API connection closed. I have tried different hardware (had 10 of them never used, bought back in 2017), I have also tried delete and re-create the unit in HA, also tried a couple of board models in the yaml config (esp12e, esp01_1m…) but it behavior the same all the time. Also tried with api password in clear text and in secret file, no difference.

From the log:

INFO Reading configuration /config/esphome/relay1.yaml...
INFO Starting log output from relay1.local using esphome API
INFO Successfully connected to relay1.local
[20:22:15][I][app:102]: ESPHome version 2021.12.1 compiled on Dec 15 2021, 19:57:22
[20:22:15][C][wifi:488]: WiFi:
[20:22:15][C][wifi:350]:   Local MAC: A0:20:A6:32:8A:A0
[20:22:15][C][wifi:351]:   SSID: [redacted]
[20:22:15][C][wifi:352]:   IP Address: 192.168.2.155
[20:22:15][C][wifi:354]:   BSSID: [redacted]
[20:22:15][C][wifi:355]:   Hostname: 'relay1'
[20:22:15][C][wifi:357]:   Signal strength: -48 dB ▂▄▆█
[20:22:15][C][wifi:361]:   Channel: 1
[20:22:15][C][wifi:362]:   Subnet: 255.255.255.0
[20:22:15][C][wifi:363]:   Gateway: 192.168.2.1
[20:22:15][C][wifi:364]:   DNS1: 192.168.2.1
[20:22:15][C][wifi:365]:   DNS2: 8.8.8.8
[20:22:15][C][logger:233]: Logger:
[20:22:15][C][logger:234]:   Level: DEBUG
[20:22:15][C][logger:235]:   Log Baud Rate: 115200
[20:22:15][C][logger:236]:   Hardware UART: UART0
[20:22:15][C][captive_portal:144]: Captive Portal:
[20:22:15][C][ota:082]: Over-The-Air Updates:
[20:22:15][C][ota:083]:   Address: relay1.local:8266
[20:22:15][C][ota:086]:   Using Password.
[20:22:15][C][api:134]: API Server:
[20:22:15][C][api:135]:   Address: relay1.local:6053
[20:22:15][C][api:139]:   Using noise encryption: NO
[20:22:15][C][mdns:084]: mDNS:
[20:22:15][C][mdns:085]:   Hostname: relay1
[20:23:14][D][api:098]: Accepted 192.168.2.200
[20:23:14][W][api.connection:082]: Home Assistant 2021.12.1 (192.168.2.200): Connection closed
[20:24:14][D][api:098]: Accepted 192.168.2.200
[20:24:14][W][api.connection:082]: Home Assistant 2021.12.1 (192.168.2.200): Connection closed
[20:25:14][D][api:098]: Accepted 192.168.2.200
[20:25:14][W][api.connection:082]: Home Assistant 2021.12.1 (192.168.2.200): Connection closed
[20:26:14][D][api:098]: Accepted 192.168.2.200
[20:26:15][W][api.connection:082]: Home Assistant 2021.12.1 (192.168.2.200): Connection closed
[20:27:15][D][api:098]: Accepted 192.168.2.200
[20:27:15][W][api.connection:082]: Home Assistant 2021.12.1 (192.168.2.200): Connection closed
[20:28:15][D][api:098]: Accepted 192.168.2.200
[20:28:15][W][api.connection:082]: Home Assistant 2021.12.1 (192.168.2.200): Connection closed
[20:29:15][D][api:098]: Accepted 192.168.2.200
[20:29:15][W][api.connection:082]: Home Assistant 2021.12.1 (192.168.2.200): Connection closed

The yaml config is as basic as it can be right now:

esphome:
  name: relay1

esp8266:
  board: esp12e

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: [redacted]

ota:
  password: "3d137443f94d37a2ea119f3999b28307"

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Relay1 Fallback Hotspot"
    password: "TOVhM8MP7vEp"

captive_portal:

Anyone that have any idea what could cause this problem. Isn’t the device supported?

Did u get well HA asking you for the password when you add it as an entity ? How does it go if you remove the api password from yaml ?

Hi!
I didn’t get any question from HA about the password, I got that when adding the first ESP (Wemos), but not the other. So I assumed it should use the same.

Anyway I deleted the “password” line below the api and got a slightly different result, it was success one time then back to connection closed:

esphome:
  name: relay1

esp8266:
  board: esp12e

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  password: "3d137443f94d37a2ea119f3999b28307"

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Relay1 Fallback Hotspot"
    password: "TOVhM8MP7vEp"

captive_portal:
[20:56:13][C][ota:082]: Over-The-Air Updates:
[20:56:13][C][ota:083]:   Address: relay1.local:8266
[20:56:13][C][ota:086]:   Using Password.
[20:56:13][C][api:134]: API Server:
[20:56:13][C][api:135]:   Address: relay1.local:6053
[20:56:13][C][api:139]:   Using noise encryption: NO
[20:56:13][C][mdns:084]: mDNS:
[20:56:13][C][mdns:085]:   Hostname: relay1
[20:56:14][D][api:098]: Accepted 192.168.2.200
[20:56:14][W][api.connection:082]: ESPHome Logs 2021.12.1 (192.168.2.200): Connection closed
[20:56:16][D][api:098]: Accepted 192.168.2.200
[20:56:16][D][api.connection:771]: Home Assistant 2021.12.1 (192.168.2.200): Connected successfully
[20:56:17][D][api:098]: Accepted 192.168.2.200
[20:56:17][W][api.connection:082]: ESPHome Logs 2021.12.1 (192.168.2.200): Connection closed
[20:56:24][D][api:098]: Accepted 192.168.2.200
[20:56:24][W][api.connection:082]: ESPHome Logs 2021.12.1 (192.168.2.200): Connection closed
[20:56:34][D][api:098]: Accepted 192.168.2.200
[20:56:34][W][api.connection:082]: ESPHome Logs 2021.12.1 (192.168.2.200): Connection closed
[20:56:53][D][api:098]: Accepted 192.168.2.200
[20:56:53][W][api.connection:082]: ESPHome Logs 2021.12.1 (192.168.2.200): Connection closed

Regarding API password,

Did you specify one in HA’s configuration.yaml?

api_password: <also redacted>

All esphome devices will use the same API password

What is your Wifi and network configuration ? sorry if I missed it but what platform HA/ESPHome is running on ? PI4 ? wired or wireless ?

No I did not.
tried that one now but got error when testing config:

Integration error: api_password - Integration 'api_password' not found
# HA api settings
api_password: !secret esp_api_password

RPI4 with SSD and wired network

what about wifi and network ?

You either have to specify the same api password in esphome secret file, or make a reference to ha’s secrets file (which I did, like this:)

Now suddenly when removed the api password from esp config the module is working.
So the problem is api password related.

Just to clear things out: If I put a password for the api inside esp module yaml config file. I also need to specify that password in some other place so that HA could know what password is supposed to be used when authenticat to the ESP module over API. Is that password supposed to be inside the configuration.yaml?

yes,
I put

api_password: somepassword

this you can store in either configuration.yaml or in secrets.yaml (i prefer the latter, as it remains a secret)

And than in esphome secret.yaml i make it to
include HA’s secrets, so i don’t need to specify it twice:

<<: !include ../secrets.yaml

I have now done exactly as you described, included the HA sectrets in ESPhom secrets.
This works great with the Wemos module but on the relay module it is back and giving “conection closed” on the API.
If I delete the password line in that config it is up and running okay, but as soon I add the passwod line again the same fault appear.
I even copied the first part of the config from the wemos file so just the names were different. Same problem, wemos is working but the other module is not. then just delete the password line and both modules are working at the same time.
Could one module support the api password function and the other not?

Highly unlikely, both use ESP8266, and it depends on the software, which is in both cases ESPhome :face_with_raised_eyebrow:

I am having the same issue … very odd.
2 identical led bulbs with same yaml files with just the name different, and one connects to HA without problem while the other gives errors.

The device itself works as expected. i can use the device using the webserver component directly without HA.

[14:52:38][C][mdns:085]:   Hostname: bulb03
[14:53:28][D][api:098]: Accepted 192.168.100.174
[14:53:28][W][api.connection:082]: Home Assistant 2021.12.7 (192.168.100.174): Connection closed

I have the same problem with ESP8266 device, which I’ve renamed. I’ve deleted old device, but API is still disconnecting.

Same problem here, I get>

[16:27:15][C][api:143]: Using noise encryption: NO
[16:27:25][D][api:102]: Accepted 192.168.2.8
[16:27:25][W][api.connection:083]: Home Assistant 2022.9.1 (192.168.2.8): Connection closed
[16:27:27][D][api:102]: Accepted 192.168.2.8
[16:27:27][W][api.connection:083]: Home Assistant 2022.9.1 (192.168.2.8): Connection closed

What does the ha log tell you at the same time? And what more information do you get if you increase the log level on your esp? :scroll: :mag:

Hi all,

Same issue here 5 identical Esp32 boards with the same function (air quality temp & hum ) throughout the house and of them is giving me the same api 083 and 081

Did not change a thing.

Update:

I did a reinstall of the Esp32. So right bottom corner 3 dot, select install. After that it seemed to work again, for now.

I think I solved it!

I found a registry entry like that:

Logger: homeassistant.components.esphome
Source: components/esphome/init.py:309
Integration: ESPHome (documentation, issues)
First occurred: 13 ottobre 2022 21:38:08 (1616 occurrences)
Last logged: 22:04:07

Name of device d1-mini-1 changed to esp8266-rgb, potentially due to IP reassignment

Well:
d1-mini-1 was an old Esphome device created but no more used for a long time.
esp8266-rgb was a NEW device that always connected and disconnected.

It has been enough to remove everything about the old d1-mini-1 device and everything worked fine.

So I think you should look for something device that shares resources with your problematic one.

1 Like