Cant create id to check a toggle button for deep sleep in home assistant for ota update on nodemcu

i cant create id in esphome configuration to stop deep sleep in nodemcu based on a toggle button that is present on home assistant


Error

Mapping values are not allowed for " id: prevent_deep_sleep "

Based on this


esphome:
  name: livingroom_node
  platform: ESP8266
  board: nodemcuv2
  on_boot:
    then:
      – script.execute: consider_deep_sleep


wifi:
  ssid: "XXXXXX"
  password: "XXXXXX"

  ap:
    ssid: " livingroom Fallback Hotspot"
    password: "XXXXXXX"

captive_portal:



sensor:
  - platform: ultrasonic
    trigger_pin: D1
    echo_pin: D2
    name: "Water level"


logger:

# Enable Home Assistant API
api:

ota:

deep_sleep:
  id: deep_sleep_control
  sleep_duration: 30s

# Will only pick up a value if this device is configured in Home Assistant > Integrations
# If the device isn't configured, or Home Assistant is offline, it'll default to false

binary_sensor:
  – platform: homeassistant
    id: prevent_deep_sleep
    entity_id: input_boolean.prevent_deep_sleep

script:
  – id: consider_deep_sleep
    mode: queued
    then:
      – delay: 10s
      – 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

You need to remove the api: and add the mqtt: information.

mqtt:
  broker: 192.168.xxx.xxx
  username: !secret mqtt_username
  password: !secret mqtt_password
  
  birth_message:
    topic: battery_voltage/topic
    payload: online
  will_message:
    topic: battery_voltage/topic
    payload: offline
  
  on_message:
    - topic: deep_sleep/ota_mode
      payload: 'ON'
      then:
        - deep_sleep.prevent: battery_deep_sleep
  
    
    - topic: deep_sleep/ota_mode
      payload: 'OFF'
      then:
        - deep_sleep.enter: battery_deep_sleep


  

can you please share the entire code and i want to use esphome api rather then using mqtt, anything would work ! thanks

Check this post that has all the information you need.

esphome:
  name: pondsensor
  platform: ESP8266
  board: esp12e
  on_boot:
    then:
      - script.execute: consider_deep_sleep

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  fast_connect: true
  power_save_mode: light

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "pondsensor Fallback Hotspot"
    password: !secret fall

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: !secret api

ota:
  password: !secret ota

dallas:
  - pin: GPIO4
    update_interval: 2s
  
sensor:
  - platform: dht
    pin: GPIO5
    temperature:
      name: "outside_temp"
      filters:
        - lambda: return x * (9.0/5.0) + 32.0;
      unit_of_measurement: "Β°F"
    humidity:
      name: "outside_humidity"
    update_interval: 5s

  - platform: dallas
    address: 0x4001193A17927B28
    name: "Pond temperature"
    filters:
      - lambda: return x * (9.0/5.0) + 32.0;
    unit_of_measurement: "Β°F" 

  - platform: adc
    pin: A0
    id: "LIION"
    name: "A0 Voltage x 4.2"
    update_interval: 5s
    accuracy_decimals: 4
    filters:
      - sliding_window_moving_average:
          window_size: 30
          send_every: 5
      - multiply: 4.2

  - platform: template
    name: "Pond Sensor battery_level"
    unit_of_measurement: '%'
    update_interval: 5s
    accuracy_decimals: 2
    lambda: |-
      return ((id(LIION).state-3) /1.1 * 100.00);
      
  - platform: template
    name: "Pond Sensor battery_level_curve"
    unit_of_measurement: '%'
    update_interval: 5s
    accuracy_decimals: 2
    lambda: |-
      return (id(LIION).state);
    filters:
      - calibrate_polynomial:
          degree: 2
          datapoints:
          # Map 0.0 (from sensor) to 0.0 (true value)
            - 3.25 -> 0.0
            - 3.4 -> 10
            - 3.55 -> 50
            - 4.1 -> 100
            - 4.3 -> 100
    
## Deep Sleep

deep_sleep:
  id: deep_sleep_control
  sleep_duration: 5min

# Will only pick up a value if this device is configured in Home Assistant > Integrations
# If the device isn't configured, or Home Assistant is offline, it'll default to false
binary_sensor:
  - platform: homeassistant
    id: prevent_deep_sleep
    entity_id: input_boolean.prevent_deep_sleep
  - platform: homeassistant
    id: ICU
    entity_id: input_boolean.ICU

script:
  - id: consider_deep_sleep
    mode: queued
    then:
      - delay: 10s
      - if:
          condition:
            binary_sensor.is_off: ICU
          then:
            - logger.log: 'Not connected to HomeAssisstant. Please setup in HA to enable deepsleep'
          else:
            - if:
                condition:
                  binary_sensor.is_on: prevent_deep_sleep
                then:
                  - logger.log: 'Skipping sleep, per prevent_deep_sleep'
                else:
                  - logger.log: 'Feeling Sleepy'
                  - deep_sleep.enter: deep_sleep_control
      - script.execute: consider_deep_sleep

Mttq will be the end goal, but here is my stop-gap with the api. Not pretty could be better but works for me.

1 Like

Thanks for posting this solution. Your sleep function works excellent on my ESP Cam. :slight_smile: I was looking for something working like this.

1 Like

I used the config above for a device on my mailbox that runs on a single 18650 battery which is on my front yard and sometimes it takes more than a minute to connect with Home Assistant.
I think I have improved it a little bit to still enter sleep mode if it’s not able to connect with HA after a few attempts. (for example if the server is down or forgot to enable ICU)
My config sets variable sleep duration based on time which is retrieved from HA. The sleep duration is 1h if it wakes up before 7:00AM, 15min before 4:00PM and 30min after 4:00PM.
Feel free to edit/improve it for your needs.

globals:
  - id: connectAttempts
    type: int
    restore_value: no
    initial_value: '0'

deep_sleep:
  sleep_duration: 15min
  id: deep_sleep_control

binary_sensor:
  - platform: homeassistant
    id: prevent_deep_sleep
    entity_id: input_boolean.prevent_deep_sleep
  - platform: homeassistant
    id: ICU
    entity_id: input_boolean.icu

time:
  - platform: homeassistant
    id: ha_time

script:
  - id: consider_deep_sleep
    mode: queued
    then:
      - delay: 10s
      - if:
          condition:
            binary_sensor.is_off: ICU
          then:
            - logger.log: 'Not connected to HomeAssisstant. Setup ICU and enable in HA to enable deepsleep'
            - if:
              # if ICU is off or not connected to HA, try 10 times (10x10sec) to connect or else enter sleep mode
                condition:
                  - lambda: |-
                      if (id(connectAttempts) < 9) {
                        id(connectAttempts) += 1;
                        ESP_LOGD("customSleep", "Connection Attempts: %d", id(connectAttempts));
                        return false;
                      }
                      else {
                        id(connectAttempts) = 0;
                        id(deep_sleep_control).set_sleep_duration(900000);
                        ESP_LOGD("customSleep", "Connection Attempts: %d", id(connectAttempts));
                        ESP_LOGD("customSleep", "Set Sleep Duration: %d min", 900000/60000);
                        return true;
                      }
                then:
                  - logger.log: 'Entering Deep Sleep after failed attempts to connect with HA and read ICU'
                  - delay: 500ms
                  - deep_sleep.enter: deep_sleep_control
                else:
                  - logger.log: 'Try again to connect with HA'
          else:
            - if:
              # if prevent is off enter sleep: 15min if time is invalid, 1h before 7:00, 15min before 16:00, 30min after 16:00
                condition:
                  binary_sensor.is_on: prevent_deep_sleep
                then:
                  - logger.log: 'Skipping sleep. Disable "Prevent Deep Sleep" in HA to enable sleep'
                else:
                  - lambda: |-
                      auto time = id(ha_time).now();
                      if (time.is_valid()){
                        if (time.hour < 7 ){
                          id(deep_sleep_control).set_sleep_duration(3600000);
                          ESP_LOGD("customSleep", "Set Sleep Duration: %d min", 3600000/60000);
                        }
                        else if (time.hour < 16 ){
                          id(deep_sleep_control).set_sleep_duration(900000);
                          ESP_LOGD("customSleep", "Set Sleep Duration: %d min", 900000/60000);
                        }
                        else{
                          id(deep_sleep_control).set_sleep_duration(1800000);
                          ESP_LOGD("customSleep", "Set Sleep Duration: %d min", 1800000/60000);
                        }
                      }
                      else{
                        id(deep_sleep_control).set_sleep_duration(900000);
                        ESP_LOGD("customSleep", "Wrong Time - Set Sleep Duration: %d min", 900000/60000);
                      }
                  - logger.log: 'Entering Deep Sleep'
                  - delay: 500ms
                  - deep_sleep.enter: deep_sleep_control
      - script.execute: consider_deep_sleep

Screenshot 2021-08-13 at 17.09.46

1 Like