[SOLVED] Make sure your YAML file contains an ‘api:’ line

Hi all,
I found several posts with this topic but none of them help me.

For my ESP8266 (and also for HA) I have static IP addresses configuration.
From HA (ssh terminal) I can ping the ESP successfully and HA recognize the ESPHome device.

When I add the integration, HA shows me the correct address but, after having clicked on “OK”, the message in topic title appear.

Configuration for device is as follow:

# Enable Home Assistant API
api:
  encryption:
    key: "21N/ejVisW8QdZ1UoMT5Q8QC+d6oDa1vwx7KwfrYz3A="

esphome:
  name: TC
  friendly_name: TC

esp8266:
  board: esp01_1m

# Enable logging
logger:

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  manual_ip:
    static_ip: xxx.xxx.xx2.1
    gateway: xxx.xxx.xx3.100
    subnet: 255.255.255.252
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "TC Fallback Hotspot"
    password: "12345678901234"

sensor:
  blablabla
  blablabla
  blablabla
  blablabla

binary_sensor:
  blablabla
  blablabla
  blablabla
  blablabla

switch:
  blablabla
  blablabla
  blablabla
  blablabla

I also try removing the encryption key from api as follow:

# Enable Home Assistant API
api:

The problem is still here.

SW platform:
HAC: 2024.1.1
HAOS: 11.3
ASSHWT: 17.0.1

HW platform:
Raspberry 4

Esphome seems to be correctly installed and I have no idea…
I really, appreciate any help!

Have you allowed ESPHome to make an API call? I can’t see rest of your yaml but just in case


Many thanks for your answer Julian !

The rest of yaml contains just sensors/switches declaration and nothing more.
Should it contains other (to allow api calls) ?

I can’t find the menu in your screenshot…
How I can allow esphome to make API calls ?

Thanks again

You don’t have to allow that. I think you are confusing this with service calls by ESP devices. That has to be allowed in the menu you showed.

@bubugian that menu is in Settings → Devices & Services → ESPHome (click the ESPHome card title, not the lower devices section.

But as I said this is not your issue.

Please post the full error message and serial logs from your device.

Thanks Tom !
When I try to connect, I get following message log.

[22:20:20][D][api:102]: Accepted 192.168.xxx3.128
[22:20:20][W][component:214]: Component api took a long time for an operation (0.29 s).
[22:20:20][W][component:215]: Components should block for at most 20-30ms.
[22:20:20][W][api.connection:102]: : Connection reset

Thanks !

Go to the ESPHome Addon web interface and click on the LOGS link for your device. Paste that here.

Tom, this is the log.
It start with a HW reset and ends with the attempt to add the device.

[00:36:50]rll\x9c\x9f|\x84l\xe0< \xc4 d\xe4 #|\x8f\x82\xe4\x93{\x92c\x84 c\x84\xfbgn\x9flno\x9c\xe3\xeccpćlrl{$p\xfbg\xe0 \xc3 d \x8c\x9c c n\xe3|\xe4 \x87\x8ec\x84\xfbgn\xe7l\x84\x87l`\x98\033og\x87$gs\x8fۓn {r\x93`x\xf3o\xe0 {\x8cܜ\x9c\xc0\x84 b g\xe3| \x8f c\x84\xfbgn\xe7 \x8fl`\x90\033'o \x8ealao;\x87\x93\x9bgr$`{\x92\x93o {d`\x9c \xe4\x8f8\x84\x8fl \xfc\xc3g\x9cad[I][logger:355]: Log initialized
[00:36:50][C][ota:473]: There have been 1 suspected unsuccessful boot attempts. [00:36:50][I][app:029]: Running through setup()... 
[00:36:50][C][switch.gpio:011]: Setting up GPIO Switch 'LED TC'... 
[00:36:50][D][switch:016]: 'LED TC' Turning OFF. 
[00:36:50][D][switch:055]: 'LED TC': Sending state OFF 
[00:36:50][D][switch:016]: 'LED TC' Turning OFF. 
[00:36:50][D][template.switch:046]: Restored state OFF 
[00:36:50][D][switch:016]: 'Blink led' Turning OFF. 
[00:36:50][D][switch:016]: 'LED TC' Turning OFF. 
[00:36:50][D][switch:055]: 'Blink led': Sending state OFF 
[00:36:50][C][adc:047]: Setting up ADC 'NTC temperature'... 
[00:36:50][C][adc:093]: ADC 'NTC temperature' setup finished! 
[00:36:50][C][wifi:038]: Setting up WiFi... [00:36:50][C][wifi:051]: Starting WiFi... [00:36:50][C][wifi:052]: Local MAC: 50:82:91:68:3B:A3 
[00:36:50][D][wifi:455]: Starting scan... 
[00:36:51][D][sensor:093]: 'NTC temperature': Sending state 0.00391 V with 2 decimals of accuracy 
[00:36:51][D][binary_sensor:034]: 'Stato cicalino TC': Sending initial state ON 
[00:36:56][D][wifi:470]: Found networks: 
[00:36:56][I][wifi:513]: - 'home_ntw' (AC:22:0B:85:91:00) [redacted]▂▄▆█ 
[00:36:56][D][wifi:515]: Channel: 10 
[00:36:56][D][wifi:516]: RSSI: -62 dB 
[00:36:56][D][wifi:518]: - 'home_ntw-FV'[redacted] (20:0D:B9:AD:1F:44) [redacted]▂▄▆█ 
[00:36:56][I][wifi:300]: WiFi Connecting to 'home_ntw'... 
[00:36:58][I][wifi:587]: WiFi Connected! 
[00:36:58][C][wifi:405]: Local MAC: 50:82:91:68:3B:A3 
[00:36:58][C][wifi:410]: SSID: 'home_ntw'[redacted] 
[00:36:58][C][wifi:411]: IP Address: 192.168.xx2.1 
[00:36:58][C][wifi:412]: BSSID: AC:29:18:85:91:00[redacted] 
[00:36:58][C][wifi:414]: Hostname: 'TC' 
[00:36:58][C][wifi:416]: Signal strength: -67 dB ▂▄▆█ 
[00:36:58][C][wifi:420]: Channel: 10 
[00:36:58][C][wifi:421]: Subnet: 255.255.255.252 
[00:36:58][C][wifi:422]: Gateway: 192.168.xx3.127 
[00:36:58][C][wifi:423]: DNS1: 0.0.0.0 
[00:36:58][C][wifi:424]: DNS2: 0.0.0.0 
[00:36:58][D][wifi:596]: Disabling AP... 
[00:36:58][C][ota:097]: Over-The-Air Updates: 
[00:36:58][C][ota:098]: Address: 192.168.xx2.1:8266 
[00:36:58][C][ota:101]: Using Password. 
[00:36:58][C][api:025]: Setting up Home Assistant API server... 
[00:36:58][I][app:062]: setup() finished successfully! 
[00:36:58][D][sensor:093]: '${friendly_name} WiFi signal': Sending state -67.00000 dBm with 0 decimals of accuracy 
[00:36:58][D][sensor:093]: 'WiFi signal Percent': Sending state 66.00000 Signal % with 0 decimals of accuracy 
[00:36:58][I][app:102]: ESPHome version 2023.12.5 compiled on Jan 6 2024, 00:34:57 
[00:36:58][C][wifi:573]: WiFi: 
[00:36:58][C][wifi:405]: Local MAC: 50:82:91:A8:3B:A3 
[00:36:58][C][wifi:410]: SSID: 'home_ntw'[redacted] 
[00:36:58][C][wifi:411]: IP Address: 192.168.xx2.1 
[00:36:58][C][wifi:412]: BSSID: AC:29:18:85:91:00[redacted] 
[00:36:58][C][wifi:414]: Hostname: 'TC' 
[00:36:58][C][wifi:416]: Signal strength: -67 dB ▂▄▆█ 
[00:36:58][C][wifi:420]: Channel: 10 
[00:36:58][C][wifi:421]: Subnet: 255.255.255.252 
[00:36:58][C][wifi:422]: Gateway: 192.168.xx3.127 
[00:36:58][C][wifi:423]: DNS1: 0.0.0.0 
[00:36:58][C][wifi:424]: DNS2: 0.0.0.0 
[00:36:58][C][logger:443]: Logger: 
[00:36:58][C][logger:444]: Level: DEBUG 
[00:36:58][C][logger:445]: Log Baud Rate: 115200 
[00:36:58][C][logger:447]: Hardware UART: UART0 
[00:36:58][C][gpio.binary_sensor:015]: GPIO Binary Sensor 'Stato cicalino TC' 
[00:36:58][C][gpio.binary_sensor:016]: Pin: GPIO0 
[00:36:58][C][switch.gpio:068]: GPIO Switch 'LED TC' 
[00:36:58][C][switch.gpio:090]: Restore Mode: always OFF 
[00:36:58][C][switch.gpio:031]: Pin: GPIO2 
[00:36:58][C][template.switch:068]: Template Switch 'Blink led' 
[00:36:58][C][template.switch:090]: Restore Mode: always OFF 
[00:36:58][C][template.switch:057]: Optimistic: YES 
[00:36:58][C][adc:097]: ADC Sensor 'NTC temperature' 
[00:36:58][C][adc:097]: Device Class: 'voltage' 
[00:36:58][C][adc:097]: State Class: 'measurement' 
[00:36:58][C][adc:097]: Unit of Measurement: 'V' 
[00:36:58][C][adc:097]: Accuracy Decimals: 2 
[00:36:58][C][adc:102]: Pin: GPIO17 
[00:36:58][C][adc:142]: Update Interval: 20.0s 
[00:36:58][C][copy.sensor:015]: Copy Sensor 'WiFi signal Percent' 
[00:36:58][C][copy.sensor:015]: State Class: 'measurement' 
[00:36:58][C][copy.sensor:015]: Unit of Measurement: 'Signal %' 
[00:36:58][C][copy.sensor:015]: Accuracy Decimals: 0 
[00:36:58][C][captive_portal:088]: Captive Portal: 
[00:36:58][C][mdns:115]: mDNS: 
[00:36:58][C][mdns:116]: Hostname: TC 
[00:36:58][C][ota:097]: Over-The-Air Updates: 
[00:36:58][C][ota:098]: Address: 192.168.xx2.1:8266 
[00:36:58][C][ota:101]: Using Password. 
[00:36:58][C][api:139]: API Server: 
[00:36:58][C][api:140]: Address: 192.168.xx2.1:6053 
[00:36:58][C][api:142]: Using noise encryption: YES 
[00:36:58][C][wifi_signal.sensor:009]: WiFi Signal '${friendly_name} WiFi signal' [00:36:58][C][wifi_signal.sensor:009]: Device Class: 'signal_strength' 
[00:36:58][C][wifi_signal.sensor:009]: State Class: 'measurement' 
[00:36:58][C][wifi_signal.sensor:009]: Unit of Measurement: 'dBm' 
[00:36:58][C][wifi_signal.sensor:009]: Accuracy Decimals: 0 
[00:37:03][D][sensor:093]: 'NTC temperature': Sending state 0.00391 V with 2 decimals of accuracy 
[00:37:04][D][sensor:093]: '${friendly_name} WiFi signal': Sending state -68.00000 dBm with 0 decimals of accuracy 
[00:37:04][D][sensor:093]: 'WiFi signal Percent': Sending state 64.00000 Signal % with 0 decimals of accuracy 
[00:37:14][D][sensor:093]: '${friendly_name} WiFi signal': Sending state -68.00000 dBm with 0 decimals of accuracy 
[00:37:14][D][sensor:093]: 'WiFi signal Percent': Sending state 64.00000 Signal % with 0 decimals of accuracy 
[00:37:23][D][sensor:093]: 'NTC temperature': Sending state 0.00391 V with 2 decimals of accuracy 
[00:37:23][D][api:102]: Accepted 192.168.xx3.128 
[00:37:24][W][component:214]: Component api took a long time for an operation (0.29 s). 
[00:37:24][W][component:215]: Components should block for at most 20-30ms. 
[00:37:24][D][sensor:093]: '${friendly_name} WiFi signal': Sending state -69.00000 dBm with 0 decimals of accuracy 
[00:37:24][D][sensor:093]: 'WiFi signal Percent': Sending state 62.00000 Signal % with 0 decimals of accuracy 
[00:37:24][W][api.connection:102]: : Connection reset

And this is complete yaml:

esphome:
  name: TC
  friendly_name: TC

esp8266:
  board: esp01_1m

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "jjMZKWYIeoEabTE57hvKwUGxN+pw0YEPZerg4SD/gIs="

ota:
  password: "5d2dd55bbb2dbe9def24f05129096171"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  manual_ip:
    static_ip: 192.168.xx2.1
    gateway: 192.168.xx3.127
    subnet: 255.255.255.252

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

captive_portal:

#dallas:
#  - pin: GPIO5
  # Non ci dovrebbero essere problemi con le lunghezze dei cavi.
  # Il collegamento di multipli sensori può essere realizzato come segue:
  # https://lastminuteengineers.com/multiple-ds18b20-arduino-tutorial/

sensor:
  - platform: adc
    pin: A0
    name: "NTC temperature"
    id: TC_temperatura_NTC
    update_interval: 20s

  #- platform: dht
  #  pin: GPIO12
  #  temperature:
  #    name: "Temperatura aria vano TC"
  #  humidity:
  #    name: "Umidita' aria vano TC"
  #  update_interval: 10s

  - platform: wifi_signal       #Potenza del segnale WiFi
    name: "${friendly_name} WiFi signal"
    id: TC_wifi_signal_db
    update_interval: 10s
    entity_category: "diagnostic"

  - platform: copy              #Riporta la potenza del segnale WiFi in percentuale
    source_id: TC_wifi_signal_db
    name: "WiFi signal Percent"
    filters:
      - lambda: return min(max(2 * (x + 100.0), 0.0), 100.0);
    unit_of_measurement: "Signal %"
    entity_category: "diagnostic"
    device_class: ""

  #- platform: dallas
  #  address: 0xA40000031F055028 #Cambiare address sensore
  #  name: "Temperature #1"      #Cambiare nome sensore
    
  #- platform: dallas
  #  address: 0xDD0000031EFB0428 #Cambiare address sensore
  #  name: "Temperature #2"      #Cambiare nome sensore
  #aggiungere sensori dallas necessari

binary_sensor:
  - platform: gpio
    id: TC_stato_cicalino
    pin: 
      number: GPIO0
      mode:
          input: True
          pullup: True  #pulluppato per default. Boot fallisce se pulldown.
    name: "Stato cicalino TC"
    filters:
      - delayed_on: 10ms  #debounce

switch:  
  - platform: gpio
    id: TC_LED
    pin: GPIO2
    name: "LED TC"

  # il LED non lampeggia !
  - platform: template
    name: "Blink led"
    optimistic: yes
    id: ledblink
    turn_on_action:
    - while:
        condition:
          lambda: 'return true;'
        then:
        - switch.turn_on: TC_LED
        - delay: 500ms 
        - switch.turn_off: TC_LED
        - delay: 500ms
    turn_off_action:
    - switch.turn_off: TC_LED

Thanks for your help !

Wifi signal strength is ok but it is dropping the connection for some reason:

Does your LED have an appropriate current limiting resistor?

It’s the onboard LED, so resistor is by design.

Maybe this ESP8266 has some problem. I’ll try with a new one.
I’ll be back soon.

EDIT
I try with 2 new ESP8266 boards. No changes.
So, in order to avoid power consumption peaks or GPIO misunderstandings, I try to comment the LED gpio definition and LED blink routine (it has never worked).
No changes again: result is the same ‘connection reset’

I try disabling the “manual IP” section allowing the IP address to be assigned by the router and… BINGO! It works!

So, I’ve understood that the issue was a network configuration problem and upon investigating, I realized I had a big mistake with the subnet mask.

It was 255.255.255.252 instead of 255.255.252.0..

Thanks all for the help and sorry for the mistake !

1 Like