Unable to find action with the name XXX ESP32S3

I’ve followed this document https://esphome.io/components/wifi I’m trying to get the on_connect to work but each time I get this error when compiling

INFO ESPHome 2025.2.2
INFO Reading configuration /config/esphome/roller-door.yaml...
Failed config

wifi: [source /config/esphome/roller-door.yaml:23]
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  output_power: 20db
  fast_connect: True
  power_save_mode: none
  on_connect: 
    - 
      Unable to find action with the name 'switch.sonoff_10023c9d7f_1'.
      switch.sonoff_10023c9d7f_1: Test
  ap: 
    ssid: Roller-Door Fallback Hotspot
    password: pPEZGn5WuRjF

Here is my yaml file…if I remove the on_connect it compiles fine. I do have an entity called
switch.sonoff_10023c9d7f_1 which is a switch.

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  output_power: 20db
  fast_connect: on
  power_save_mode: none
  on_connect:
  - switch.sonoff_10023c9d7f_1: "Test"

Am i reading the documents incorrect?
Thanks in advance.

Which example are you following?

This seems to be the only relevant one

wifi:
  # ...
  on_connect:
    - switch.turn_on: switch1
  on_disconnect:
    - switch.turn_off: switch1

Which you haven’t followed.

Hi Nick thanks for your reply…I have followed the above, I have substituted switch.turn_on with my sonoff switch but initially followed the example

wifi:
  # ...
  on_connect:
    - switch.turn_on: Test (My friendly name for my switch.sonoff_10023c9d7f_1)
  on_disconnect:
    - switch.turn_off: Test (My friendly name for my switch.sonoff_10023c9d7f_1)

And I even tried

on_connect:
    - switch.turn_on: switch.sonoff_10023c9d7f_1
  on_disconnect:
    - switch.turn_off: switch.sonoff_10023c9d7f_1

Also tried with and without on_disconnect with reference to my sonoff switch.

Please what have I missed?

providing the COMPLETE YAML file your are using.

The error seems clear to me. It says it can’t find something (that is supposed to be defined in the YAML file). That something doesn’t exist in the YAML snippet you provided. It might exist in the full file, but the syntax/spacing could be just slightly incorrect and that could cause the error you are getting (but not seeing).

Thanks Neel here is my esp32 yaml file

esphome:
  name: roller-door
  friendly_name: Roller Door

esp32:
  board: esp32-s3-devkitc-1
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "4+YR1KR+23A+D16unLo6bAzqmoNje/l7zRCdZjTHk2Q="

ota:
  - platform: esphome
    password: "8746e8464b5f4e7cddc09f9b62196051"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  output_power: 20db
  fast_connect: on
  power_save_mode: none
  on_connect:
    - switch.turn_on: switch.sonoff_10023c9d7f_1

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Roller-Door Fallback Hotspot"
    password: "pPEZGn5WuRjF"

captive_portal:

web_server:
  port: 80

I think you are right the esp32 is looking for a device connected to it, there is no reference to it. I assumed I could use entities from home assistant without the need to define inputs. Am I right?

Only if you use the homeassistant.service action.

  on_connect:
    - homeassistant.service:
        service: switch.turn_on
        data:
          entity_id: switch.sonoff_10023c9d7f_1

And only if you enable the esp device to run actions in home assistant:

Settings → Devices & Services → ESPHome → roller-door → CONFIGURE

There is no such switch in your yaml. If it is a home assistant entity, use a home assistant action as @tom_l says

Thanks Tom for your help.
This time I flashed the esp32 successfully and the log said it is connected to my network.
To test, I physically disconnected the esp32 and then reconnected, should my device switch on as per config below? if so it doesn’t. I did allow the device to perform home assistant actions in the config for my device. Is there anything else I can look into?

esphome:
  name: esphome-web-154e78
  friendly_name: Wifi
  min_version: 2024.11.0
  name_add_mac_suffix: false

esp32:
  board: esp32-s3-devkitc-1
  framework:
    type: esp-idf

# Enable logging
logger:

# Enable Home Assistant API
api:

# Allow Over-The-Air updates
ota:
- platform: esphome

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  on_connect:
    - homeassistant.service:
        service: switch.turn_on
        data:
          entity_id: switch.sonoff_10023c9d7f_1

I would have thought so.

Check the serial logs of the device when it starts up. See if it logs calling the homeassistant service.

Check the home assistant logs for errors.

Tom I cant see anything obvious in the HAS logs but the bootup logs for my ESP32 doesnt show any reference starting the home assistant service. Here is my log, the warning you see at the end is when I disconnected from my USB. Is there any configurations that need editing in the config.yaml or additional integrations that I may need. I cant find anything on how to use the correct syntax but is there an action service instead of service?

- homeassistant.service:
        service: switch.turn_on (Is there such a thing as action instead of service)
        data:
          entity_id: switch.sonoff_10023c9d7f_1

I’d love to figure this out.
Thanks in advance.

INFO ESPHome 2024.12.4
INFO Reading configuration /config/esphome/wifi.yaml...
INFO Starting log output from 192.168.1.151 using esphome API
INFO Successfully connected to wifi @ 192.168.1.151 in 0.083s
INFO Successful handshake with wifi @ 192.168.1.151 in 0.065s
[11:54:58][I][app:100]: ESPHome version 2024.12.4 compiled on Mar 12 2025, 11:53:05
[11:54:58][C][wifi:600]: WiFi:
[11:54:58][C][wifi:428]:   Local MAC: CC:BA:97:15:4E:78
[11:54:58][C][wifi:433]:   SSID: [redacted]
[11:54:58][C][wifi:436]:   IP Address: 192.168.1.151
[11:54:58][C][wifi:440]:   BSSID: 28:EE:52:C0:D2:CE[redacted]
[11:54:58][C][wifi:441]:   Hostname: 'wifi'
[11:54:58][C][wifi:443]:   Signal strength: -61 dB ▂▄▆█
[11:54:58][C][wifi:447]:   Channel: 4
[11:54:58][C][wifi:448]:   Subnet: 255.255.255.0
[11:54:58][C][wifi:449]:   Gateway: 192.168.1.1
[11:54:58][C][wifi:450]:   DNS1: 192.168.1.1
[11:54:58][C][wifi:451]:   DNS2: 0.0.0.0
[11:54:58][C][logger:185]: Logger:
[11:54:58][C][logger:186]:   Level: DEBUG
[11:54:58][C][logger:188]:   Log Baud Rate: 115200
[11:54:58][C][logger:189]:   Hardware UART: USB_CDC
[11:54:58][C][captive_portal:089]: Captive Portal:
[11:54:58][C][mdns:116]: mDNS:
[11:54:58][C][mdns:117]:   Hostname: wifi
[11:54:58][C][esphome.ota:073]: Over-The-Air updates:
[11:54:58][C][esphome.ota:074]:   Address: wifi.local:3232
[11:54:58][C][esphome.ota:075]:   Version: 2
[11:54:58][C][esphome.ota:078]:   Password configured
[11:54:58][C][safe_mode:018]: Safe Mode:
[11:54:58][C][safe_mode:020]:   Boot considered successful after 60 seconds
[11:54:58][C][safe_mode:021]:   Invoke after 10 boot attempts
[11:54:58][C][safe_mode:023]:   Remain in safe mode for 300 seconds
[11:54:58][C][api:140]: API Server:
[11:54:58][C][api:141]:   Address: wifi.local:6053
[11:54:58][C][api:143]:   Using noise encryption: YES
WARNING wifi @ 192.168.1.151: Connection error occurred: [Errno 104] Connection reset by peer
INFO Processing unexpected disconnect from ESPHome API for wifi @ 192.168.1.151
WARNING Disconnected from API
INFO Successfully connected to wifi @ 192.168.1.151 in 0.008s
INFO Successful handshake with wifi @ 192.168.1.151 in 0.093s

If you are not connected to the API you are not going to be able to call the homeasssitant.service.

Is that not because I disconnected from power?

Here is my config.yaml

default_config:
frontend:
  themes: !include_dir_merge_named themes
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
api:
http:
  server_port: 9000

I would expect after your power was re-applied to the device it would run through the entire initialisation log again, including successfully connecting to the API.

I don’t see that. I see:

I think it is working yeah? theres reference to api integration (https://developers.home-assistant.io/docs/api/rest/) I'm assuming adding this to config.yaml ap: is all thats required.

[safe_mode:020]	
  Boot considered successful after 60 seconds
12:23:55	[C]	[safe_mode:021]	
  Invoke after 10 boot attempts
12:23:55	[C]	[safe_mode:023]	
  Remain in safe mode for 300 seconds
12:23:55	[C]	[api:140]	
API Server:
12:23:55	[C]	[api:141]	
  Address: wifi.local:6053
12:23:55	[C]	[api:143]	
  Using noise encryption: YES
[D][api.connection:1446]: Home Assistant 2025.3.2 (192.168.1.105): Connected successfully

How long after connecting to wifi does the API connect?

Your action (that requires the API) occurs on connection to the wifi. If that runs before the API is ready then nothing will happen.

Try adding a delay to your wifi on_connect action, or instead of running your action on wifi connect, use the api connect trigger:

api:
  on_client_connected:
    - homeassistant.service:
        service: switch.turn_on
        data:
          entity_id: switch.sonoff_10023c9d7f_1

I have bad syntax, I’ve tried indenting different positions. Where would the time delay go…that could be a possibility.

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
api:
  on_client_connected:
    - homeassistant.service:
        service: switch.turn_on
        data:
          entity_id: switch.sonoff_10023c9d7f_1

Thanks for staying with me…appreciate it.

You don’t need the delay if you are using the api on connect trigger.

I get this now when I try to compile

 [api] is an invalid option for [wifi]. Did you mean [ap], [eap]?

Look at your oringinal config. You already have an api: component:

Yes I saw that I took it out now…I might redo my esp32 start from scratch, any other advice you suggest?