[time] Use set_interval for CronTrigger instead of loop()

This commit is contained in:
J. Nick Koston
2026-04-03 18:10:17 -10:00
parent 2337767c38
commit 89ff3491b6
3 changed files with 15 additions and 2 deletions
+6 -1
View File
@@ -20,7 +20,12 @@ bool CronTrigger::matches(const ESPTime &time) {
return time.is_valid() && this->seconds_[time.second] && this->minutes_[time.minute] && this->hours_[time.hour] &&
this->days_of_month_[time.day_of_month] && this->months_[time.month] && this->days_of_week_[time.day_of_week];
}
void CronTrigger::loop() {
void CronTrigger::setup() {
// Cron resolution is 1 second — check once per second instead of every loop iteration
this->set_interval(1000, [this]() { this->check_time_(); });
}
void CronTrigger::check_time_() {
ESPTime time = this->rtc_->now();
if (!time.is_valid())
return;
+2 -1
View File
@@ -26,10 +26,11 @@ class CronTrigger : public Trigger<>, public Component {
void add_day_of_week(uint8_t day_of_week);
void add_days_of_week(const std::vector<uint8_t> &days_of_week);
bool matches(const ESPTime &time);
void loop() override;
void setup() override;
float get_setup_priority() const override;
protected:
void check_time_();
std::bitset<61> seconds_;
std::bitset<60> minutes_;
std::bitset<24> hours_;
+7
View File
@@ -6,5 +6,12 @@ api:
time:
- platform: homeassistant
on_time:
- at: "16:00:00"
then:
- logger.log: It's 16:00
- cron: "0 30 * * * *"
then:
- logger.log: Every hour at 30 minutes
- platform: sntp
id: sntp_time