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.
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
[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.
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:
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?
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 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.