ESPHome 2026.10.0-dev
Loading...
Searching...
No Matches
automation.cpp
Go to the documentation of this file.
1#include "automation.h"
2#ifdef USE_TIME_TRIGGERS
3
4#include "esphome/core/log.h"
5
6#include <cinttypes>
7
8namespace esphome::time {
9
10static const char *const TAG = "automation";
11static const int MAX_TIMESTAMP_DRIFT = 900; // how far can the clock drift before we consider
12 // there has been a drastic time synchronization
13
14void CronTrigger::add_second(uint8_t second) { this->seconds_[second] = true; }
15void CronTrigger::add_minute(uint8_t minute) { this->minutes_[minute] = true; }
16void CronTrigger::add_hour(uint8_t hour) { this->hours_[hour] = true; }
17void CronTrigger::add_day_of_month(uint8_t day_of_month) { this->days_of_month_[day_of_month] = true; }
18void CronTrigger::add_month(uint8_t month) { this->months_[month] = true; }
19void CronTrigger::add_day_of_week(uint8_t day_of_week) { this->days_of_week_[day_of_week] = true; }
20bool CronTrigger::matches(const ESPTime &time) {
21 return time.is_valid() && this->seconds_[time.second] && this->minutes_[time.minute] && this->hours_[time.hour] &&
22 this->days_of_month_[time.day_of_month] && this->months_[time.month] && this->days_of_week_[time.day_of_week];
23}
25 // Cron resolution is 1 second — check once per second instead of every loop iteration
26 this->set_interval(1000, [this]() { this->check_time_(); });
27}
28
30 ESPTime time = this->rtc_->now();
31 if (!time.is_valid())
32 return;
33
34 if (this->last_check_.has_value()) {
35 auto &last_check = *this->last_check_;
36 if (last_check > time && last_check.timestamp - time.timestamp > MAX_TIMESTAMP_DRIFT) {
37 // We went back in time (a lot), probably caused by time synchronization
38 ESP_LOGW(TAG, "Time has jumped back!");
39 } else if (last_check >= time) {
40 // already handled this one
41 return;
42 } else if (time > last_check && time.timestamp - last_check.timestamp > MAX_TIMESTAMP_DRIFT) {
43 // We went ahead in time (a lot), probably caused by time synchronization
44 ESP_LOGW(TAG, "Time has jumped ahead!");
45 this->last_check_ = time;
46 return;
47 }
48
49 while (true) {
50 last_check.increment_second();
51 if (last_check >= time)
52 break;
53
54 if (this->matches(last_check))
55 this->trigger();
56 }
57 }
58
59 this->last_check_ = time;
60 if (!time.fields_in_range()) {
61 ESP_LOGW(TAG, "Time is out of range!");
62 ESP_LOGD(TAG, "Second=%02u Minute=%02u Hour=%02u DayOfWeek=%u DayOfMonth=%u DayOfYear=%u Month=%u time=%" PRId64,
63 time.second, time.minute, time.hour, time.day_of_week, time.day_of_month, time.day_of_year, time.month,
64 (int64_t) time.timestamp);
65 }
66
67 if (this->matches(time))
68 this->trigger();
69}
71void CronTrigger::add_seconds(const std::vector<uint8_t> &seconds) {
72 for (uint8_t it : seconds)
73 this->add_second(it);
74}
75void CronTrigger::add_minutes(const std::vector<uint8_t> &minutes) {
76 for (uint8_t it : minutes)
77 this->add_minute(it);
78}
79void CronTrigger::add_hours(const std::vector<uint8_t> &hours) {
80 for (uint8_t it : hours)
81 this->add_hour(it);
82}
83void CronTrigger::add_days_of_month(const std::vector<uint8_t> &days_of_month) {
84 for (uint8_t it : days_of_month)
85 this->add_day_of_month(it);
86}
87void CronTrigger::add_months(const std::vector<uint8_t> &months) {
88 for (uint8_t it : months)
89 this->add_month(it);
90}
91void CronTrigger::add_days_of_week(const std::vector<uint8_t> &days_of_week) {
92 for (uint8_t it : days_of_week)
93 this->add_day_of_week(it);
94}
96
98 rtc->add_on_time_sync_callback([this]() { this->trigger(); });
99}
100
101} // namespace esphome::time
102
103#endif // USE_TIME_TRIGGERS
void set_interval(const char *name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a const char* name.
Definition component.cpp:88
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Definition automation.h:461
void add_second(uint8_t second)
void add_minutes(const std::vector< uint8_t > &minutes)
void add_month(uint8_t month)
std::bitset< 8 > days_of_week_
Definition automation.h:42
void add_hour(uint8_t hour)
void add_day_of_month(uint8_t day_of_month)
void add_seconds(const std::vector< uint8_t > &seconds)
bool matches(const ESPTime &time)
void add_months(const std::vector< uint8_t > &months)
float get_setup_priority() const override
void add_days_of_month(const std::vector< uint8_t > &days_of_month)
std::bitset< 32 > days_of_month_
Definition automation.h:40
std::bitset< 61 > seconds_
Definition automation.h:37
CronTrigger(RealTimeClock *rtc)
void add_minute(uint8_t minute)
std::bitset< 24 > hours_
Definition automation.h:39
optional< ESPTime > last_check_
Definition automation.h:44
void add_days_of_week(const std::vector< uint8_t > &days_of_week)
void add_day_of_week(uint8_t day_of_week)
std::bitset< 60 > minutes_
Definition automation.h:38
std::bitset< 13 > months_
Definition automation.h:41
void add_hours(const std::vector< uint8_t > &hours)
The RealTimeClock class exposes common timekeeping functions via the device's local real-time clock.
ESPTime now()
Get the time in the currently defined timezone.
void add_on_time_sync_callback(F &&callback)
SyncTrigger(RealTimeClock *rtc)
uint8_t month
Definition date_entity.h:1
uint8_t second
uint8_t minute
uint8_t hour
constexpr float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.h:43
A more user-friendly version of struct tm from time.h.
Definition time.h:23
uint8_t minute
minutes after the hour [0-59]
Definition time.h:32
uint8_t second
seconds after the minute [0-60]
Definition time.h:30
bool fields_in_range(bool check_day_of_week=true, bool check_day_of_year=true) const
Check if time fields are in range.
Definition time.h:89
uint8_t hour
hours since midnight [0-23]
Definition time.h:34
time_t timestamp
unix epoch time (seconds since UTC Midnight January 1, 1970)
Definition time.h:48
uint16_t day_of_year
day of the year [1-366]
Definition time.h:40
bool is_valid(bool check_day_of_week=true, bool check_day_of_year=true) const
Check if this ESPTime is valid (year >= 2019 and the requested fields are in range).
Definition time.h:82
uint8_t day_of_month
day of the month [1-31]
Definition time.h:38
uint8_t month
month; january=1 [1-12]
Definition time.h:42
uint8_t day_of_week
day of the week; sunday=1 [1-7]
Definition time.h:36