Need Help setting up deepsleep.prevent

Hello Everyone
I am trying to prevent deepsleep from a sensor.
But when I publish the code on hassio for preventing the deepsleep while the sensor is sleeping, it doesn’t get the message when it wakes up. It only works when I publish it while it is awake.

Any idea how to get it to update the message on wakeup?

this is the code

esphome:
  name: niveau_cuve
  platform: ESP8266
  board: d1_mini_pro


wifi:
  ssid: "mySSID"
  password: "mypass"
  manual_ip:
    static_ip: 192.168.XX.XX (hidden on purpose)
    gateway: 192.168.XX.XX (hidden on purpose)
    subnet: 255.255.255.0


captive_portal:

# Enable logging
logger:

# Enable Home Assistant API

ota:

mqtt:
  broker: 192.168.68.150
  username: Myuser
  password: MyPass
  discovery: true
  discovery_retain: true
  birth_message:
    topic: niveau_cuve/topic
    payload: online
  will_message:
    topic: niveau_cuve/topic
    payload: offline
  on_message:
    - topic: niveau_cuve/ota_mode
      payload: 'ON'
      then:
        - deep_sleep.prevent: deep_sleep_1
    - topic: niveau_cuve/ota_mode
      payload: 'OFF'
      then:
        - deep_sleep.enter: deep_sleep_1
sensor:
  - platform: ultrasonic
    trigger_pin: D1
    echo_pin: D2
    name: "Ultrasonic Sensor"
    update_interval: 10s
    filters:
      - filter_out: nan
  
switch:
  - platform: restart
    name: "niveau_cuve"

deep_sleep:
  id: deep_sleep_1
  run_duration: 1min
  sleep_duration: 10h`

Dont use the api. Use mqtt and retain the sensor message.

I am not using the Api, it isn’t in the code.
I understand you suggestion to retain the mqtt message but how to tell the esphome to retain?

Apologies, I mistook ota: for api.

You haven’t shown your home assistant mqtt publish code. But that’s where you set retain: true.

Hi and thank you.
You where right the retain is in home assistant.

This is my config for anyone who gets the same issues

esphome

esphome:
  name: niveau_cuve
  platform: ESP8266
  board: d1_mini_pro


wifi:
  ssid: "mySSID"
  password: "mypass"
  manual_ip:
    static_ip: 192.168.XX.XX (hidden on purpose)
    gateway: 192.168.XX.XX (hidden on purpose)
    subnet: 255.255.255.0


captive_portal:

# Enable logging
logger:


ota:

mqtt:
  broker: 192.168.68.150
  username: Myuser
  password: MyPass
  discovery: true
  discovery_retain: true
  birth_message:
    topic: niveau_cuve/topic
    payload: online
  will_message:
    topic: niveau_cuve/topic
    payload: offline
  on_message:
    - topic: niveau_cuve/ota_mode
      payload: 'ON'
      then:
        - deep_sleep.prevent: deep_sleep_1
    - topic: niveau_cuve/ota_mode_enterdeepsleep
      payload: 'ON'
      then:
        - deep_sleep.enter: deep_sleep_1

sensor:
  - platform: ultrasonic
    trigger_pin: D1
    echo_pin: D2
    name: "Ultrasonic Sensor"
    update_interval: 10s
    filters:
      - filter_out: nan
  
switch:
  - platform: restart
    name: "niveau_cuve"

deep_sleep:
  id: deep_sleep_1
  run_duration: 1min
  sleep_duration: 10h`

Home Assistant

Automation:

- alias:  Cuve_deepsleep_1_off
  initial_state: true
  trigger:
      - entity_id: input_boolean.cuve_deepsleep_1
        from: 'on'
        to: 'off'
        platform: state
  action:
    - service: mqtt.publish
      data:
          topic: niveau_cuve/ota_mode
          payload: 'ON'
          retain: true
          
- alias:  Cuve_deepsleep_1_on
  initial_state: true
  trigger:
      - entity_id: input_boolean.cuve_deepsleep_1
        from: 'off'
        to: 'on'
        platform: state
  action:
# this will remove the deepsleep.prevent message retained
    - service: mqtt.publish
      data:
          topic: niveau_cuve/ota_mode
          payload: 'OFF'
          retain: true

# this will send message to start deepsleep
    - service: mqtt.publish
      data:
          topic: niveau_cuve/ota_mode_enterdeepsleep
          payload: 'ON'
# this will remove the message so on wakeup the sensor doesn't immediately go in deepsleep again
    - service: mqtt.publish
      data:
          topic: niveau_cuve/ota_mode_enterdeepsleep
          payload: 'OFF'



Boolean:

cuve_deepsleep_1:
    name: cuve_deepsleep_1
    initial: on
    icon: mdi:gauge-low

Would you please post the sensor code too? Thank you!

This is my solution with one topic

// ESPHome code
....
....
mqtt:
  broker: 192.168.1.xx
  username: xxxx
  password: xxxxx
  discovery: true
  discovery_retain: true

  on_message:
    - topic: mailbox_notifier/ota_mode
      payload: 'ON'
      then:
        - logger.log: 'OTA Mode ON - Deep sleep DISABLED'
        - deep_sleep.prevent: deep_sleep_1
    - topic: mailbox_notifier/ota_mode
      payload: 'OFF'
      then:
        - logger.log: 'OTA Mode OFF - Deep sleep ENABLED'
        - deep_sleep.enter: deep_sleep_1

Automations code:

alias: Mailbox OTA Mode ON
description: ''
trigger:
  - platform: state
    entity_id: input_boolean.mailbox_deepsleep
    from: 'off'
    to: 'on'
condition: []
action:
  - service: mqtt.publish
    data:
      topic: mailbox_notifier/ota_mode
      payload: 'ON'
      retain: true
mode: single

alias: Mailbox OTA Mode OFF
description: ''
trigger:
  - platform: state
    entity_id: input_boolean.mailbox_deepsleep
    from: 'on'
    to: 'off'
condition: []
action:
  - service: mqtt.publish
    data:
      topic: mailbox_notifier/ota_mode
      payload: 'OFF'
# change the retained payload message to avoid deep sleep
  - service: mqtt.publish
    data:
      topic: mailbox_notifier/ota_mode
      payload: PASS
      retain: true
mode: single

Helpers Toogle:

mailbox_deepsleep:
    name: Mailbox OTA Mode
    initial: off
3 Likes

Thank you this helped me a lot, I spent several hours trying to figure the deep sleep out and your example did the trick :slight_smile:

this is how I did it,

  • Look for the on_boot settings “Priority” in ESPhome
  • first on_boot check of the inpoot_boolean “weerstation_wakker_blijven” from within HA is set or not (weerstation wakker blijven is dutch for weatherstation stay awake)
  • If the input boolean “weerstation_wakker_blijven” is swiched back to the original off state then the ESP needs to be reset bij the restart switch.
type or paste code here
```on_boot:
    priority: -200
    then:
      - delay: 35s
      - logger.log: "controleer diepe slaap"
      - lambda: |-          
          if(id(weerstation_wakker_blijven).state) {
            ESP_LOGD("main", "!!!! WAKKER BLIJVEN !!! voorkom diepe slaap"); //dutch for stay awake
            id(deep_sleep_1).prevent_deep_sleep();
          } else {
            ESP_LOGD("main", "alles ok !!!!! SLAAP ZACHT !!!!"); // dutch for Ok, sleep well
          }
      - logger.log: "diepe slaap gecontroleerd"  // dutch for deep sleep checked

switch: // switch to restart the ESP so the proces of checking deep sleep restart on_boot
  - platform: restart
    name: "Herstart weerstation"
    icon: "mdi:restart"

deep_sleep:
  run_duration: 50sec  ## needs some time to calculate measurements
  sleep_duration: 20min

  id: deep_sleep_1

- platform: homeassistant   #input boolean made in HA, the ESP checks it state on_boot
    name: "wakker blijven"
    internal: true
    id: weerstation_wakker_blijven
    entity_id: input_boolean.swicth_weerstation_wakker_blijven

    on_press:
      then:
        - logger.log: "press afstand wakker houden"
        - deep_sleep.prevent: deep_sleep_1

    on_state:
      then:
        - delay: 4s
        - logger.log: "switch weerstation wakker blijven getriggerd vanuit HA"

> Blockquote

Hello ilfuriano,
I understand you can switch the ESPHome to listen for ota at next boot by HA automation, which can happen in next hours. But how do you then actually push ota to ESPHome? Do you somehow trigger it at same time when the automation script sends “disable next sleep” mqtt message to ESPHome, or you need to wait until ESPHome is online and then push it manually with command line? Thanks, Jan

Hi janbenes,

with this toggle button on the dashboard, you can set the OTA Mode (enabled/disabled) whenever you want

mailbox_deepsleep:
    name: Mailbox OTA Mode
    initial: off

the “retained” config lets the ESPHome receives the right command on the next boot.