Hi there,
I’m trying to create an automation that fires a trigger based on a Google Calendar end-event but with a templated offset.
To give you the scenario, I need an automation that tells me when to leave to pick my kids up from school. I’m imagining I have a Google Calendar setup that has “At School” events during term-times that run from eg 9am to 3pm. As I don’t generally know where I’ll be on any given day, I would like to use my Waze drive-time integration to calculate the time required to get to school from wherever I currently am and then offset that from the “end-event” time of the school calendar, so that it gives me…
End event time = 3pm, Current Waze travel time = 17minutes,
therefore fire trigger at 2:43pm
I realise there may be some issues here with a varying travel time and it may fire the automation more than once depending on if i’m travelling(?) but my main concern at the moment is how to template the time offset on a trigger
The other complication i have is that the calendar in question has various events across the day so I need to only run this on an event that has the “School” summary title
That requirement rules out the use of Calendar Trigger because its offset option doesn’t support templates.
Without the use of a Calendar Trigger, it becomes more challenging to determine when a Calendar event is scheduled to occur. You would need to periodically collect scheduled events, using calendar.get_events, and check if any meet your criteria. It’s far from ideal and not recommended if you intend to poll the calendar very frequently.
Don’t be tempted to monitor the calendar entity itself. It can only show the next scheduled event and if that event is an all-day event then it will be the only event it reports for the entire day (it will not report any other scheduled events for that day).
I’m not sure if this will work or not though and if it does(!) how would I then narrow this down to just 1 event of a day containing multiple events? Somehow I need to specify the event summary text in the above code…
eg…
{% if (((state_attr('calendar.child','end_time','<MY SUMMARY TEXT>')|as_datetime
I think I’ve got it working with the following code. Posting here so it helps someone else…
alias: Notification - Child - School pickup reminder
description: ""
triggers:
- trigger: template
value_template: >-
{% if ((((state_attr('calendar.child','end_time')|as_datetime -
timedelta(minutes=5))|as_timestamp|int -
(state_attr('sensor.me_to_school','duration')|int)*60)|as_datetime)|as_timestamp|int
== now().replace(second=0)|as_timestamp|int) and
state_attr('calendar.child','message') == 'School' %}true{% endif %}
conditions: []
actions:
- action: notify.mobile_app_phone
metadata: {}
data:
message: It's time to leave to pick up child from school
title: Time To Go
mode: single
As Taras mentioned above, don’t rely on the state of the calendar entity, it will come back to bite you unless you can guarantee that the calendar will never have events (including all-day events) that will overlap with the “School” event.