Toggling a boolean on wake up from deepsleep

I have an esp32-c3 that I wish to use to register when a button is pressed. As this will be running off of a small battery, I would it to be in deep sleep until the button is pressed. Googling around I came up with the below which makes logical sense to me but won’t compile.

The idea is that when the button is pressed on the esp32-c3, it toggles a variable I have in HA that I can then use in an automation.

esphome:
  name: button-01
  friendly_name: Button-01

esp32:
  board: esp32-c3-devkitm-1
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: !secret ekey-button-01

ota:
  - platform: esphome
    password: !secret ota-button-01

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

# Set up the deep sleep wake-up mechanism
deep_sleep:
  id: deep_sleep_1
  wakeup_pin: GPIO0  # Wake up when GPIO0 is pressed
  run_duration: 5s  # Duration to run before going to sleep again
  sleep_duration: 0s  # Sleep indefinitely until GPIO0 is pressed

# Actions after wake-up (set button_01_pushed to True for 2 seconds, then back to False)
on_boot:
  priority: 100
  then:
    - homeassistant.service:
        service: input_boolean.turn_on
        target:
          entity_id: input_boolean.button_01_pushed
    - delay: 2s  # Wait for 2 seconds
    - homeassistant.service:
        service: input_boolean.turn_off
        target:
          entity_id: input_boolean.button_01_pushed
    - deep_sleep.enter: deep_sleep_1

The main error appears to be "ModuleNotFoundError: No module named ‘esphome.components.on_boot’ so I need to know what I do to make the last part execute on boot.

on_boot goes under the esphome section,

esphome:
  name: button-01
  friendly_name: Button-01
  on_boot:
    priority: 100
    then: etc...

Note you may need to wake for more than 5 seconds to connect to wifi. There is a fast_connect option that may help, see: WiFi Component — ESPHome

Thanks Tom, I now have

esphome:
  name: button-01
  friendly_name: Button-01
  # Actions after wake-up (set button_01_pushed to True for 2 seconds, then back to False)
  on_boot:
    priority: 100
    then:
      - homeassistant.service:
          service: input_boolean.turn_on
          target:
            entity_id: input_boolean.button_01_pushed
      - delay: 2s  # Wait for 2 seconds
      - homeassistant.service:
          service: input_boolean.turn_off
          target:
            entity_id: input_boolean.button_01_pushed
      - deep_sleep.enter: deep_sleep_1

esp32:
  board: esp32-c3-devkitm-1
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: !secret ekey-button-01

ota:
  - platform: esphome
    password: !secret ota-button-01

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

# Set up the deep sleep wake-up mechanism
deep_sleep:
  id: deep_sleep_1
  wakeup_pin: GPIO0  # Wake up when GPIO0 is pressed
  run_duration: 5s  # Duration to run before going to sleep again
  sleep_duration: 0s  # Sleep indefinitely until GPIO0 is pressed

I now get

INFO ESPHome 2025.2.2
INFO Reading configuration /config/esphome/button-01.yaml...
Failed config

esphome: None
  name: button-01
  friendly_name: Button-01
  on_boot: 
    priority: 100
    then: 
      - homeassistant.service: 
          service: input_boolean.turn_on
          
          [target] is an invalid option for [homeassistant.service]. Please check the indentation.
          target: 
            entity_id: input_boolean.button_01_pushed
      - delay: 2s
      - homeassistant.service: 
          service: input_boolean.turn_off```

so I have indented the parts under service,

   then:
      - homeassistant.service:
          service: input_boolean.turn_on
            target:
              entity_id: input_boolean.button_01_pushed
      - delay: 2s  # Wait for 2 seconds
      - homeassistant.service:
          service: input_boolean.turn_off
            target:
              entity_id: input_boolean.button_01_pushed
      - deep_sleep.enter: deep_sleep_1```

but I now get

INFO ESPHome 2025.2.2
INFO Reading configuration /config/esphome/button-01.yaml...
ERROR Error while reading config: Invalid YAML syntax:

mapping values are not allowed here
  in "/config/esphome/button-01.yaml", line 10, column 19

Which refers to the ‘:’ in the first target line

            target:

Try this:

   then:
      - homeassistant.service:
          service: input_boolean.turn_on
          data:
            entity_id: input_boolean.button_01_pushed

Note you have to allow the device to perform services in home assistant.

Settings → Devices & Services → ESPHome → Button-01 → CONFIGURE

Awesome, code compiled, thanks… can continue now with testing

I now have another issue.

The logs reports the following:

[17:40:30][C][api:140]: API Server:
[17:40:30][C][api:141]:   Address: button-01.local:6053
[17:40:30][C][api:143]:   Using noise encryption: YES
[17:40:30][C][deep_sleep:026]: Setting up Deep Sleep...
[17:40:30][C][deep_sleep:029]:   Sleep Duration: 0 ms
[17:40:30][C][deep_sleep:032]:   Run Duration: 10000 ms
[17:40:30][C][deep_sleep:043]:   Wakeup Pin: GPIO0
[17:40:32][I][deep_sleep:060]: Beginning Deep Sleep
[17:40:32][I][deep_sleep:062]: Sleeping for 0us
[17:40:32][D][esp32.preferences:114]: Saving 1 preferences to flash...
[17:40:32][D][esp32.preferences:143]: Saving 1 preferences to flash: 0 cached, 1 written, 0 failed
INFO Processing expected disconnect from ESPHome API for button-01 @ 192.168.20.102
WARNING Disconnected from API
INFO Successfully connected to button-01 @ 192.168.20.102 in 2.214s
INFO Successful handshake with button-01 @ 192.168.20.102 in 0.072s
[17:40:41][I][deep_sleep:060]: Beginning Deep Sleep
[17:40:41][I][deep_sleep:062]: Sleeping for 0us
[17:40:41][D][esp32.preferences:114]: Saving 1 preferences to flash...
[17:40:41][D][esp32.preferences:143]: Saving 1 preferences to flash: 0 cached, 1 written, 0 failed
INFO Processing expected disconnect from ESPHome API for button-01 @ 192.168.20.102
WARNING Disconnected from API
INFO Successfully connected to button-01 @ 192.168.20.102 in 2.154s
INFO Successful handshake with button-01 @ 192.168.20.102 in 0.069s
[17:40:50][I][deep_sleep:060]: Beginning Deep Sleep
[17:40:50][I][deep_sleep:062]: Sleeping for 0us
[17:40:50][D][esp32.preferences:114]: Saving 1 preferences to flash...
[17:40:50][D][esp32.preferences:143]: Saving 1 preferences to flash: 0 cached, 1 written, 0 failed
INFO Processing expected disconnect from ESPHome API for button-01 @ 192.168.20.102
WARNING Disconnected from API
INFO Successfully connected to button-01 @ 192.168.20.102 in 3.145s
INFO Successful handshake with button-01 @ 192.168.20.102 in 0.071s
[17:40:59][D][api:103]: Accepted 192.168.20.80
[17:40:59][D][api.connection:1446]: Home Assistant 2025.3.1 (192.168.20.80): Connected successfully
[17:40:59][I][deep_sleep:060]: Beginning Deep Sleep
[17:40:59][I][deep_sleep:062]: Sleeping for 0us
[17:40:59][D][esp32.preferences:114]: Saving 1 preferences to flash...
[17:40:59][D][esp32.preferences:143]: Saving 1 preferences to flash: 0 cached, 1 written, 0 failed
INFO Processing expected disconnect from ESPHome API for button-01 @ 192.168.20.102
WARNING Disconnected from API
INFO Successfully connected to button-01 @ 192.168.20.102 in 2.156s
INFO Successful handshake with button-01 @ 192.168.20.102 in 0.113s
[17:41:08][I][deep_sleep:060]: Beginning Deep Sleep
[17:41:08][I][deep_sleep:062]: Sleeping for 0us
[17:41:08][D][esp32.preferences:114]: Saving 1 preferences to flash...
[17:41:08][D][esp32.preferences:143]: Saving 1 preferences to flash: 0 cached, 1 written, 0 failed
INFO Processing expected disconnect from ESPHome API for button-01 @ 192.168.20.102
WARNING Disconnected from API

Does this mean that the devices is contunally waking up even though there is no button being pushed? I have nothing connected to the board other than the USB lead providing power.

It reconnects every 13s

Add this:

api:
  encryption:
    key: !secret ekey-button-01
  reboot_timeout: 0s

The device starts a 15 minute timer to reboot the device when it loses connection to the API. Setting it to 0s will prevent this. There’s also one for the wifi:

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  reboot_timeout: 0s

The timing (13 seconds ) does not seem right for that though. So…

You probably want to use another GPIO as well. GPIO 0 does funny things at start up. See the table lower down on this page: ESP32 Pinout Reference: Which GPIO pins should you use? | Random Nerd Tutorials

Also how are you connecting the GPIO to the switch?

It should be pulled to GND via a resistor (10k to 50k Ohms) and the button should apply 3.3V.

Thanks again, yes I realised I would need a pull down resistor but haven’t attached a button yet. I wanted the button to be the same as the wake pin. Basically trying to rig a door bell button. Need something that does not use much power. When the wake-up button is pressed, a variable is toggled allowing an automation to broadcast the fact on my speakers.

I realise there will be a delay in wifi connecting etc but wanted to test it out anyway. But I am also open to other ideas.

Added the rebooot_timesout: 0s but still rebooting itself.

Changed to GPIO1

Could powering via the USB on the board be causing this?

My board is esp32-c3 (pinout)

esphome:
  name: button-01
  friendly_name: Button-01
  # Actions after wake-up (set button_01_pushed to True for 2 seconds, then back to False)
  on_boot:
    priority: 100
    then:
      - logger.log: "Button pressed, waking up..."
      - homeassistant.service:
          service: input_boolean.turn_on
          data:
            entity_id: input_boolean.button_01_pushed
      - delay: 10s  # Wait for 10 seconds
      - homeassistant.service:
          service: input_boolean.turn_off
          data:
            entity_id: input_boolean.button_01_pushed
      - logger.log: "Entering deep sleep..."
      - deep_sleep.enter: deep_sleep_1

esp32:
  board: esp32-c3-devkitm-1
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: !secret ekey-button-01
  reboot_timeout: 0s

ota:
  - platform: esphome
    password: !secret ota-button-01

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  reboot_timeout: 0s

# Set up the deep sleep wake-up mechanism
deep_sleep:
  id: deep_sleep_1
  wakeup_pin: GPIO1  # Wake up when GPIO1 is pressed
  run_duration: 10s  # Duration to run before going to sleep again
  sleep_duration: 0s  # Sleep indefinitely until GPIO1 is pressed

Also not a good choice. Check the table again.

No.

You need this. If the pin is floating it may pick up noise and trigger the wake up.

This is a very convoluted way of going about it.

Esphome is not really optimized for low power. You might get better results modifying some battery powered door/window sensor. Just remove the reed switch and wire it to your button.

Ok, thanks all, so I have learn’t a heap here :slight_smile: Will continue playing with different pins to see if it fixes the issue but in the end will probably go with Karosm’s suggestion of modifying an existing sensor.

Gpio0 and 1 should be both fine on C3.
Remove the sleep duration.
Increase the run duration.
Make sure you have external pulldown.