I’m not even sure what title to give this thread - help !
I my testing of deep_sleep, I am wanting to save the reason for the current wakeup in sensor wakeup_cause … as per the code in Deep Sleep Component — ESPHome. I am also wanting to record the number of times the ESP32 has woken up in a global variable num_wake_cycles - but I want to ensure this is only updated once per wake period … so on_boot: seems the best place to do the increment.
Being old school, and not proficient with C++, I like to add print statements (or logger.log or ESP_LOGD()) to check that the variables have the values I intend before I try to use them in other logic … but log statements in the on_boot: and on_shutdown: don’t go to the HA ESPHome log.
… and that is where I have come unstuck. Sometimes a variable appears to have one value, and in the next line it appears to have a different value. I have tried logger.log and ESP_LOGD()
I assume there is some core principle I have forgotten or don’t properly understand. Hoping that some kind soul will look past my incorrect (old fashioned) terminology and point me in the right direction.
I have cut down this yaml code to minimum - but have added debug statements to the two sensors …
#####################################################################
# #
# The ESP32-S3-DevKitC-1 (v1) board with ESP32-S3-WROOM-1-N16R8 #
# #
# CURRENT usage for debugging parts of the greenhouse code #
# in particular rain_gauge, water module and on_boot/shutdown #
#
#####################################################################
substitutions:
devicename: "esp32s3-test"
deviceIP: "97" # last octet of the IP Address
wifi_ssid: !secret upstairs_ssid ### which wi-fi to connect to
wifi_password: !secret upstairs_password
update_interval_network: "30 min" # How often to measure and report network values
update_interval_sensor: "3 min" # How often to measure and report sensor values
# deep_sleep
sleep_awake_duration: '600000' # =10 min # how long to run (be awake)
sleep_asleep_duration: '300000' # =5 min # how long to sleep for
sleep_low_batt_duration: '10800000' # =3 hours
esp32:
board: esp32-s3-devkitc-1 # devkit from AliExpress
variant: esp32s3
flash_size: 16MB
framework:
type: esp-idf
version: recommended
psram:
mode: octal
speed: 80MHz
esphome:
name: $devicename
platformio_options:
board_build.flash_mode: dio
on_boot:
then:
- sensor.template.publish:
id: wakeup_cause
state: !lambda 'return esp_sleep_get_wakeup_cause();'
- lambda: |-
ESP_LOGD("on_boot", "");
ESP_LOGD("on-boot", "#################### ON_BOOT ####################");
ESP_LOGD("on_boot", ">>>>>> on_boot: initial values wakeup_cause=%d, wakeup_cause.state=%d, num_wake_cycles=%d", id(wakeup_cause), id(wakeup_cause).state, id(num_wake_cycles) );
// id(wakeup_cause) = esp_sleep_get_wakeup_cause();
id(num_wake_cycles) += 1;
- logger.log: "##### on_boot finished"
deep_sleep:
id: deep_sleep_1
run_duration: ${sleep_awake_duration} ms # how long to run (be awake)
sleep_duration: ${sleep_asleep_duration}ms # how long to sleep for
packages:
wi-fi: !include _common_wifi.yaml
logger:
level: DEBUG
i2c:
- id: bus_a
sda: GPIO5
scl: GPIO4
scan: true
frequency: 400kHz
globals:
- id: num_wake_cycles
type: int
restore_value: yes
initial_value: '0'
sensor:
- platform: template
id: wakeup_cause
name: $devicename Wakeup cause
update_interval: $update_interval_sensor
accuracy_decimals: 0
lambda: return esp_sleep_get_wakeup_cause();
on_value:
then:
- if:
condition:
lambda: 'return id(wakeup_cause).state > 0;'
then:
lambda: |-
ESP_LOGD("testing", " >>>>>> WOKEN UP wakeup_cause=%d, esp_sleep_get_wakeup_cause()=%d, num_wake_cycles=%d", id(wakeup_cause), esp_sleep_get_wakeup_cause(), id(num_wake_cycles) );
- logger.log:
format: ">>>>>> WOKEN UP logger esp_sleep_get_wakeup_cause()=%d, wakeup_cause=%d, num_wake_cycles=%d, x=%d "
args: [ 'esp_sleep_get_wakeup_cause()', 'id(wakeup_cause).state', 'id(num_wake_cycles)', 'x' ]
- lambda: |-
ESP_LOGD("testing", ">>>>>> WOKEN UP LOGD esp_sleep_get_wakeup_cause()=%d, wakeup_cause=%d, num_wake_cycles=%d, x=%d",
esp_sleep_get_wakeup_cause(), id(wakeup_cause).state, id(num_wake_cycles), x );
- platform: bh1750
name: $devicename BH1750 Illuminance
address: 0x23
update_interval: $update_interval_sensor
on_value:
then:
- logger.log:
format: ">>>>>> logger esp_sleep_get_wakeup_cause()=%d, wakeup_cause=%d, num_wake_cycles=%d "
args: [ 'esp_sleep_get_wakeup_cause()', 'id(wakeup_cause).state', 'id(num_wake_cycles)' ]
- lambda: |-
ESP_LOGD("test", ">>>>>> LOGD esp_sleep_get_wakeup_cause()=%d, wakeup_cause=%d, num_wake_cycles=%d",
esp_sleep_get_wakeup_cause(), id(wakeup_cause).state, id(num_wake_cycles) );
Results in:
[22:02:41][C][deep_sleep:026]: Setting up Deep Sleep...
[22:02:41][C][deep_sleep:029]: Sleep Duration: 300000 ms
[22:02:41][C][deep_sleep:032]: Run Duration: 600000 ms
[22:03:24][D][esp32.preferences:114]: Saving 2 preferences to flash...
[22:03:24][D][esp32.preferences:142]: Saving 2 preferences to flash: 0 cached, 2 written, 0 failed
[22:03:35][W][api.connection:135]: ESPHome Logs 2025.3.0 (192.168.1.98): Connection closed
[22:03:36][I][safe_mode:041]: Boot seems successful; resetting boot loop counter
[22:03:36][D][esp32.preferences:114]: Saving 1 preferences to flash...
[22:03:36][D][esp32.preferences:142]: Saving 1 preferences to flash: 0 cached, 1 written, 0 failed
[22:03:45][D][light:036]: 'esp32s3-test RGB LED' Setting:
[22:03:45][D][light:047]: State: ON
[22:03:45][D][light:085]: Transition length: 1.0s
[22:03:55][D][light:036]: 'esp32s3-test RGB LED' Setting:
[22:03:55][D][light:047]: State: OFF
[22:03:55][D][light:085]: Transition length: 1.0s
[22:04:08][D][bh1750.sensor:159]: 'esp32s3-test BH1750 Illuminance': Got illuminance=23.1lx
[22:04:08][D][sensor:093]: 'esp32s3-test BH1750 Illuminance': Sending state 23.09055 lx with 1 decimals of accuracy
[22:04:08][D][main:120]: >>>>>> logger esp_sleep_get_wakeup_cause()=0, wakeup_cause=3, num_wake_cycles=0
[22:04:08][D][test:141]: >>>>>> LOGD esp_sleep_get_wakeup_cause()=0, wakeup_cause=0, num_wake_cycles=0
[22:04:48][D][sensor:093]: 'esp32s3-test Wakeup cause': Sending state 0.00000 with 0 decimals of accuracy
[22:04:48][D][main:128]: >>>>>> WOKEN UP logger esp_sleep_get_wakeup_cause()=0, wakeup_cause=1070229052, num_wake_cycles=0, x=0
[22:04:48][D][testing:129]: >>>>>> WOKEN UP LOGD esp_sleep_get_wakeup_cause()=0, wakeup_cause=0, num_wake_cycles=0, x=0
[22:07:08][D][bh1750.sensor:159]: 'esp32s3-test BH1750 Illuminance': Got illuminance=23.1lx
[22:07:15][D][sensor:093]: 'esp32s3-test BH1750 Illuminance': Sending state 23.09055 lx with 1 decimals of accuracy
[22:07:15][D][main:120]: >>>>>> logger esp_sleep_get_wakeup_cause()=0, wakeup_cause=-16777216, num_wake_cycles=0
[22:07:15][D][test:141]: >>>>>> LOGD esp_sleep_get_wakeup_cause()=0, wakeup_cause=0, num_wake_cycles=0
[22:07:48][D][sensor:093]: 'esp32s3-test Wakeup cause': Sending state 0.00000 with 0 decimals of accuracy
[22:07:48][D][main:128]: >>>>>> WOKEN UP logger esp_sleep_get_wakeup_cause()=0, wakeup_cause=1070229052, num_wake_cycles=0, x=0
[22:07:48][D][testing:129]: >>>>>> WOKEN UP LOGD esp_sleep_get_wakeup_cause()=0, wakeup_cause=0, num_wake_cycles=0, x=0
[22:10:08][D][bh1750.sensor:159]: 'esp32s3-test BH1750 Illuminance': Got illuminance=23.1lx
[22:10:08][D][sensor:093]: 'esp32s3-test BH1750 Illuminance': Sending state 23.09055 lx with 1 decimals of accuracy
[22:10:08][D][main:120]: >>>>>> logger esp_sleep_get_wakeup_cause()=0, wakeup_cause=-16777216, num_wake_cycles=0
[22:10:08][D][test:141]: >>>>>> LOGD esp_sleep_get_wakeup_cause()=0, wakeup_cause=1070181668, num_wake_cycles=0
[22:10:48][D][sensor:093]: 'esp32s3-test Wakeup cause': Sending state 0.00000 with 0 decimals of accuracy
[22:10:48][D][main:128]: >>>>>> WOKEN UP logger esp_sleep_get_wakeup_cause()=0, wakeup_cause=1070229052, num_wake_cycles=0, x=0
[22:10:48][D][testing:129]: >>>>>> WOKEN UP LOGD esp_sleep_get_wakeup_cause()=0, wakeup_cause=0, num_wake_cycles=0, x=0
[22:12:39][I][deep_sleep:060]: Beginning Deep Sleep
[22:12:39][I][deep_sleep:062]: Sleeping for 300000000us
[22:12:39][D][esp32.preferences:114]: Saving 1 preferences to flash...
[22:12:39][D][esp32.preferences:142]: Saving 1 preferences to flash: 1 cached, 0 written, 0 failed
[22:12:39][D][main:061]: ##### on_shutdown -100 start
[22:12:39][D][main:064]: ##### on_shutdown -100 finished
INFO Processing expected disconnect from ESPHome API for esp32s3-test @ 192.168.1.97
WARNING Disconnected from API
WARNING Can't connect to ESPHome API for esp32s3-test @ 192.168.1.97: Error connecting to [AddrInfo(family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_STREAM: 1>, proto=6, sockaddr=IPv4Sockaddr(address='192.168.1.97', port=6053))]: [Errno 113] Connect call failed ('192.168.1.97', 6053) (SocketAPIError)
INFO Trying to connect to esp32s3-test @ 192.168.1.97 in the background
INFO Successfully connected to esp32s3-test @ 192.168.1.97 in 0.031s
INFO Successful handshake with esp32s3-test @ 192.168.1.97 in 0.186s
[22:18:32][D][esp32.preferences:114]: Saving 1 preferences to flash...
[22:18:32][D][esp32.preferences:142]: Saving 1 preferences to flash: 0 cached, 1 written, 0 failed
[22:18:36][I][safe_mode:041]: Boot seems successful; resetting boot loop counter
[22:18:36][D][esp32.preferences:114]: Saving 1 preferences to flash...
[22:18:36][D][esp32.preferences:142]: Saving 1 preferences to flash: 0 cached, 1 written, 0 failed
[22:19:34][D][sensor:093]: 'esp32s3-test Wakeup cause': Sending state 4.00000 with 0 decimals of accuracy
[22:19:34][D][testing:124]: >>>>>> WOKEN UP wakeup_cause=1070229548, esp_sleep_get_wakeup_cause()=4, num_wake_cycles=1744
[22:19:34][D][main:128]: >>>>>> WOKEN UP logger esp_sleep_get_wakeup_cause()=4, wakeup_cause=-16777216, num_wake_cycles=0, x=1074790400
[22:19:34][D][testing:129]: >>>>>> WOKEN UP LOGD esp_sleep_get_wakeup_cause()=4, wakeup_cause=0, num_wake_cycles=0, x=1074790400
[22:19:40][D][bh1750.sensor:159]: 'esp32s3-test BH1750 Illuminance': Got illuminance=21.7lx
[22:19:40][D][sensor:093]: 'esp32s3-test BH1750 Illuminance': Sending state 21.73228 lx with 1 decimals of accuracy
[22:19:40][D][main:120]: >>>>>> logger esp_sleep_get_wakeup_cause()=4, wakeup_cause=-16777216, num_wake_cycles=0
[22:19:40][D][test:141]: >>>>>> LOGD esp_sleep_get_wakeup_cause()=4, wakeup_cause=0, num_wake_cycles=0
[22:22:33][D][sensor:093]: 'esp32s3-test Wakeup cause': Sending state 4.00000 with 0 decimals of accuracy
[22:22:33][D][testing:124]: >>>>>> WOKEN UP wakeup_cause=1070229548, esp_sleep_get_wakeup_cause()=4, num_wake_cycles=1744
[22:22:33][D][main:128]: >>>>>> WOKEN UP logger esp_sleep_get_wakeup_cause()=4, wakeup_cause=-16777216, num_wake_cycles=0, x=1074790400
[22:22:33][D][testing:129]: >>>>>> WOKEN UP LOGD esp_sleep_get_wakeup_cause()=4, wakeup_cause=0, num_wake_cycles=0, x=1074790400
[22:22:36][D][bh1750.sensor:159]: 'esp32s3-test BH1750 Illuminance': Got illuminance=23.2lx
[22:22:36][D][sensor:093]: 'esp32s3-test BH1750 Illuminance': Sending state 23.20374 lx with 1 decimals of accuracy
[22:22:36][D][main:120]: >>>>>> logger esp_sleep_get_wakeup_cause()=4, wakeup_cause=-16777216, num_wake_cycles=0
[22:22:36][D][test:141]: >>>>>> LOGD esp_sleep_get_wakeup_cause()=4, wakeup_cause=0, num_wake_cycles=0
[22:25:33][D][sensor:093]: 'esp32s3-test Wakeup cause': Sending state 4.00000 with 0 decimals of accuracy
[22:25:33][D][testing:124]: >>>>>> WOKEN UP wakeup_cause=1070229548, esp_sleep_get_wakeup_cause()=4, num_wake_cycles=1744
[22:25:33][D][main:128]: >>>>>> WOKEN UP logger esp_sleep_get_wakeup_cause()=4, wakeup_cause=-16777216, num_wake_cycles=0, x=1074790400
[22:25:33][D][testing:129]: >>>>>> WOKEN UP LOGD esp_sleep_get_wakeup_cause()=4, wakeup_cause=0, num_wake_cycles=0, x=1074790400
[22:25:36][D][bh1750.sensor:159]: 'esp32s3-test BH1750 Illuminance': Got illuminance=23.2lx
[22:25:36][D][sensor:093]: 'esp32s3-test BH1750 Illuminance': Sending state 23.20374 lx with 1 decimals of accuracy
[22:25:36][D][main:120]: >>>>>> logger esp_sleep_get_wakeup_cause()=4, wakeup_cause=-16777216, num_wake_cycles=0
[22:25:36][D][test:141]: >>>>>> LOGD esp_sleep_get_wakeup_cause()=4, wakeup_cause=0, num_wake_cycles=0
[22:27:36][I][deep_sleep:060]: Beginning Deep Sleep
[22:27:36][I][deep_sleep:062]: Sleeping for 300000000us
[22:27:36][D][esp32.preferences:114]: Saving 1 preferences to flash...
[22:27:36][D][esp32.preferences:142]: Saving 1 preferences to flash: 1 cached, 0 written, 0 failed
[22:27:36][D][main:061]: ##### on_shutdown -100 start
[22:27:36][D][main:064]: ##### on_shutdown -100 finished
INFO Processing expected disconnect from ESPHome API for esp32s3-test @ 192.168.1.97
WARNING Disconnected from API
The value of esp_sleep_get_wakeup_cause() is consistent at 0 in the first awake period, and 4 in the second awake period - CORRECT !
But does wakeup_cause have a value of 3, 0, 1070229052, 1070229548, or -16777216 ? I wondered if those last 3 values might be the address of the esp_sleep_get_wakeup_cause() function, so added the .state in the statements - but they still show up.
All but one of the ESP_LOGD() lines (the test:141 at 22:10:08) give id(wakeup_cause).state a value of zero, suggesting that logger.log and ESP_LOGD() treat this variable differently.
And is num_wake_cycles actually 0 or 1744 ? It is actually 1744 from my previous testing, and is being correctly incremented in on:boot: … but why showed always as 0 in the first wake period ?