-
I think I understand how the DeepSleepTime works now, but it was quite difficult to decipher as the document suggests there is a max sleep time of 86400 and that value wakes at 00:00, somewhat implying that you can only sleep until midnight as a maximum. But this documentation is incorrect as you can specify up to 10 years of sleep time. Also, depending on what you specify and when you perform the sleep - specifying a smaller sleep time can actually result in waking later. It also hints that there's no way to 'shift' the wake up time and the difficulties of targeting a particular time - but really that is referring to the next wake up time in a recurring set. If you don't care about the recurring period you can certainly target a given first wake up time if you happen on just the right value. You can always ignore the recurring period and recalculate the next wake up time yourself. This is combined with the fact the documentation doesn't mention the 5% fudge factor, which I believe is a bit buggy/excessive as 86400 actually wakes at 01:12 the next day. I think this is quite excessive buffer amount. I've listed below some relatively random sleep times and the local time that they were performed. Something like this might be useful in the doco.
It would be great if Tasmota had a "DeepSleepFor" or "DeepSleepUntil" command that slept for x seconds or slept until a given local time. But anyway, I will work with this current command which is really more like a "DeepSleepSchedule". My question is whether anyone has a formula for calculating this "first wake time" given a current time and deep sleep time? Or even better, calculate the largest deep sleep time given a current time and target wake time? I would like to specify a target wake time for just a once off wake - I can then recalculate the next deep sleep amount as required for the next wake (e.g in berry). I understand it is periods of deep sleep time from the epoch up until it exceeds the current time, then 1 more deep sleep time unit after that. But I cannot think of a way to calculate this. I believe calculating the ideal deep sleep time might be finding the largest factor of 'floor(timestamp(target_time) + x mins)'. The reason I want to use the largest value is to avoid any interim wakes between now and the target time. I can round the target time up/down 10 minutes or so if that makes finding a suitable factor easier. Any other ideas? |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 17 replies
-
I thought that taking the target timestamp and dividing by 8.4 (getting a factor within the valid range, then reducing by the 5%) would give you an appropriate "deep sleep time". But that doesn't seem to work. I believe the formula is more or less: waketime = ((curtime / dsleep) + 1) * dsleep) + (dsleep * 0.05) based on : https://github.com/arendst/Tasmota/blob/development/tasmota/tasmota_xdrv_driver/xdrv_29_deepsleep.ino#L117 but I am not maths enough to figure it out :( dsleep = (waketime - curtime) / 1.05 but it's not quite right for me. |
Beta Was this translation helpful? Give feedback.
-
deepsleep 16200 is a bad choice because this in epoch seconds is every day a different time. Anyhow the time between the wakeups is more or less exactly 16200 seconds. You get the current epoch time here: https://www.epochconverter.com/ e.g. now: 1700206164 --> 1700206164/16200 = 104.950 full cycle -> wakeup at 104.951 * 16200 timestamp Your time zone: Freitag, 17. November 2023 08:28:38 [GMT+01:00] -> wakeup : Freitag, 17. November 2023 08:30:00 [GMT+01:00] |
Beta Was this translation helpful? Give feedback.
-
16200 is a bad choice for waking 2 or more times, but as far as waking once all I care about is the first wake time. After that I will cancel the sleep schedule and issue a new deep sleep command with a new value. I think your calculation is more or less what I was performing (except I was also taking into account the 5% bump)- but I was not getting the same values as "DeepSleepTime x" was reporting. |
Beta Was this translation helpful? Give feedback.
-
EDIT: ChatGPT is a terrible math tool. Using a excel I get the correct value = (( ( floor ((1700224097+ 5) / 16200)) + 1) * 16200) + (16200* 0.05) |
Beta Was this translation helpful? Give feedback.
-
Here is a spreadsheet that takes a given 'current time' and shows the 'first wake time' values for various values of 'deep sleep time'. Ideally I need a way to do the reverse - calculate the deep sleep time given a desired wake time. As you can see, unless I am mistaken, things get pretty weird and unpredictable with values above 20,000 or so. |
Beta Was this translation helpful? Give feedback.
-
This is a bit of berry code that will sleep until a specific time: import math
import string
def sleep_until(target)
var curtime = tasmota.rtc()["local"]
var ds = 60
for i: 1 .. 10000
var wake = (((curtime / ds) + 1) * ds ) + (ds * 0.05)
if (math.abs(wake - target) < 600)
# print('sleep time: ', ds, i)
tasmota.cmd(string.format("DeepSleepTime %d", ds))
return i
end
if (i % 1000 == 0)
tasmota.yield()
end
ds += 60
end
print('not found..')
end
# Sleep until 12 hours after the current time
sleep_until(tasmota.rtc()["local"] + (3600 * 12)) It's not that efficient, and would need some tuning of the search iteration depending on how long you needed to sleep for. But it works ok for < 24hr sleeps. The sleep time chosen is unlikely to be suitable for the "next" period. You will need to have a rule that immediately disables DeepSleepTime on init, or immediately sets it to the new desired value. If Tasmota ever changes the sleep time algo it will likely break. Would be nice to have native support for this functionality in Tasmota, but alas this is ok for now. |
Beta Was this translation helpful? Give feedback.
-
Christmas is comming!! @smhc Added a new pull request to use TIMERS for wakeup from deepsleep. The new add on calculates the next wakeup based on the TIMERS. You can use nearly all TIMERS functionality beside the RANDOM change. Pull request: #20117 How does it work (must be rule1 ther rules 2,3 do not work): Define TIMERS with action RULE New functionality: On power restart and reset Deepsleepcountdown is 60seconds. after deepsleep wakeup 4 seconds |
Beta Was this translation helpful? Give feedback.
-
Documentation updated: https://tasmota.github.io/docs/DeepSleep/#deepsleep-wakeup-based-on-timer-events |
Beta Was this translation helpful? Give feedback.
This is a bit of berry code that will sleep until a specific time:
It's not that efficient, and would need…