Oh, I assumed it should sleep during night.
You have to change on_sunset
to on_sunrise
and set until
to a time in the evening.
Oh, I assumed it should sleep during night.
You have to change on_sunset
to on_sunrise
and set until
to a time in the evening.
Yup cool ok I did that, so now I guess I just wait and see if it sleeps at 7am.
Another thing I noticed is it does not like 24hr time entries.
Is this because it gets its time from HA?
Also this is not possible on the ESP01 correct?
Well, for this first run, it will stay awake till on_sunrise
is triggered. Because it just gets to sleep when sun rises.
For wakeups during the day with immediately sleeping after reboot, you would have to include something like
on_boot:
- if:
condition:
- sun.is_above_horizon:
then:
- deep_sleep.enter:
id: deep_sleep_1
until: "19:00:00"
time_id: ha_time
Maybe. The examples use 24h times. Does your HA but run with 24h times?
Im not sure where to place this, it keeps telling me its an invalid option for (sun, deep_sleep etc) no matter where I place it.
on_boot:
- if:
condition:
- sun.is_above_horizon:
then:
- deep_sleep.enter:
id: deep_sleep_1
until: "07:00"
time_id: ha_time
on_boot
belongs below esphome
esphome:
#...
on_boot:
#...
BTW: unfortunately no chance to dynamically set sunrise time. The error is: This option is not templatable!
Thats cool, no worries ill just stick to the wake up time then
Thank you!
Do I add this on boot in addition to the other? Like this
esphome:
name: rf-transmitter
friendly_name: rf-transmitter
on_boot:
- if:
condition:
- sun.is_above_horizon:
then:
- deep_sleep.enter:
id: deep_sleep_1
until: "19:00:00"
time_id: ha_time
esp32:
board: esp32dev
framework:
type: arduino
# Enable logging
logger:
# Enable Home Assistant API
api:
encryption:
key: "0TtebCpgbi1peARMnjqtfdurcRUN9VRFDXhH0XVSOZA="
ota:
password: "177422200ac36dd305a952791921eed0"
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
captive_portal:
time:
- platform: homeassistant
id: ha_time
sun:
# your location
latitude: -36.848461
longitude: 174.763336
on_sunrise:
then:
- deep_sleep.enter:
id: deep_sleep_1
until: "18:00:00"
time_id: ha_time
deep_sleep:
id: deep_sleep_1
Thanks
With the above code, the device seems to have gone into sleep mode as soon as flashed, this is correct.
However, I set it to wake up an hour from now, which it has failed to do.
Any ideas?
I do not know esp32. But esp8266 needs D0 and RST to be connected to wake up from deep sleep. I use jumpers for this case, since you cannot flash via serial when this connection is active. Maybe esp32 also needs some circuit.
Another possiblity could be the time source. Maybe you should insert conditions to ensure HA api is connected and the time source, which is necessary for sun
and until
, is set.
on_boot:
priority: 200
then:
- logger.log: Waiting for API connection...
- wait_until:
condition:
api.connected:
timeout: 10s
- logger.log: Waiting for time...
- wait_until:
condition:
time.has_time:
timeout: 10s
- if:
condition:
- sun.is_above_horizon:
then:
- deep_sleep.enter:
id: deep_sleep_1
until: "19:00:00"
time_id: ha_time
Thanks for this,
Can anyone confirm for me if I need to jump between D0 and RST on the ESP32?
Thanks
I still cannot get this working.
Ok im stlll stuck with this.
here is the behaviour im experiencing. After boot, it sucesfully goes straight into sleep mode if the sun is below horizon, this is correct. Then I set the until time that I want it to wake up with, and it wakes up but appears to go straight back to sleep again.
It comes online, then shortly after it goes offline again.
Is it possible that the on_boot part is running in a loop?
Beginning to go a little mad.
esphome:
name: rf-transmitter
friendly_name: rf-transmitter
on_boot:
priority: 200
then:
- logger.log: Waiting for API connection...
- wait_until:
condition:
api.connected:
timeout: 10s
- logger.log: Waiting for time...
- wait_until:
condition:
time.has_time:
timeout: 10s
- if:
condition:
- sun.is_below_horizon:
then:
- deep_sleep.enter:
id: deep_sleep_1
until: "23:00:00"
time_id: ha_time
esp32:
board: esp32dev
framework:
type: arduino
# Enable logging
logger:
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
captive_portal:
time:
- platform: homeassistant
id: ha_time
sun:
# your location
latitude: -36.848461
longitude: 174.763336
deep_sleep:
id: deep_sleep_1
Wait, dumbass me figured it out.
Itâs actually obeying the condition each time it boots. When it boots, the sun is still below horizon so its going back to sleep.
Its waking at the time specified, in my case 23:00:00, for testing which is 11 at night, where the sun is below_horizon because thatâs what happens at nightâŚthen it sees that the sun is below_horizon it decides it needs more sleep.
I will do a full cycle test and see if its awake when I awake
This is working pretty well now.
Have been testing for a few days! All seems good.
What I would like to do is change the window of sleep. Is it possible to instead of using on_sunrise to enter deepsleep, I can just use a time.
Like
at 02:00 enter deepsleep
until 19:00
Thanks
Something like, Does this look correct?
Thanks
deep_sleep:
id: deep_sleep_1
time:
- platform: homeassistant
id: esptime
on_time:
- hours: 02
then:
- deep_sleep.enter:
id: deep_sleep_1
until: "19:00:00"
time_id: esptime
Hi,
I canât make it work, my goal is:
5:00 AM Wake-up
deepsleep every 30min till sunset
Sunset sleep until 5:00AM
everything works, but after it sleep at sunset it doesnât wake up at 5:00AM
any ideas?
substitutions:
sleep_time: 30min
auto_wake_time: 30s
esphome:
on_boot:
then:
- logger.log: Waiting for API connection...
- wait_until:
condition:
api.connected:
timeout: 10s
- logger.log: Waiting for time...
- wait_until:
condition:
time.has_time:
timeout: 10s
- if:
condition:
- sun.is_below_horizon:
then:
- deep_sleep.enter:
id: deep_sleep_control
until: "05:00:00"
time_id: ha_time
- script.execute: consider_deep_sleep
# Enable logging
logger:
level: VERBOSE
sun:
# your location
latitude: XXXXX
longitude: VVVV
on_sunset:
then:
- deep_sleep.enter:
id: deep_sleep_control
until: "05:00:00"
time_id: ha_time
time:
- platform: homeassistant
id: ha_time
deep_sleep:
id: deep_sleep_control
sleep_duration: ${sleep_time}
#run_duration: ${auto_wake_time}
# 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
Hi, I started another experiment and I changed a little bit the logic.
# Define substitutions
substitutions:
device_name: test
friendly_name: "test"
device_platform: espressif32
device_board: nodemcu-32s
device_ip: 192.168.1.XXX
# Include common packages
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 with VERBOSE level
logger:
level: VERBOSE
# Enable Home Assistant API
api:
# Define binary sensor to prevent deep sleep
binary_sensor:
- platform: homeassistant
id: prevent_deep_sleep
entity_id: input_boolean.prevent_deep_sleep
# Define binary sensor to trigger deep sleep from Home Assistant button press
- platform: homeassistant
id: deep_sleep_button
entity_id: input_boolean.deep_sleep_button # updated entity ID to match Home Assistant input_boolean entity ID
on_press:
then:
- logger.log:
format: "Deep sleep button pressed"
- lambda: |-
ESP_LOGD("main", "Entering deep sleep for %f minutes", id(deep_sleep_duration).state);
- deep_sleep.enter:
id: deep_sleep_control
# Define sensor to get deep sleep duration from Home Assistant
sensor:
- platform: homeassistant
id: deep_sleep_duration
entity_id: input_number.deep_sleep_duration # change this to the entity ID of the input_number in Home Assistant
- platform: homeassistant
id: deep_sleep_delay_duration
entity_id: input_number.deep_sleep_delay_duration # change this to the entity ID of the input_number in Home Assistant
# Define time from Home Assistant
time:
- platform: homeassistant
id: homeassistant_time
on_time:
# Deep sleep based on deep_sleep_duration sensor value unless input_boolean.prevent_deep_sleep is ON.
- seconds: [0]
then:
- logger.log: "Checking conditions for deep sleep"
- if:
condition:
binary_sensor.is_on: prevent_deep_sleep # input_boolean.prevent_deep_sleep is on.
then:
- logger.log:
format: "prevent_deep_sleep binary sensor state: %s"
args: [ 'id(prevent_deep_sleep).state ? "ON" : "OFF"' ]
- logger.log: "Conditions not met for deep sleep, prevent deep sleep is on"
else:
- if:
condition:
sun.is_below_horizon:
id: sun_component # Sun is below horizon.
then:
- logger.log:
format: "prevent_deep_sleep binary sensor state: %s"
args: [ 'id(prevent_deep_sleep).state ? "ON" : "OFF"' ]
- logger.log:
format: "Sun elevation: %f"
args: [ 'id(sun_component).elevation()' ]
- logger.log: "Conditions met for deep sleep during the night"
- logger.log:
format: "Entering deep sleep until sunrise"
- logger.log:
format: "Sleep duration: %d seconds"
args: [ 'id(sun_component).sunrise(0).value().timestamp - id(homeassistant_time).now().timestamp' ]
- deep_sleep.enter:
id: deep_sleep_control
sleep_duration: !lambda 'return id(sun_component).sunrise(0).value().timestamp - id(homeassistant_time).now().timestamp;'
else:
- logger.log:
format: "prevent_deep_sleep binary sensor state: %s"
args: [ 'id(prevent_deep_sleep).state ? "ON" : "OFF"' ]
- logger.log:
format: "Sun elevation: %f"
args: [ 'id(sun_component).elevation()' ]
- logger.log: "Conditions met for deep sleep during the day"
- lambda: |-
ESP_LOGD("main", "Entering deep sleep for %f minutes", id(deep_sleep_duration).state);
- deep_sleep.enter:
id: deep_sleep_control
sleep_duration: !lambda 'return id(deep_sleep_duration).state * 60;'
#- delay: !lambda 'return id(deep_sleep_duration).state * 60 * 1000;'
- logger.log:
format: "Deep sleep delay duration: %f minutes"
args: [ 'id(deep_sleep_delay_duration).state' ]
- logger.log:
format: "Delay duration: %f milliseconds"
args: [ 'id(deep_sleep_delay_duration).state * 60 * 1000' ]
- delay: !lambda 'return id(deep_sleep_delay_duration).state * 60 * 1000;'
# Define sun location for sunrise/sunset calculations
sun:
id: sun_component
latitude: 34.325577
longitude: 4.315322
on_sunset:
# Deep sleep at sunset until sunrise unless input_boolean.prevent_deep_sleep is ON.
then:
- if:
condition:
binary_sensor.is_off: prevent_deep_sleep # input_boolean.prevent_deep_sleep is OFF.
then:
- logger.log:
format: "Entering deep sleep until sunrise"
- logger.log:
format: "Sleep duration: %d seconds"
args: [ 'id(sun_component).sunrise(0).value().timestamp - id(homeassistant_time).now().timestamp' ]
- deep_sleep.enter:
id: deep_sleep_control
sleep_duration: !lambda 'return id(sun_component).sunrise(0).value().timestamp - id(homeassistant_time).now().timestamp;'
# Define sunrise and sunset text sensors from ESPHome sun component
text_sensor:
- platform: sun
name: Sun Next Sunrise
type: sunrise
- platform: sun
name: Sun Next Sunset
type: sunset
# ESPHome configuration
esphome:
# Wait for API connection, time synchronization, and prevent_deep_sleep binary sensor value on boot.
on_boot:
priority: -10 # higher priority than default (0)
then:
- wait_until:
api.connected:
- wait_until:
time.has_time:
- wait_until:
lambda: 'return id(prevent_deep_sleep).state;'
- wait_until:
lambda: 'return id(deep_sleep_duration).state;'
- wait_until:
lambda: 'return id(deep_sleep_delay_duration).state;'
- logger.log:
format: "API connected, time synchronized, and prevent_deep_sleep binary sensor value received."
deep_sleep:
id: deep_sleep_control
this should be the expected behavior:
- Every minute (
on_time
withseconds: [0]
), the device checks if it should enter deep sleep.- If the
prevent_deep_sleep
binary sensor is on (which corresponds to theinput_boolean.prevent_deep_sleep
entity in Home Assistant), then the device will not enter deep sleep and will log that the conditions for deep sleep were not met.- If the
prevent_deep_sleep
binary sensor is off and the sun is below the horizon (as determined by thesun_component
), then the device will enter deep sleep until sunrise. The duration of deep sleep is calculated as the time difference between the next sunrise and the current time.- If the
prevent_deep_sleep
binary sensor is off and the sun is above the horizon, then the device will enter deep sleep for a duration specified by thedeep_sleep_duration
sensor (which corresponds to theinput_number.deep_sleep_duration
entity in Home Assistant).The expected behavior is that the device will enter deep sleep according to these conditions and will wake up either at sunrise or after the specified duration has elapsed
the log shows that Iâm entering in the correct branch after sunset, but the device wake up quickly instead to remain in deep sleep until sunrise
[23:42:37][C][deep_sleep:049]: Setting up Deep Sleep...
[23:43:00][D][main:305]: Checking conditions for deep sleep
[23:43:00][D][main:192]: prevent_deep_sleep binary sensor state: OFF
[23:43:00][D][main:195]: Sun elevation: -27.446308
[23:43:00][D][main:198]: Conditions met for deep sleep during the night
[23:43:00][D][main:201]: Entering deep sleep until sunrise
[23:43:00][D][main:204]: Sleep duration: 24846 seconds
[23:43:00][I][deep_sleep:116]: Beginning Deep Sleep
[23:43:00][I][deep_sleep:118]: Sleeping for 24846000us
[23:43:00][D][esp32.preferences:114]: Saving 1 preferences to flash...
[23:43:00][V][esp32.preferences:126]: sync: key: 233825507, len: 4
[23:43:00][D][esp32.preferences:143]: Saving 1 preferences to flash: 0 cached, 1 written, 0 failed
INFO Processing expected disconnect from ESPHome API for 192.168.1.XXX
WARNING Disconnected from API
INFO Successfully connected to 192.168.1.XXX
[23:43:35][D][homeassistant.binary_sensor:026]: 'input_boolean.prevent_deep_sleep': Got state OFF
[23:43:35][D][binary_sensor:034]: 'prevent_deep_sleep': Sending initial state OFF
[23:43:36][D][homeassistant.binary_sensor:026]: 'input_boolean.deep_sleep_button': Got state OFF
[23:43:36][D][binary_sensor:034]: 'deep_sleep_button': Sending initial state OFF
[23:43:36][D][homeassistant.sensor:024]: 'input_number.deep_sleep_duration': Got state 15.00
[23:43:36][V][sensor:043]: 'deep_sleep_duration': Received new state 15.000000
[23:43:36][D][sensor:094]: 'deep_sleep_duration': Sending state 15.00000 with 1 decimals of accuracy
[23:43:36][D][homeassistant.sensor:024]: 'input_number.deep_sleep_delay_duration': Got state 10.00
[23:43:36][V][sensor:043]: 'deep_sleep_delay_duration': Received new state 10.000000
[23:43:36][D][sensor:094]: 'deep_sleep_delay_duration': Sending state 10.00000 with 1 decimals of accuracy
any ideas?