Adding offsets for calendar triggers

The newly implemented calendar triggers are awesome but it would be great if you could add (negative) offsets to the trigger, so that the automation can start before or after the event is scheduled in the calendar. e.g. to notify someone. It would be great if there would be a greater flexibility with this trigger, because it could be very powerful.

I implemented this in Add calendar trigger offsets by allenporter · Pull Request #70963 · home-assistant/core · GitHub and will be available in the next release.

2 Likes

@allenporter - I look after the O365 integration available through HACS. Is there any documentation on how the triggers work, and if there are any changes required for the integration? I seem to remember that the offset is put in the title of the event, but I get the impression from something I read that you wrote, that this may be going away.

Any guidance appreciated.

Hi the calendar integration has documentation at Calendar - Home Assistant and it has been updated recently to include links to the main automation docs and some examples. I believe offsets are in 2022.6 so it may not have the detail yet.

Regarding offsets in titles: Yes, there is some hack in the google calendar integration to support offsets based on strings in titles which is really brittle and was documented a little incorrectly. I’m trying to make it go away moving to a common way to do this that works for all calendars, not just Google, using triggers.

And just to add to this, is there any event fired at the offset, or is any action only initiated by a configured automation trigger. Personally I use Node Red for automation, and wondered if there is anyway of taking advantage of this capability within that.

Ok, I’m working through this, looking at the rc docs. Looks like for my own purposes I can creation an automation to fire an event that I can work from (or even just use the automation event itself).

I guess I was hoping that there was something that would just fire an event based on any Reminder time set on the event (yes the Google !! thing is kinda weird). At the moment the reminder time whilst surfaced from the o365 api is not actually exposed against the calendar entity. Is there anyway the reminder time can be added (I’m sure it can be done technically, but you may have reasons you may not want to), that could then be utilised to on the fly to set notifications on a per calendar event basis, rather than based on relatively static automation?

I can envisage me getting asked that since the reminder is set against the calendar event (in outlook or whatever), why do they then need to set it again in HA.

1 Like

It is possible to return the reminder time from the google calendar API into home assistant. The part I have trouble with is figuring out exactly how to run an automation using the reminder times as input, since it is technically seems harder than using the offset provided by the user. Getting into the details here, the problem is you have to query all possible events over 24 hours then apply the calendar notification offset to compute the actual start/end time of the event, then know what time to make the trigger fire – that is a lot more complex than using a fixed offset to shift the event start/end times. And its specific to google calendar, and not others. Anyway, you get the idea…

So for know, we’re sticking with offsets defined in home assistant only

I thought that was the case and that’s fine. I’ve done various open source calendar work over quite a few years, and understand the challenges. To add to your challenge, of course an event may be added at anytime, so you’ve got to be continually looking for new/changed events/offsets that may crop up at any time.

I can use what is there for myself to improve my own automations, and I’ll watch out for you fully replacing Google offsets to improve the O365 integration as well.

1 Like

Yes good point on detecting newly changed events! Right now it polls the API every 15 minutes looking for events in the next “window” of possible events. One other thing i’m playing with is the idea of syncing events down locally gcal_sync/DESIGN.md at 1d9f547aa6f2d12ed987507a28e5678ab7fb7a2c · allenporter/gcal_sync · GitHub so that triggers will be more efficient and resilient to network outages, hopefully giving the benefits of cloud + local calendar.

Anyway, really helpful to hear how you’re using this so thanks for the discussion and glad to have someone to commiserate with about calendar fun.

Interesting read and sounds like a significant piece of work. I think the O365 integration was originally based on the Google one, and since the creator stopped working on it I have done considerable work on it.

It would be great if whatever you create as a local event store for gcal was reusable by other integrations as well. I understand what you are trying to achieve and it would be a shame if to achieve the same sims others had to duplicate. I imagine you are planning to achieve that anyway.

I do wonder if a flexible offset could be achieved by basically enabling a maximum offset (say an hour) such that when you query for events (I haven’t looked through your logic yet, but I think you query a window) then you just query an hour longer, then used the returned reminders to set the offset rather than using the single configured one on the trigger.

Obviously you understand the complexity far better than I do. But I’m happy to review anything you do on this if you would like me to whatever way you decide to go (just let me know where is the best place to converse). I’ll try and look at the trigger code tomorrow to see if I can come up with ideas.

I’ve been thinking that https://github.com/ics-py/ics-py would be a good basis for a local calendar integration for home assistant and have started looking into it. The main missing feature is recurring events, which seem more complex than I thought! Support recuring events · Issue #14 · ics-py/ics-py · GitHub

The local calendar store code isn’t that complex, but it only supports one operation:
gcal_sync/api.py at 1d9f547aa6f2d12ed987507a28e5678ab7fb7a2c · allenporter/gcal_sync · GitHub – so not too hard for other integrations to do something similar, or we could locally using ics-py.

Recurring events are an absolute nightmare, so be careful. You either end up in complexity of reducing database records, and then overlaying changes to individual instances of an event. Or having every instance of an event in the database,and therefore the risk of database ballooning. I imagine there are cleverer ways of handling that these days, but I struggled with it in a VB based calendar on something called DotNetNuke back in the mid naughties. But it is something you want to think through carefully.

Just looking through that feature request you linked. This brings back nightmare memories of creating iCal feeds based on the data created by the DNN Events module. Recurring events and RRULEs and VTIMEZONEs, with recurring events crossing DST changes, and more and more. Hopefully the ics-py module can insulate you from some of that.

Happy to talk about it if you wish, but iCal support is a completely different ball game to handling the O365 Rest api. This is a potentially interesting read on timezone support in iCal - ics-py/timezone.rst at c1715525423c449f71fd8f718a70b00e5875fed9 · ics-py/ics-py · GitHub