@Mar1us thank you, because after your message I provided more love to the code; it seems that now it works
- The button to suspend deep sleep works
- The input to control the deep sleep duration and the delay duration are working
- The sunset condition seems ok (testing now)
essentially this is the behavior:
if the prevent deep sleep is on, the esp remains up
if the prevent deep sleep is off, it goes in deep sleep for the amount of minutes defined by the input sleep duration, then the script is delayed for the amount of time defined by the input number script delat duration. This is the normal condition, when the sun is below the horizontal then the esp goes in deep sleep until the next sunrise. (in order to calculate the sunst/sunrise duration I used the SUN2 component)
here the full code, testing it right now.
any feedback/comments is really apprecated
# Substitutions
# Define variables to be used throughout the configuration
substitutions:
device_name: test
friendly_name: "test"
device_platform: espressif32
device_board: nodemcu-32s
device_ip: X.X.X.X
# Packages
# Include common configurations from external files
packages:
wifi: !include common/device_wifi.yaml
device_base: !include common/device_base_ESP32.yaml
home_assistant_api: !include common/device_api.yaml
sensor_wifi_ip_address: !include common/sensor_wifi_ip_address.yaml
# Enable logging
logger:
level: VERBOSE
# API
api:
# Time configuration
time:
- platform: homeassistant
id: homeassistant_time
# Sun configuration
sun:
latitude: 999999
longitude: 99999
id: sun_component
on_sunset:
then:
- logger.log: "Sunset occurred, entering deep sleep mode until next sunrise"
- logger.log:
format: "Sleep duration until next sunrise: %f minutes"
args: [ "id(sleep_duration_calc).state" ]
- deep_sleep.enter:
id: deep_sleep_control
sleep_duration: !lambda "return id(sleep_duration_calc).state * 60000;"
# Deep sleep configuration
deep_sleep:
id: deep_sleep_control
binary_sensor:
# Get the state of the prevent_deep_sleep input_boolean from Home Assistant to control whether or not to enter deep sleep mode
- platform: homeassistant
id: prevent_deep_sleep
entity_id: input_boolean.prevent_deep_sleep
icon: "mdi:sleep-off"
entity_category: diagnostic
sensor:
# Get the sleep_duration value from Home Assistant to control how long to sleep for in deep sleep mode
- platform: homeassistant
id: sleep_duration
entity_id: input_number.deep_sleep_sleep_duration
# Get the delay_duration value from Home Assistant to control how long (in seconds) to wait before proceeding
- platform: homeassistant
id: delay_duration
entity_id: input_number.deep_sleep_script_delay_duration
##################
- platform: template
name: "Sleep Duration to next Sunrise"
id: sleep_duration_calc
unit_of_measurement: "min"
lambda: |-
if (id(tomorrow_sunrise_string).has_state() && id(today_sunset_string).has_state()) {
struct tm tomorrow_sunrise_tm;
strptime(id(tomorrow_sunrise_string).state.c_str(), "%Y-%m-%d %H:%M:%S", &tomorrow_sunrise_tm);
time_t tomorrow_sunrise_time = mktime(&tomorrow_sunrise_tm);
struct tm today_sunset_tm;
strptime(id(today_sunset_string).state.c_str(), "%Y-%m-%d %H:%M:%S", &today_sunset_tm);
time_t today_sunset_time = mktime(&today_sunset_tm);
double sleep_duration_seconds = difftime(tomorrow_sunrise_time, today_sunset_time);
return sleep_duration_seconds / 60.0;
}
return {};
##################
text_sensor:
- platform: homeassistant
id: today_sunrise_string
entity_id: sensor.suninfo_sunrise
attribute: today
internal: true
- platform: homeassistant
id: today_sunset_string
entity_id: sensor.suninfo_sunset
attribute: today
internal: true
- platform: homeassistant
id: tomorrow_sunrise_string
entity_id: sensor.suninfo_sunrise
attribute: tomorrow
internal: true
- platform: homeassistant
id: tomorrow_sunset_string
entity_id: sensor.suninfo_sunset
attribute: tomorrow
internal: true
- platform: template
name: "Today Sunset"
id: today_sunset
lambda: |-
if (id(today_sunset_string).has_state()) {
struct tm t;
strptime(id(today_sunset_string).state.c_str(), "%Y-%m-%d %H:%M:%S", &t);
char buf[30];
strftime(buf, sizeof(buf), "%B %d, %Y at %I:%M %p", &t);
return std::string(buf);
}
return {};
- platform: template
name: "Tomorrow Sunrise"
id: tomorrow_sunrise
lambda: |-
if (id(tomorrow_sunrise_string).has_state()) {
struct tm t;
strptime(id(tomorrow_sunrise_string).state.c_str(), "%Y-%m-%d %H:%M:%S", &t);
char buf[9];
strftime(buf, sizeof(buf), "%H:%M:%S", &t);
return std::string(buf);
}
return {};
esphome:
on_boot:
priority: -10
then:
- logger.log:
format: "Waiting for API to connect..."
# Wait until API is connected and time is synchronized before proceeding
- wait_until:
api.connected:
- logger.log:
format: "API connected. Waiting for time to synchronize..."
- wait_until:
time.has_time:
- logger.log:
format: "Time synchronized. Checking prevent_deep_sleep binary sensor value..."
# Check if the prevent_deep_sleep binary sensor value is on or off before proceeding
- if:
condition:
lambda: 'return id(prevent_deep_sleep).state;'
then:
- logger.log:
format: "prevent_deep_sleep binary sensor value is on"
else:
- logger.log:
format: "prevent_deep_sleep binary sensor value is off"
# Log that API is connected, time is synchronized, and prevent_deep_sleep binary sensor value is checked
- logger.log:
format: "API connected, time synchronized, and prevent_deep_sleep binary sensor value checked."
- wait_until:
lambda: "return id(today_sunset).has_state() && id(tomorrow_sunrise).has_state();"
- wait_until:
lambda: "return id(prevent_deep_sleep).has_state() && id(sleep_duration).has_state() && id(delay_duration).has_state();"
- script.execute: consider_deep_sleep
script:
- id: consider_deep_sleep
mode: queued
then:
- logger.log:
format: "Executing consider_deep_sleep script"
- logger.log:
format: "Delaying for %d minutes"
args: [ "(int) id(delay_duration).state" ]
- delay: !lambda "return id(delay_duration).state * 60000;"
- logger.log:
format: "After the delay"
- if:
condition:
binary_sensor.is_on: prevent_deep_sleep
then:
- logger.log:
format: "Skipping sleep, per prevent_deep_sleep"
else:
- logger.log:
format: "Entering deep sleep mode"
- if:
condition:
- sun.is_above_horizon:
id: sun_component # Sun is above horizon.
then:
- logger.log:
format: "Entering deep sleep normal"
- logger.log:
format: "Sleep for %d minutes"
args: [ "(int) id(sleep_duration).state" ]
- deep_sleep.enter:
id: deep_sleep_control
sleep_duration: !lambda "return id(sleep_duration).state * 60000;"
else:
- logger.log:
format: "Entering deep sleep until sunrise"
- logger.log:
format: "Sleep duration until next sunrise: %f minutes"
args: [ "id(sleep_duration_calc).state" ]
- deep_sleep.enter:
id: deep_sleep_control
sleep_duration: !lambda "return id(sleep_duration_calc).state * 60000;"
- script.execute: consider_deep_sleep