Esp8266 preventing deep_sleep through script

Hi All,

I have an esp8226, working through esphome with HA. I found an article on how to use the API to prevent it from going into deep sleep, depending on a helper toggle. (🔋 ESPHome: Batteries, Deep Sleep, and Over-the-Air Updates – Tatham Oddie)

I have followed the instructions and tried other things i found here and on other sites, but I cannot get it to work.

If i toggle the switch i can see in the log that the board is receiving the update, but it does not respond to it. No doubt user error, but I am not seeing what i did wrong. Basically the script runs 15 seconds, checks a state and if it is on, it will run another 15 seconds.

If it is off, it will go into deep_sleep for 30 seconds (thats just a trial time, the real time should be 10 minutes when I get it working) …rince and repeat.

My code in the espbord’s yaml is:

  id: deep_sleep_control
  sleep_duration: 30s
  
binary_sensor:
  - platform: homeassistant
    id: prevent_deep_sleep
    entity_id: input_boolean.prevent_deep_sleep

script:
  - id: consider_deep_sleep
    mode: queued
    
    then:
      - delay: 15s
      - if:
          condition:
          
            binary_sensor.is_on: prevent_deep_sleep
             
          then:
            - deep_sleep.prevent: deep_sleep_control
          else:
            - logger.log: 'Skipping sleep, per prevent_deep_sleep'
      - script.execute: consider_deep_sleep```

The helper entity is called: input_boolean.prevent_deep_sleep


What am i missing?

PS: I know there is another way with MQTT, but I dont quite understand how that works yet, so i want to try this options first, since it is only for update and toggling a switch to put sensor in and update mode seem to make sense
binary_sensor:
  - platform: homeassistant
    id: prevent_deep_sleep
    entity_id: input_boolean.prevent_deep_sleep
    on_press:
       then:
          - deep_sleep.prevent: deep_sleep_control

Thanks lordzid, I updated the sensor with your code, but it doesnt seem to make any difference. I also removed the intergration with this board and added it again but that doesnt make any difference.

One thing that puzzles me, shouldn’t that binary sensor show up under the entities of the board?

One last thing i was wondering, the then statement now only has a logging line, shouldn’t there be a line like: deep_sleep.prevent: deep_sleep_control (Edit: No that cant be it, because the board doesnt go to sleep, no matter what the setting of the toggle is))

Edit2: I am also not seeing “Skipping sleep, per prevent_deep_sleep” when i look at the live log of the board, I see it sending temperature and humidity and i see it when i toggle the sleep control on and off, but not that sentence. Does that mean it doesnt even execute the script?

The current code is:

deep_sleep:
  id: deep_sleep_control
  sleep_duration: 2min
  
binary_sensor:
  - platform: homeassistant
    id: prevent_deep_sleep
    entity_id: input_boolean.prevent_deep_sleep
    on_press:
       then:
          - deep_sleep.prevent: deep_sleep_control

script:
  - id: consider_deep_sleep
    mode: queued
    then:
      - delay: 15s
      - if:
          condition:
            binary_sensor.is_on: prevent_deep_sleep
          then:
            - logger.log: 'Skipping sleep, per prevent_deep_sleep'
          else:
            - deep_sleep.enter: deep_sleep_control 
      - script.execute: consider_deep_sleep

Post your full code.

Below the full code

esphome:
  name: "temp-humidity-living"
  
esp8266:
  board: d1_mini

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "xxx"

ota:
  password: "xxx"

wifi:
  ssid: xxx
  password: xxx

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Temp-Humidity Fallback Hotspot"
    password: "xxx"

#mqtt
#mqtt:
#  broker: 192.168.178.25
#  username: xxx
#  password: xxx
#  topic_prefix: living-temp-mqtt
#  log_topic: ota
  
#sleep connect the  rts pin and d0 first!
deep_sleep:
  id: deep_sleep_control
  sleep_duration: 2min
  
binary_sensor:
  - platform: homeassistant
    id: prevent_deep_sleep
    entity_id: input_boolean.prevent_deep_sleep
    on_press:
       then:
          - deep_sleep.prevent: deep_sleep_control

script:
  - id: consider_deep_sleep
    mode: queued
    then:
      - delay: 15s
      - if:
          condition:
            binary_sensor.is_on: prevent_deep_sleep
          then:
            - logger.log: 'Skipping sleep, per prevent_deep_sleep'
          else:
            - deep_sleep.enter:  deep_sleep_control

      - script.execute: consider_deep_sleep

# webserver
web_server:
  port: 8088
  local: true
  include_internal: true

captive_portal:
i2c:
     sda: GPIO4
     scl: GPIO5
     scan: false

sensor:
  - platform: am2320
    setup_priority: -100
    address: 0x5C
    temperature:
      name: "Living Room Temperature"
    humidity:
      name: "Living Room Humidity"
    update_interval: 10s
 
  - platform: adc
    pin: VCC
    name: "VCC Living"
    update_interval: 60s    
1 Like

You deleted the part of the code that initializes the script

esphome:
  name: ${device_name}
  on_boot:
    then:
      - light.turn_on: status_led
      - script.execute: consider_deep_sleep
2 Likes

I think that fixed it, thank you!