Is the normal state of the Home Assistant API - "Disconnected"?

I am puzzled by the API connection in ESPHome. This could be normal but I only detected this after using the status LED code (below) from @Protoncek in this thread.

When I first power up the ESP, I get the expected “not connected” flash pattern of “Lit, with quick off’s: No connection”. After 20-30 seconds the ESP connects to my WiFi network and I get the blink code of “Steady flashing: WiFi connection, no API”. And it stays that way. The API does not connect. When I open the log page on the ESPHome dashboard the blink pattern goes to “Short ‘on’ pulses: connected to WiFi and HA API”. When the logs window is closed, the blink pattern goes back to “Steady flashing: WiFi connection, no API”.

Is this normal?

#esp-test.yaml
substitutions:
  device_name: esp-test
  friendly_name: esp_test
  led_pin: D4

packages:
  wifi: !include common/wifi.yaml
  device_base: !include common/esp8266.yaml

#Override the default board:
esp8266:
  board: d1_mini
    
web_server:
  port: 80    


# Status light
# Lit, with quick off’s: No connection
# Steady flashing: WiFi connection, no API
# Short ‘on’ pulses: connected to HA API
#
interval:
  - interval: 2s
    then:
      if:
        condition:
          wifi.connected:
        then:
          - if:
              condition:
                api.connected:
              then:
                - light.turn_on:
                    id: status_led
                    flash_length: 1950ms
              else:
                - light.turn_on:
                    id: status_led
                    flash_length: 1000ms
        else:
          - light.turn_on:
              id: status_led
              flash_length: 50ms

light:
  - platform: binary
    id: status_led
    internal: true
    output: status_led_out

output:
  - id: status_led_out
    platform: gpio
    pin: ${led_pin}

######## Packages ########

#esp8266.yaml
esphome:
  name: ${device_name}

esp8266:
  board: esp01_1m
  framework:
    version: recommended
    
# Logger level "debug" is needed to get 1-Wire addresses, like the ds18b20.
logger:
  level: DEBUG
  # Set the log level for the light component to info
  logs:
    light: info

ota:
  safe_mode: True

# Enable Home Assistant API
api:
#WiFi.yaml
wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

sensor:
  - platform: wifi_signal
    name: ${friendly_name} WiFi Level
    id: ${friendly_name}_WiFi_level
    update_interval: 300s

# Get the WiFi details
text_sensor:
  - platform: wifi_info
    ip_address:
      name: ${friendly_name} IP
    ssid:
      name: ${friendly_name} SSID
    mac_address:
      name: ${friendly_name} Mac Address

This counts as api connection (ota logger).

So HA doesn’t connect to this esphome node via native api?

Do you also have configured mqtt?

No. I posted all the code. No MQTT.

According to the gurus over on the ESPHome discord Home Assistant should maintain the API connection.

I verified my findings on another ESP running the same YAML config file.

Has integration been set up for the ESPHome device in Home Assistant?

i.e. has the device been added to Home Assistant?

It is the Home Assistant integration that initiates the API connection.

BINGO! When I added it to the integration, the blink sequence went to connected with API.

It’s clear now- thanks.

Because you didn’t add it to HA (yet)

And now it connects :raised_hands:

47007_2

Well chosen avatar :slight_smile:

Well, i’m glad that you found a mistake… but, at the same time it seems that you found a “side effect” of my led system (short pulses when only uart connected). Is there any system to avoid that (I mean to to distinct between api an uart connected)? It seems that “api.connected” also goes for uart connection…?

No, they are indistinguishable.

https://esphome.io/components/api.html#api-connected-condition
Screenshot 2023-12-30 at 18-29-33 Native API Component

1 Like

Aha… thanks for explantion. At least now we know… after all, we should know when / if we connect phisically to our esp, correct…?:crazy_face: