Sleep example code from documentation not working?

Hi there :slight_smile:
On this page, about half way down, there is an example that shows how to make the device sleep once a certain condition is met: Deep Sleep Component — ESPHome

It looks like this:

on_...:
  then:
    - deep_sleep.enter:
        id: deep_sleep_1
        sleep_duration: 20min

When I do the same it looks like this in my code (my entire code is below):

    on_value_range:
      above: 100
      below: 100000000000
      then:
        - deep_sleep.enter:
        id: deep_sleep1
        sleep_duration: 1min

This gets me this error in the editor:
“unable to find action with the name deep_sleep.enter”

Why is this? I practically copy pasted this from the documentation…

The goal is to get the ESP to sleep once the pulse-counter output reaches a certain threshold. It’s for a greenhouse watering device.

The entire code looks like this:

esphome:
  name: vanding2

esp8266:
  board: nodemcuv2

# Enable logging
logger:

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

ota:
  password: "...some pw"

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
#  ap:
#    ssid: "Vanding2 Fallback Hotspot"
#    password: "fallback pw"

captive_portal:
    
    #kontakter start
switch:
  - platform: gpio
    pin: D3
    name: "Vanding open"
    id: open_switch
  - platform: gpio
    pin: D4
    name: "vanding close"
    id: close_switch
cover:
  - platform: template
    name: "Garage Door"
    open_action:
      # Cancel any previous action
      - switch.turn_off: close_switch
      # Turn the OPEN switch on briefly
      - switch.turn_on: open_switch
      - delay: 0.1s
      - switch.turn_off: open_switch
    close_action:
      - switch.turn_off: open_switch
      - switch.turn_on: close_switch
      - delay: 0.1s
      - switch.turn_off: close_switch
    stop_action:
      - switch.turn_off: close_switch
      - switch.turn_off: open_switch
    optimistic: true
    assumed_state: true
    
    #kontakter slut
    
sensor:
  - platform: pulse_meter
    pin: D6
    unit_of_measurement: 'Liter'
    name: 'Vanding, forbrug'
    internal_filter: 100ms
    internal_filter_mode: "PULSE"
    accuracy_decimals: 3
    filters:
      - multiply: 1.0
      
    on_value_range:
      above: 100
      below: 100000000000
      then:
        - deep_sleep.enter:
          id: deep_sleep1
          sleep_duration: 1min
      
    total:
      name: "Vanding, total"
      unit_of_measurement: "Liter"
      accuracy_decimals: 3
      filters:
        - multiply: 1.00

I haven’t used it before, but I think your esphome yaml is missing the deep_sleep component and (optional) run_duration and id definitions:

deep_sleep:
  run_duration: 10s
  id: deep_sleep1

The “deep_sleep:” block should be entered at the same (top) level as wifi, ota, captive_portal, etc.

does not look like

look at your indentation

Thanks, but I tried both prior to posting (and again just now). It doesn’t really change much. Also the error is tied to the function call (so the line just before the ones you point to.

So I messed around with it a bit more, and in case anyone wants to know (future readers perhaps) both Nickrout and gonzotek answers has to be used in combination:

So the correct way of doing it appears to be

Top of code:

deep_sleep:
   id: deep_sleep1

(no other params are used here since we want to trigger the sleep somewhere else)

When sleep is to be triggered, use the code from Nickrouts answer.

1 Like