ESP32 doesn't do any measures when coming back from deep sleep

Hi there,
i hope there is someone who can point me into the right direction.

Following situation:
I have programmed an ESP32 to do some basic measures for a tank level.
To run it on battery, i need to put it on deep sleep between the measurements.
I also created a prevent from sleep with MQTT.
This works good so far, i upload the code, measurements are done, i put it to deep sleep, device comes back, but doesn’t update any measurements and goes to deep sleep again.

Here is my code:

sensor:
  - platform: uptime
    name: "Uptime"

  - platform: adc
    pin: GPIO34
    name: "Spannung"
    id: voltage_sensor
    update_interval: 1s
    # accuracy_decimals: 3
    attenuation: 12db
    internal: false
    samples: 10
    filters:
      - multiply : 1000


  - platform : template
    name : "Tanklevel"
    id: id_tanklevel
    unit_of_measurement : "%"
    accuracy_decimals : 0
    update_interval : 5s
    lambda: |-
      if (id(voltage_sensor).state >= 3110){
        return 100;
      } else if (id(voltage_sensor).state <= 150) {
        return 0;
      } else {
        return (((id(voltage_sensor).state) - 150)*100) / (3110-150);
      }    

# Tank leer = xx,yy V   0,150 -> 150
# Tank voll = xx,yy V.  3,113 -> 3113

mqtt:
  broker: 192.168.1.200
  username: !secret mqtt_user
  password: !secret mqtt_password
  on_message:
    - topic: tank/prevent.sleep
      payload: 'on'
      then:
        - deep_sleep.prevent: deep_sleep_1
    - topic: tank/prevent.sleep
      payload: 'off'
      then:
        - deep_sleep.allow: deep_sleep_1
        - deep_sleep.enter: deep_sleep_1

deep_sleep:
  id: deep_sleep_1
  run_duration: 30s
  sleep_duration: 1min

this is what i get in the log:
[08:55:02][D][api:133]: Accepted 192.168.1.200
[08:55:02][D][api.connection:1570]: Home Assistant 2025.6.1 (192.168.1.200) connected
[08:55:02][D][sensor:098]: ‘Tanklevel’: Sending state 100.00000 % with 0 decimals of accuracy
[08:55:02][D][sensor:098]: ‘Spannung’: Sending state 3113.00000 V with 2 decimals of accuracy
[08:55:03][D][sensor:098]: ‘Spannung’: Sending state 3113.00000 V with 2 decimals of accuracy
[08:55:04][D][sensor:098]: ‘Spannung’: Sending state 3113.00000 V with 2 decimals of accuracy
[08:55:05][D][sensor:098]: ‘Spannung’: Sending state 3113.00000 V with 2 decimals of accuracy
[08:55:06][D][sensor:098]: ‘Spannung’: Sending state 3113.00000 V with 2 decimals of accuracy
[08:55:07][D][sensor:098]: ‘Tanklevel’: Sending state 100.00000 % with 0 decimals of accuracy
[08:55:07][D][sensor:098]: ‘Spannung’: Sending state 3113.00000 V with 2 decimals of accuracy
[08:55:08][I][deep_sleep:062]: Beginning sleep
[08:55:08][I][deep_sleep:064]: Sleeping for 60000000us
[08:55:08][D][esp32.preferences:142]: Writing 1 items: 0 cached, 1 written, 0 failed
[08:55:08][D][esp-idf:000]: I (12107) mqtt_client: Client asked to disconnect
INFO Processing expected disconnect from ESPHome API for wassertank-bug @ 192.168.1.51
WARNING Disconnected from API
INFO Successfully resolved wassertank-bug @ 192.168.1.51 in 0.000s
WARNING Can’t connect to ESPHome API for wassertank-bug @ 192.168.1.51: Error connecting to [AddrInfo(family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_STREAM: 1>, proto=6, sockaddr=IPv4Sockaddr(address=‘192.168.1.51’, port=6053))]: [Errno 113] Connect call failed (‘192.168.1.51’, 6053) (SocketAPIError)
INFO Trying to connect to wassertank-bug @ 192.168.1.51 in the background
INFO Successfully resolved wassertank-bug @ 192.168.1.51 in 0.000s
INFO Successfully resolved wassertank-bug @ 192.168.1.51 in 0.000s
INFO Successfully resolved wassertank-bug @ 192.168.1.51 in 0.000s
INFO Successfully resolved wassertank-bug @ 192.168.1.51 in 0.000s

That’s not your full code. By any chance are you running API as well as mqtt. They may not play well together.

thats the full code:

esphome:
  name: wassertank-bug
  friendly_name: wassertank-bug

esp32:
  board: esp32dev
  framework:
    type: esp-idf

# Enable logging
logger:
  level: DEBUG

# Enable Home Assistant API
api:
  encryption:
    key: "ve7YBO49e7f92gg/G32vrJJGUus8UjJjfs3eBz9NOyU="

ota:
  - platform: esphome
    password: "189c7dae3c727083b25b887f72890bed"

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Wassertank-Bug Fallback Hotspot"
    password: "yPAwyEsSFrNh"

captive_portal:

#####################################################
mqtt:
  broker: 192.168.1.200
  username: !secret mqtt_user
  password: !secret mqtt_password
  on_message:
    - topic: tank/prevent.sleep
      payload: 'on'
      then:
        - deep_sleep.prevent: deep_sleep_1
    - topic: tank/prevent.sleep
      payload: 'off'
      then:
        - deep_sleep.allow: deep_sleep_1
        - deep_sleep.enter: deep_sleep_1

deep_sleep:
  id: deep_sleep_1
  run_duration: 1min
  sleep_duration: 1min
  
sensor:
  - platform: uptime
    name: "Uptime"

  - platform: adc
    pin: GPIO34
    name: "Spannung"
    id: voltage_sensor
    update_interval: 1s
    # accuracy_decimals: 3
    attenuation: 12db
    internal: false
    samples: 10
    filters:
      - multiply : 1000


  - platform : template
    name : "Tanklevel"
    id: id_tanklevel
    unit_of_measurement : "%"
    accuracy_decimals : 0
    update_interval : 5s
    lambda: |-
      if (id(voltage_sensor).state >= 3110){
        return 100;
      } else if (id(voltage_sensor).state <= 150) {
        return 0;
      } else {
        return (((id(voltage_sensor).state) - 150)*100) / (3110-150);
      }    

# Tank leer = xx,yy V   0,150 -> 150
# Tank voll = xx,yy V.  3,113 -> 3113

Try removing api

Did, but i am getting a error message in Homeassistant

Unable to connect to the ESPHome device. Make sure the device’s YAML configuration includes an api section.

I just followed the guide in the Dokumentation how to enable and disable deep sleep mode.

Is there any way without using MQTT?

Mario

Yes, some people do use api and not mqtt.

Where are you seeing this?

Do you have an example for using api ?

Mario

The device will not be seen under esphome in integrations but under mqtt now. I would have deleted it from esphome integrations
Sorry, I see other people have api and deepsleep working in their projects but I have always used mqtt successfully with deepsleep.

The above popular thread ises api with deepsleep

This finally helped me most (method 3) and made me drop a very similar mqtt part I see in your lines of code.