Actual I use this script for the deep sleep mode:
deep_sleep:
run_duration: 30s
sleep_duration: 60min
Is there a way to implement a time window (night) for a longer deep sleep phase?
Actual I use this script for the deep sleep mode:
deep_sleep:
run_duration: 30s
sleep_duration: 60min
Is there a way to implement a time window (night) for a longer deep sleep phase?
Hi
There is a way (or several) to change the deep sleep time from either a MQTT message or the state of a sensor. In my case I an Automation in Home Assistant to check values and then send a message that is retained on the MQTT server and picked up by the ESP when it wakes up.
This way I can check for instance each night if the temperatur outside is low and I want to preserve battery during the night and send the state a litte less often.
Here is an example:
substitutions:
devicename: wemos_weather_station
friendly_devicename: weather station
esphome:
name: $devicename
platform: ESP8266
board: d1_mini_pro
wifi:
fast_connect: true
ssid: !secret wifi_ssid
password: !secret wifi_password
manual_ip:
static_ip: 192.168.1.201
gateway: 192.168.1.1
subnet: 255.255.255.0
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Wemos WS Fallback Hotspot"
password: !secret ota_password
# NO API when using battery device
# api:
# password: !secret api_password
mqtt:
broker: '192.168.1.7'
username: !secret mqtt_username
password: !secret mqtt_password
discovery: false
discovery_retain: false
birth_message:
will_message:
on_message:
- topic: esp/ota_mode
payload: 'ON'
then:
- deep_sleep.prevent: deep_sleep_1
# Enable logging
logger:
level: INFO
ota:
password: !secret ota_password
sensor:
- platform: wifi_signal
name: $friendly_devicename signal strength
- platform: mqtt_subscribe
name: "Sleep time topic"
id: custom_sleep_time
unit_of_measurement: ms
accuracy_decimals: 0
topic: esp/sleep_mode_time
on_value:
then:
- lambda: |-
id(deep_sleep_1).set_sleep_duration(id(custom_sleep_time).state);
switch:
- platform: restart
id: reboot_esp
name: $friendly_devicename restart switch
deep_sleep:
id: deep_sleep_1
sleep_duration: 300s
run_duration: 1s
It’s the mqtt_subscribe sensor that does all the “work”.
Another way to do it would be to have two different message topic for like a winter setting and a summer setting. In my case 60s deep sleep during summer and 180s deep sleep during winter (when my small solar panel can’t charge the batteries so much).
mqtt:
broker: '192.168.1.7'
username: !secret mqtt_username
password: !secret mqtt_password
discovery: false
discovery_retain: false
birth_message:
will_message:
on_message:
- topic: esp/ota_mode
payload: 'ON'
then:
- deep_sleep.prevent: deep_sleep_1
- topic: esp/sleep_mode_summer
payload: 'ON'
then:
- lambda: |-
id(deep_sleep_1).set_sleep_duration(60000);
- topic: esp/sleep_mode_winter
payload: 'ON'
then:
- lambda: |-
id(deep_sleep_1).set_sleep_duration(180000);
Remember to keep a default deep sleep duration in both examples, that must be there in case there are no messages from the MQTT server and the code will default to something like sleep duration = infinity if it is not set I believe.
deep_sleep:
id: deep_sleep_1
sleep_duration: 300s
run_duration: 1s
Also, forgot about the third way. I don’t use it my self but I have tested this in my Test ESP32 with a temperature sensor attached and it works fine.
This way the ESP can change it’s deep sleep durations without intervention from another server, I am sure there are other ways to do it as well. Unfortunately this is not documented well in the Deep Sleep component on the ESPHome website.
sensor:
- platform: dallas
address: 0x5500000970DEB928
name: "Test temp1"
id: some_sensor_id
on_value:
then:
- if:
condition:
lambda: 'return id(some_sensor_id).state < 5;'
then:
- lambda: |-
id(deep_sleep_1).set_sleep_duration(180000);
else:
- lambda: |-
id(deep_sleep_1).set_sleep_duration(60000);
Hej Lars
Cool. Gonna look into that.
I got I sorta working but I still have trouble with battery life. How long can u get your demos to last on a single 18650 battery?
Hmm not sure exactly, I have a combination of small solar cells and old laptop batteries and never run out of charge with three very old 18650 cells and a small 6W solar panel.
Another small project gets around 8 months of battery life out of four also very old 18650 cells soldered together. This one I charge around two times a year (I seldom let it got below 3.7V) with a 10000 mhA USB-battery/charger.
It also depends on which ESP you use, Andreas Spiess (The Guy with the Swizz accent ) has done great work on figuring out which ESP8266-boards and ESP32 boards that use the least energy. He also has other great videos on how to measure the voltage without drawing to much power doing it (I have not implemented that on all my projects).
Here are som links:
I am trying to also maximise my battery power. Is it possible to have two separate deep sleep cycles?
eg:
deep_sleep:
id: deep_sleep_1
sleep_duration: 300s
run_duration: 1s
id: deep_sleep_2 (night time sleep)
sleep_duration: 300m
Not sure how to trigger each without one interfering with the other.
Pat
I just do it like this and works fine for me:
deep_sleep:
id: deep_sleep_1
run_duration: ${auto_wake_time}
sleep_duration: ${sleep_time}
time:
- platform: homeassistant
id: esptime
on_time:
#Deep sleep at 8pm and wake up at 5am.
- hours: 20
then:
#Publish battery level to the end of day sensor since it will be our last update.
- lambda: return id(end_of_day_battery_percent).publish_state(id(batt_level).state);
#Reset culmulative light sesnor reset
- sensor.integration.reset: irrigation_cul_lux_hours
- delay: 1s
#Rest up
- deep_sleep.enter:
id: deep_sleep_1
until: "05:00:00"
time_id: esptime
Mahko
If I am reading this correctly your device goes into deep sleep from 8pm to 5am. During the day it is always operating?
Pat
Yes. Sleeps all night.
During the day it wakes every hour.
It’s stays awake until sensor data updates are done or auto wake time is up (in case something isn’t working properly).
More detail over here…
Andreas Spiess is the best!
Mahko,
Having read through your yaml a few times I think I have the gist of it. I won’t get to play with it for a couple of weeks. Thanks for your help. I agree Andreas is a legend.
Mahko
does the below yaml seem OK?
Would be kind enough to let me know if I have inserted the code for time in the right place?
name: "main-tank"
platform: esp32
board: nodemcu-32s
# Send power to sensor by enabling relay on boot
on_boot:
then:
- output.turn_on: water_sensor_power
- delay: 5s
- script.execute: consider_deep_sleep
# prevent deep sleep
binary_sensor:
- platform: homeassistant
id: prevent_deep_sleep
name: Prevent Deep Sleep
entity_id: input_boolean.prevent_deep_sleep
time:
- platform: homeassistant
id: esptime
on_time:
#Deep sleep at 5pm and wake up at 7am.
- hours: 17
then:
- delay: 1s
#Rest up
- deep_sleep.enter:
id: deep_sleep_control
until: "07:00:00"
time_id: esptime
script:
- id: consider_deep_sleep
mode: queued
then:
- delay: 60s
- if:
condition:
binary_sensor.is_on: prevent_deep_sleep
then:
- deep_sleep.prevent: deep_sleep_control
- logger.log: 'Skipping sleep, per prevent_deep_sleep'
else:
- deep_sleep.enter: deep_sleep_control
- script.execute: consider_deep_sleep
# Enable logging
logger:
# Deep sleep
deep_sleep:
id: deep_sleep_control
run_duration: 1min
sleep_duration: 60min
output:
- platform: gpio
pin: 19
id: water_sensor_power
inverted: false
i2c:
sda: GPIO21
scl: GPIO22
scan: true
id: bus_a
ads1115:
- address: 0x48
i2c_id: bus_a
sensor:
- name: maintank water level
platform: ads1115
multiplexer: 'A0_GND'
update_interval: 6s
gain: 4.096
unit_of_measurement: "V"
icon: "mdi:gauge"
accuracy_decimals: 3
filters:
- median:
window_size: 23
send_every: 21
send_first_at: 7
- name: battery voltage
platform: ads1115
multiplexer: 'A1_GND'
gain: 4.096
unit_of_measurement: "V"
accuracy_decimals: 3
# Enable Home Assistant API
api:
ota:
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
fast_connect: true
manual_ip:
static_ip: 192.168.0.178
gateway: 192.168.0.1
subnet: 255.255.255.0
captive_portal: ```
Pat
Sorry, on holiday and mostly offline. Won’t get to look at this properly for a week or so.
It looks ok from a quick look.
Mahko
I haven’t tried this yet by going live. I’m just trying to get my head around the time command. I’m trying for the life of me to work out where the code says for the esp to do nothing from 1700 to 0700 and then enter the set deep sleep cycle.
Pat
This bit says “at 5pm, sleep until 7am”.
Then when it wakes at 7am, this sleep schedule should kick in…
Mahko
I’ll give that a go. Thanks for that
Pat
Mahko
mate that works a treat so far. Two days in and the esp just dissapears overnight and then boom - here I am again. I wish I had discovered this earlier - what a boon for anyone using water level sensors on solar power.
I do have one question. I note the code has in there esptime. Should I use time homeassistant?
The reason I ask is that even though I set a time off at 1700 and then time on at 0700 I have HA telling me that my device is connecting back up at 6.30am?
Is there some weird daylight savings calculation here? (yes we are still one of those nations that turn clocks in different directions according to seasons)
Pat
Not sure about that.
Does your sensor.time look ok in HA? You might need to configure it in HA.
Or you might need to configure your ESPHome time.
I’m on daylight savings in Melbourne and never noticed a issue.
You can probably get your wake time down from 1min to around 10-20sec if you manually request updates on wake then sleep when they are done. It requires a bit more code.
It’s in my previous example.
Mahko
have just tried by inputting homeassistant_time instead of esptime in code sections. I’ll monitor that and see how it goes.
I have the sensor giving readings for 2 min for a bit more accuracy. It is a water level monitor that will eventually use values to turn on/off a pump for our main house water supply.
Pat
I’m using mine with variable sensors too (water level in fact). On a solar powered battery project.
I sample 5 times at ~1 sec intervals , then take the median and push a single value to HA. Then sleep. Updates take about 10sec.