Page 1 of 1

Dynamic SleepMode

Posted: 13 Mar 2018, 10:50
by Domosapiens
I like to measure the sun energy with a SI1145 on a regular interval.
It will be a solar powered battery set-up.
I prefer a 1 minute sleep cycle for the next measurement during day-time.
During night hours this 1 minute cycle is useless and a waste of battery energy.

The SleepMode has a static sleep time at this moment.
I understand the limitation of the sleep counter: maximum 4229 seconds.

2 Questions:
- Is it technically possible to adapt ESPEasy, to define/override the SleepTime in a rule?
- Are more people interested in such feature?

Re: Dynamic SleepMode

Posted: 13 Mar 2018, 17:13
by arion_p
You can achieve the same result using rules and the deepSleep command. deepSleep puts the system to sleep immediately and takes one argument the duration of the sleep. So depending on the conditions you choose you can set different duration. You can use the System#Sleep event to take over control and start sleep yourself using the duration you want. E.g if you want to increase sleep time between 22:00 and 05:00

Code: Select all

on System#Sleep do
  if %systime% > 22:00:00 or %systime% < 05:00:00
    deepSleep,300 //5 minutes
  endif
  // otherwise let the system use the default sleep duration
endon
Of course night time changes throughout the year. So you may want to have some sensor to check if it is day or night. Then instead of using time, use the sensor measurement to determine which sleep duration to apply

Re: Dynamic SleepMode

Posted: 13 Mar 2018, 17:56
by Domosapiens
Top!
I will give it a try.
Thanks

Re: Dynamic SleepMode

Posted: 13 Mar 2018, 20:23
by Elektrofreak
Hi there,

Is it also possible to execute the deep sleep command with the FHEM Plugin? Which modifications would be needed to be able to do so?

Re: Dynamic SleepMode

Posted: 16 Mar 2018, 23:17
by Domosapiens
Thanks arion_p.
I finaly solved my NTP time problem: a local server was needed for a reliable NTP at start-up.

Now I use this Rule:

Code: Select all

On System#Boot do
OLEDCMD,on
endon

on System#Sleep do
OLEDCMD,off
  if %systime% > 19:00:00 or %systime% < 05:00:00
   deepSleep,3600 //60 minutes
  endif
  // otherwise let the system use the default sleep duration
endon

I used the portable syslog server mentioned here, and see this: it looks like the SystemSleep Rule is executed 3 times:

Code: Select all

<7>MUC16_LOAD EspEasy: EVENT: Clock#Time=Fri,20:55		192.168.1.86	16/03 20:55:55.477	
<7>MUC16_LOAD EspEasy: EVENT: System#Sleep			192.168.1.86	16/03 20:55:55.483	
<7>MUC16_LOAD EspEasy: ACT  : OLEDCMD,off			192.168.1.86	16/03 20:55:55.492	
<7>MUC16_LOAD EspEasy: ACT  : deepSleep,3600			192.168.1.86	16/03 20:55:55.503	
<7>MUC16_LOAD EspEasy: EVENT: System#Sleep			192.168.1.86	16/03 20:55:55.513	
<7>MUC16_LOAD EspEasy: ACT  : OLEDCMD,off			192.168.1.86	16/03 20:55:55.523	
<7>MUC16_LOAD EspEasy: ACT  : deepSleep,3600			192.168.1.86	16/03 20:55:55.541	
<7>MUC16_LOAD EspEasy: EVENT: System#Sleep			192.168.1.86	16/03 20:55:55.549	
<7>MUC16_LOAD EspEasy: ACT  : OLEDCMD,off			192.168.1.86	16/03 20:55:55.558	
<7>MUC16_LOAD EspEasy: ACT  : deepSleep,3600			192.168.1.86	16/03 20:55:55.569	
<7>MUC16_LOAD EspEasy: EVENT: Time#Initialized			192.168.1.86	16/03 21:53:29.498	
<7>MUC16_LOAD EspEasy: Current Time Zone: STD time start: 2018-10-28 03:00:00 offset: 60 min	192.168.1.86	16/03 21:53:29.507
I have also seen that it was executed 2 times (when the OR Rule is false).

Is there any explanation for this?

The second remark is that 3600 seconds is not executed as an hour, but as 57 minutes and 34 seconds.
So by using deepSleep 3810, it will wake just after an hour and just after 05:00 instead of close to 06:00.

Pre-compiled GIT version: v2.0-20180312 on Wemos D1

Re: Dynamic SleepMode

Posted: 16 Mar 2018, 23:27
by grovkillen
I think it could be that the new AND/OR operators introduced a triple check/trigger since it will do up to three values. Not sure, only a hunch.

Re: Dynamic SleepMode

Posted: 16 Mar 2018, 23:41
by Domosapiens
Yeh ..but ..
then you would expect that only in the IF .. OR section
but the OLEDCMD is executed also 3 times :o

Re: Dynamic SleepMode

Posted: 16 Mar 2018, 23:44
by grovkillen
It's part of that IF chunk. Because it's triggered and the action is then carried out. Thus them commands will be showing up three times.

EDIT: not part of the IF.... Sorry. But still I think it's the reason for the three times trigger.

Re: Dynamic SleepMode

Posted: 16 Mar 2018, 23:50
by Domosapiens
Too much logging creates sleepless nights ;)

Re: Dynamic SleepMode

Posted: 16 Mar 2018, 23:53
by grovkillen
The more you know, the more you don't understand. And ignorance is a bliss.

Good quotes 8-)

Re: Dynamic SleepMode

Posted: 17 Mar 2018, 01:37
by budman1758
This is a perfect use case for external RTC support like the DS3231. :ugeek: :ugeek:

Re: Dynamic SleepMode

Posted: 17 Mar 2018, 19:23
by arion_p
Domosapiens wrote: 16 Mar 2018, 23:17 I used the portable syslog server mentioned here, and see this: it looks like the SystemSleep Rule is executed 3 times:
....

I have also seen that it was executed 2 times (when the OR Rule is false).

Is there any explanation for this?
I can see why it is executed twice (but I cannot imagine why it would be executed a third time).

In ESPEasy.ino, in the main loop when sleep is enabled it runs all tasks at once (instead of at scheduled times) then if rules are enabled, it triggers the System#sleep event. Later at bottom of the loop if it's time to sleep it calls deepSleep() which calls startDeepSleep() which triggers the event again.

The first event trigger is superfluous. However the second one is also problematic. If it's time to sleep and startDeepSleep is called, the event is triggered. Now if you have a rule that runs the deepSleep command, this ends up calling startDeepSleep again triggering the event recursively. This should end up in an endless loop and finally to stack overflow. But according to your logs it does not.

I need to investigate further, however the first event trigger should definately be removed.

Edit: is there an issue on github for this?