I was missing this feature in Home Assistant, so I spent some time figuring out how to make a custom component.
The integration allows Home Assistent events to be scheduled for future delivery. For example, if you have an automation that listens to reminder events, you could schedule an event to trigger that automation in the future. This is more flexible than a fixed time trigger, or even a time pattern.
Please let me know any feedback and code (architecture) reviews you have!
Use
The Event Scheduler integration offers 3 services:
event_scheduler.upsert_event: Add or Update an event to be fired at a specific time.
event_scheduler.remove_event: Remove a scheduled event.
event_scheduler.fire_events: Fire the events that were scheduled at or before the current time.
The documentation for the fields in each service can most easily be viewed through the developer tools ā services UI. Specifically you can view the description in the GUI view and the fields documentation in the YAML view (HA quirk).
I have a normal automation (so not included in the custom component) that calls event_scheduler.fire_events every minute (which is good enough for my use case).
Install
You can most easily install the integration through HACS if you add it as a custom repository (3 dots in the top right corner ā custom repositories).
Alternatively you can copy the (custom_component/)event_scheduler folder to your config/custom_components folder.
Hi. I am not understanding how this integration works. Can you give an example of an automation before using this integration and one adapting this integration??
Sure, here is a simplified example of how I use it to trigger a TTS reminder every 30 minutes after a calendar event in my ādagplanningā has started (daily schedule tasks). This specific example automation will never stop, but thatās part of what I removed for simplicity.
The closest approximation I could make without this integration, was to make boolean helper variables for each item in my calendar and set them to true if the TTS needs to occur. That also required a time pattern automation trigger to check the state of those helper variables at predefined times. In short, it requires more setup than simply adding an item to my calendar and all reminders happen at a predefined time (e.g. xx:00 and xx:30) rather than any timedelta after the calendar item.
I hope this clarifies my use case, but Iām sure there are many others.
I found a different and much simpler example. This automation shows an app notification to empty the laundry, 1 hour and 5 minutes after I scan an NFC tag.
The difference is that instead of inserting a delay action, I store the upcoming event and the automation can finish immediately after.
There are 2 benefits that come to mind, one obvious and one less so.
The event will still happen even if Home Assistant is restarted during the waiting period. Restarts happen often enough that itās a concern, for example when a new version of a HACS integration is released.
Less obviously, itās now very easy to āinterruptā the waiting time by removing the scheduled event. This is useful if you want to schedule a TTS reminder that happens unless the app notification is acknowledged quickly enough.
Before
Note that I have a separate automation that listens to notification events
Hello. Thanks for the examples. Will study this in bit more detail when I have some free time. So you are saying with the after example above, the automation will continue even after HA restarts?? That would be a plus.
More or less. The nuance is that the automation can stop/finish and the event will be picked up and fired off atnote the scheduled time. That could trigger the same or a different automation.
Note: The way Iāve implemented it now, the event is not fired off exactly at the scheduled time, but when the event_scheduler.fire_events service is called after that. I do that every minute in a separate automation right now. Itās a bit ugly, but close enough to be usable for my current purposes, and I hope usable for others too.
FWIW, the current way to schedule something in the future would be to use a Local Calendar (or Google Calendar). In the following example, when the tag is scanned, a calendar event is created for one hour and five minutes later in calendar.notifications.
A separate automation, employing a Calendar Trigger, can monitor calendar.notifications and send a notification, containing the text in summary at the scheduled time
The advantage of your custom integration is that you can use service calls to programmatically insert/update/remove events whereas currently thereās only the calendar.create_event service call available for Local and Google calendars.
Another benefit compared to local calendars is that you can easily pass complicated data structures, rather than maybe trying to put them into the description field and hope that it can fit enough data (havenāt looked into that).
Also, itās not always necessary to catch the scheduled event with your own automation. It could just as easily be a call_service event.
FWIW, in a Calendar Event, you can pass data structures as JSON.
Thatās where Calendar Events currently have the advantage because thereās no need for such an āEvent Schedulerā automation (itās handled internally).