Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate TIMERS into Deepsleep #20117

Merged
merged 2 commits into from Nov 28, 2023
Merged

Conversation

stefanbode
Copy link
Contributor

@stefanbode stefanbode commented Nov 28, 2023

Added new functionality to control the Deepsleep Wakeup process with TIMERS.
Timers must be type of "RULE"
"rule1 Wakeup" and ARMED and REPEAT will be used
Timer can use: TIME and SUNRISE and SUNSET with offset
Current +- variation is not supported

If above is NOT set the old behavior with "deepsleeptime xx" is still supported to wakeup on a regular base.

Description:

Related issue (if applicable): fixes #

Checklist:

  • The pull request is done against the latest development branch
  • Only relevant files were touched
  • Only one feature/fix was added per PR and the code change compiles without warnings
  • The code change is tested and works with Tasmota core ESP8266 V.2.7.4.9
  • The code change is tested and works with Tasmota core ESP32 V.2.0.14
  • I accept the CLA.

NOTE: The code change must pass CI tests. Your PR cannot be merged unless tests pass

Added new functionality to controll the Deepsleep Wakeup process with TIMERS.
@arendst arendst merged commit d0f47ef into arendst:development Nov 28, 2023
64 checks passed
@Jason2866
Copy link
Collaborator

Jason2866 commented Nov 28, 2023

@stefanbode
Copy link
Contributor Author

Thanks will check tomorrow morning. Should skip if rules and script in not enabled. Thanks for pointing

@smhc
Copy link
Contributor

smhc commented Nov 28, 2023

Hey @stefanbode thanks for this, it looks promising.
I have a couple questions/thoughts;

  1. How does it "go to sleep" when wake timers are enabled? Does it just happen the second the rule is enabled?
  2. Is there a way to defer the resume to sleep after wake? Perhaps requiring an explicit "DeepSleepResume" might be useful to control exactly when to sleep until the next wake. You need a rule to handle the wake anyway, so adding a DeepSleepResume in there doesn't seem to onerous.
  3. Is the "Wakeup" a special parameter to Rule? It might benefit from some new documentation under the "Rule" doco.

I have a bit of feedback on the DeepSleep doco, but it's probably best to just submit a separate PR for review.

@stefanbode
Copy link
Contributor Author

  1. You have to set currently deepsleeptime to 60 or something valid. Then you have 60 seconds.
  2. If the device start not with a deepsleep wakeup you have 60 seconds. Countdown in the logs. Disable timers will make it
  3. Yes and only captured by deepsleep. Documentation will need a change

@smhc
Copy link
Contributor

smhc commented Nov 29, 2023

  1. That is perhaps a bit confusing as "DeepSleepTime" will instruct when to sleep in this mode, but when to wakeup in the other mode.
  2. I believe a common use case would be to sleep until a certain time each day then remain awake until ready to sleep again. e.g a lamp that goes into deep sleep during the day but is awake all night or something. (Or in my case a cat flap that wakes twice a day to perform an operation then wants to sleep immediately after the action is done).

How would that work?

  1. Set a rule timer for sunset
  2. On wake - how to keep awake? Do both a DeepSleepTime 0 and turn off the timer?
  3. At sunset - how to sleep immediately? Re-enable the timer and do a "DeepSleepTime 1" ? (hopefully the "1" doesn't impact the next wake).

Do you think a mandatory "Restart 9" or "DeepSleepResume" command that actually initiates the sleep might be a reasonable solution to all of the above.?

@smhc
Copy link
Contributor

smhc commented Nov 29, 2023

Although I think a wake from "Restart 9" seems most intuitive, another option is using a timer to sleep. i.e "Rule1 Sleep" and "Rule2 Wakeup". This would allow you to schedule when to be asleep vs wake. I'm not sure about the implementation difficulty of this however.

@stefanbode
Copy link
Contributor Author

Regarding 1 I agree. This is not optimal right now. The main problem is there is no change in Timers and therefore a change here I cannot catch. There must be a restart or the deepsleeptime to get notification this is changed. I will check today how to optimize.

My answer on 2 was wrong. You want to keep the device awake. Easiest way is to create a rule on system#start rule1 0. Maybe it is an idea to use the ONCE flag on rules. This is currently unused. In this case the rule Wakeup got disabled once go to deepsleep. To go to sleep on sunset currently a rule1 1;restart 1 will do the job. Please be aware minimum code addition is required. This is not a pc. Introducing all this Wakeup, Sleep, depsleepresume is an overkill. We have to be smarter.

@smhc
Copy link
Contributor

smhc commented Nov 29, 2023

Yeah I agree a 'sleep timer' UI is probably overkill. It was just one possible solution to the problem of when to sleep. I don't think an automatic sleep immediately after wake is ideal when using a wake timer. So either an explicit sleep or scheduled sleep could solve that.

Writing a bit of code is fine - if anything I was proposing mandating writing some code to explicitly sleep instead of it being automatic. I think it's more intuitive to have the wake timer only be concerned with wakes, rather than it also implying an automatic sleep that you need to disable on boot.

@stefanbode
Copy link
Contributor Author

stefanbode commented Nov 29, 2023

@smhc : There is a new push on the way. As the initial build was to integrate the timers in general the new push is to optimize the work and automation with deepsleep.

What changed:
as soon as there is a working configuration for deepsleep the 60 second timer starts. No need anymore for a reboot or a deepsleep whatever. To prevent a deepsleep best is to disable all timers in the UI until ready and then switch it on.
the rule1 now also support the status 5 (ONCE). This does mean if you set rule1 5 the deepsleep will take the next possible event and go to deepsleep. After wakeup it will stay alive. E.g. with this you are easily able to wakeup the device daily at sunrise.

What do you need to send the device back to deepsleep?
I would go with a rule2 and an event.
ON Time#Minute=%sunset% DO rule1 5 ENDON

this will trigger the 60 second countdown before the device go to sleep.

@stefanbode stefanbode deleted the patch-16 branch November 30, 2023 08:24
@smhc
Copy link
Contributor

smhc commented Nov 30, 2023

Hmm ok, so essentially turning on the special rule is how to turn on the sleep schedule and initiate first sleep.

Iterating the rules on "Restart 9" for one that is marked "Wakeup" still seems a bit more intuitive for the stay awake after wake scenario. But if "rule1 5" does actually go to sleep immediately (or close to it) then that is still workable for the "stay awake" scenario.

I'll give it a try over the next few days and see how it goes.

@stefanbode
Copy link
Contributor Author

The problem of restart 9 is that it defines no sleep time. As far as I can see. And there is no deep sleep with a proper time. Just wake up through external pin in not easy.

Anyhow your proposal also have some smartness and make sense.

@pkkrusty
Copy link
Contributor

Looks like this new feature is actively being tweaked, so documentation probably doesn't exist yet. Is there anywhere laid out exactly how you envision it working? Is the goal to set a (for example) 12 hour timer, then, with deepsleeptime set to 60, the unit will deepsleep for 60 second intervals (like current implementation) until the timer runs out, then it will remain awake?

@smhc
Copy link
Contributor

smhc commented Dec 1, 2023

The problem of restart 9 is that it defines no sleep time.

What I meant by this is that the he current code for enabling the Rule["Wakeup"] could be moved to the callback for "Restart 9" (or Restart n). This would require an explicit "Restart x" to go to sleep instead of it being automatic on rule enablement.

e.g

OnRestart9() {
  if rule1 == Wakeup && rule1 enabled {
     foreach timer {
        if timer == rule1 {
          rtc.wakeup = calcwakeup(timer)
        }
     }
  }
  deepsleep()
}

You could define a new "Restart 10" instead, and it could return an error if there is no respective rule/timer if you wanted some safeguards.

@stefanbode
Copy link
Contributor Author

Documentation already updated. The timers are a replacement for the Intervall to get more control. E.g. wake-up every morning on sunrise and goto sleep again.

Intervall is still working, but you cannot mix.

@stefanbode
Copy link
Contributor Author

@smhc : the idea is nice but there is no option to select the rule that gets executed on a timer. I thought it is different but it isn't. Always all rules get called on the event. The 1,2,3,4... is only valid for the relays

@smhc
Copy link
Contributor

smhc commented Dec 2, 2023

@stefanbode

The point was to use the restart9 command to initiate the sleep.
It'd be the same code as this:

for (uint32_t i = 0; i < MAX_TIMERS; i++) {

(which just uses the minimum across all timers

secondsToTarget = tmin(secondsToTarget, secondsToTargetTemp);
)

but executed immediately by the Restart 9 command callback instead of using a countdown initiated in the deep sleep drivers TelePeriod callback.

i.e

CmdRestart9() {
   if Rule1=wakeup {
     DeepSleepCalculate()
   }
   if RtcSettings.nextwakeup > 0 {
     DeepSleepStart()
   } else {
     deepsleep()
   }
}

It would allow you to have the timer and rule enabled without a countdown sending it off to sleep - you'd have more logical control over when it sleeps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants