Scheduler
Anything that depends on a time is handled by the Scheduler. The general idea is that the scheduler will wait do an async wait until the next scheduled event, and if anything happens
Scheduler Entry Structure
Each entry in the schedule (self.AD.sched.schedule) contains the following fields:
Field |
Type |
Description |
|---|---|---|
|
|
The name of the scheduled callback (usually the method name) |
|
|
Unique identifier for this scheduled entry |
|
|
The callback function to be executed, wrapped with bound method |
|
|
The next scheduled execution time (timezone-aware) |
|
|
Time interval between repeated executions (0 for one-time callbacks) |
|
|
The base time used for calculating subsequent executions |
|
|
Whether this callback should repeat after execution |
|
|
Time offset applied to the base time for scheduling |
|
|
Start of random time window (if using random scheduling) |
|
|
End of random time window (if using random scheduling) |
|
|
Type of schedule (e.g., ‘next_rising’, ‘interval’, ‘cron’) |
|
|
Whether to pin this callback to a specific app thread |
|
|
The thread number to pin this callback to (0 for main thread) |
|
|
Additional keyword arguments to pass to the callback |
Reference
- class appdaemon.scheduler.Scheduler(ad: AppDaemon)
AppDaemon subsystem to manage internal scheduling, calculate the times of sun-based events, and parse datetime strings.
- async get_next_period(interval: TimeDeltaLike, start: time | datetime | str | None = None) datetime
Calculate the next execution time for a periodic interval.
If start is “immediate”, returns the current time. Otherwise, calculates a start time (defaulting to “now”) and advances by the interval until a future time is reached.
- async get_now(tz: str | BaseTzInfo | None = None) datetime
Get the current time for the scheduler.
The time represented will be the same regardless of the timestamp. The timezone only influences how the time is displayed.
- Parameters:
tz – The timezone to use for the current time. If None, uses the timezone configured in appdaemon.yaml.
- async loop()
Core scheduler loop, which processes scheduled callbacks and sleeping between them.
- make_naive(dt: datetime) datetime
Convert a timezone-aware datetime to a naive datetime in local timezone.
This is used for display purposes only. The scheduler internally works with timezone-aware UTC datetimes.
- async parse_datetime(input_: str | time | datetime, aware: bool = False, today: bool | None = None, days_offset: int = 0, *, now: datetime | None = None) datetime
Parse a variety of inputs into a datetime object.
- Parameters:
input (str | time | datetime) – The input to parse. Can be a string, time, or datetime object.
aware (bool, optional) – If False, the resulting datetime will be naive (without timezone). Defaults to True.
today (bool, optional) – If True, forces the result to have the same date as the now datetime. False is effectively equivalent to next. The default value is None, which doesn’t try to coerce the output at all. This results in slightly different date results for different input types. For example, a time string will be given the same date as the one in the now datetime, but a sun event string will be the datetime of the next one.
days_offset (int, optional) – Number of days to offset from the current date for sunrise/sunset parsing. If this is negative, this will unset the today argument, which allows the result to be in the past.
now (datetime, optional) – The current time to use as reference. If not provided, will call get_now().
- async restart_timer(uuid_: str, args: dict[str, Any]) dict
Used to restart a timer. This directly modifies the internal schedule dict.
- start() None
Starts the scheduler, which creates the the async task for
loop()and adds some cleanup callbacks using the Python-nativeadd_done_callback().