-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Apply most recent time-controlled preset on boot #5297
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
base: main
Are you sure you want to change the base?
Apply most recent time-controlled preset on boot #5297
Conversation
WalkthroughAdds a persisted "apply scheduled preset on boot" flag with UI and config I/O; centralizes timer minute-of-day computation; refactors timer checks to use it; and applies the most-recent applicable timer preset after the first NTP sync via a new applyBootTimerPreset() routine. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes 🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
I use WLED bulbs that are controlled by a wall switch, so they power cycle frequently. I wanted them to automatically switch to red light after 8pm to reduce melatonin suppression before sleep. The existing time-controlled presets only trigger at the exact scheduled minute, not when powering on after that time has passed. This change adds a one-time check after NTP sync that finds and applies the most recent preset that should have triggered today. Changes: - Add getTimerMinuteOfDay() helper to reduce duplication between checkTimers() and the new boot-time logic - Add applyBootTimerPreset() called after first NTP sync - Add "Apply scheduled preset on boot" checkbox in Time settings UI - Setting is enabled by default and persisted in config Addresses wled#2546
5084308 to
32bed66
Compare
@kylephillipsau please also check if this comment by the rabbit makes sense. |
Match checkTimers() behavior where all matching presets are applied in order and the last one sticks. Changed > to >= and added comment explaining the design choice.
|
That makes sense, thanks for letting me know. I have updated |
|
seems like a good addition to have. It should not be enabled by default for backwards compatibility though. |
|
That's a fair point, thank you. I just changed the default value to false in b24e7db. |
Update comment from "if time changed" to "NTP sync succeeded" for better clarity about when this code executes.
|
Hey @softhack007, Thank you for bringing that up. I traced through the codebase to verify the behavior.
Since only the NTP packet receipt triggers this path, and the guard flag handles re-syncs, the implementation should behave correctly. I updated the comment to read "NTP sync succeeded" which I think captures the intent more clearly. |
softhack007
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me - no more questions ;-) I did not find time to test the code on hardware, though.
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
@kylephillipsau on last thing - for your next PR: please avoid Edit: we can live with the "damage" that was done by accident in this PR, as I think its good for merging. Just for the future please remember "force-push considered harmful" 😉 |
|
What about playlists specifically crafted to apply certain presets at defined times after a trigger time? Those would be broken. I.e. playlist to apply preset/effect A at 7:00 with duration TA then preset/effect B with duration TB, ending with preset C. One may not want this sequence to play at 8:00, only preset C would be wanted. |
|
Yeah, that's a really good point. As you noted in #2546, playlist content only exists in the Since this feature is disabled by default, users with time-sequenced playlists can leave it off. For the simpler use cases this targets (e.g. warm white after sunset and red at night), it works as expected. If this behavior is acceptable, I can add a note in the UI description to clarify it. Otherwise if it feels incomplete, I'm happy to defer until a better way to address this can be identified. |
Why not? My comment was made 3 years ago when I was still fresh with WLED. Playlists are actually simple to parse. Each entry has duration assigned (most often but there may be exceptions) so you sum durations until time is greater than current time. The only drawback I see is that if a user has boot preset assigned, it will start, only to be replaced a few seconds later by "last timed preset". The "few seconds later" may be very variable as you never know how long will it take to connect to WiFi and receive NTP response. A bit awkward and solvable by removing boot preset, but still. The reality is that user may not always want last timed preset to play (emphasis on always). I.e. I may not want to play last timed preset in the morning but would want to in the afternoon.
Let's assume users want the behaviour but have mixed presets and playlists scheduled. Life is not simple, unfortunately. There are of course other arguments "pro et contra" so the choice to enable this behaviour should definitely be in the hands of enduser. In general I am in favour of this PR but would really like to see most aspects to be considered and implemented correctly if possible. It would also be beneficial if this PR be combined with #5140 . |
Adds an option to apply the most recent scheduled preset when the device boots after NTP sync.
Currently, timed presets only trigger at the exact scheduled minute. If you power on at 9pm with an 8pm preset, nothing happens until 8pm the next day. This checks what preset should be active and applies it.
New checkbox in Time & Macros: "Apply scheduled preset on boot" (enabled by default).
Also refactored checkTimers() to share validation logic with the new code.
My use case: WLED bulbs on a wall switch that I wanted to go red after 8pm to reduce melatonin suppression before sleep.
Addresses #2546
Summary by CodeRabbit
New Features
Improvements
✏️ Tip: You can customize this high-level summary in your review settings.