cap_scheduler — Scheduling
Source: cap_scheduler.c · Header: cap_scheduler.h
Mission
Section titled “Mission”cap_scheduler is the time-trigger center for ESP-Claw. It is responsible for generating events at specified points in time and pushing them to the claw_event_router. The scheduler itself does not execute specific business logic (like sending messages or calling tools); instead, it triggers subsequent automation rules by publishing events.
This “separation of scheduling and execution” design allows scheduled tasks to reuse all action capabilities of the Event Router (run_agent, call_cap, run_script, etc.).
Core Concepts
Section titled “Core Concepts”Schedule Item
Section titled “Schedule Item”Each schedule item defines “when” to publish “what kind of event”. Key fields include:
id: Unique identifier.kind: Schedule type, supportsonce(one-time),interval(interval-based),cron(standard 5-field Cron expression).enabled: Whether it is enabled.event_type: The type of event to publish (corresponds tomatch.event_typein routing rules).event_key: The event key (corresponds tomatch.event_keyin routing rules).payload_json: Accompanying JSON data.
Data Persistence
Section titled “Data Persistence”The scheduler manages two persisted data sets:
- Definition file (
schedules.json): Stores all schedule item configurations. - State file (derived automatically as
schedules_path + ".state", e.g.schedules.json.state): Stores runtime dynamic information, such aslast_fire_ms(last triggered time) andrun_count(number of runs). This ensures that the device can correctly resume progress after a reboot without losing or repeating tasks.
After each scheduler_reload (and when loading on startup), the scheduler fills missing fields in each entry of schedules.json using default rules.
Tools Exposed to LLM
Section titled “Tools Exposed to LLM”| Tool ID | Function | Input Parameters |
|---|---|---|
scheduler_list | List all schedule items and their running status | (None) |
scheduler_get | Get details for one schedule item | id |
scheduler_add | Add a new schedule item | schedule_json (string) |
scheduler_update | Update an existing schedule item | schedule_json (string) |
scheduler_remove | Remove a schedule item | id |
scheduler_enable | Enable a specific schedule item | id |
scheduler_disable | Disable a specific schedule item | id |
scheduler_pause | Pause a specific schedule item | id |
scheduler_resume | Resume a specific schedule item | id |
scheduler_trigger_now | Trigger a schedule immediately (does not affect subsequent plans) | id |
scheduler_reload | Reload configuration from disk | (None) |
Where:
schedule_json(string) means{"schedule_json":"<JSON string>"}. Hereschedule_jsonitself is a string value (escaped), not a nested object.idmeans{"id":"..."}.
Usage Examples
Section titled “Usage Examples”1. Configure a Schedule Item
Section titled “1. Configure a Schedule Item”A typical entry in schedules.json:
2. Configure a Routing Rule
Section titled “2. Configure a Routing Rule”Add a corresponding rule in router_rules.json to send this schedule event to IM:
Time Synchronization Notes
Section titled “Time Synchronization Notes”cap_scheduler relies on the accuracy of the system time.
- For
cronandoncetasks, they will only be triggered after SNTP synchronization is successful. - For
intervaltasks, ifstart_atorend_atare not configured, they do not rely on external time and start timing upon power-on. cronmatching is based on the device’s current local time (localtime_r).
Time rebase after first time sync
Section titled “Time rebase after first time sync”After the first successful SNTP sync in basic_demo (had_valid_time == false → valid time), cap_scheduler_handle_time_sync() rebases the scheduler: if the device had already been counting from a wrong clock since boot, the first accurate system time adjusts each task’s reference points so triggers are not duplicated or skipped.
Configuration and Initialization
Section titled “Configuration and Initialization”Initialize at the application layer: