I haven’t worked with the built-in offset settings (!!), but I wanted to have an event that alerts me 15 min before an event ends (so that I’m reminded in time to pick up my son from school). I didn’t have any luck getting this to work as a template trigger, so I created a sensor (with the same template rules), then created an automation that watches the state of that sensor. I ended up creating several sensors that all worked about the same way (various weekly kid school-things), so I set up a sensor for each, then one automation to remind me.
The various Google calendar entities are searches on one calendar.
Here’s my code:
calendar:
- cal_id: [email protected]
entities:
- device_id: nathan_tech_shop
name: Nathan Tech Shop
search: Nate to tech shop
track: true
- device_id: nora_mathcounts
name: Nora MathCounts
search: Nora MathCounts
track: true
- device_id: nora_chessclub
name: Nora Chess Club
search: Nora chess club
track: true
sensor(s) - repeated for each calendar entity. 900sec = 15min. Note that I have a sensor.date__time that keeps the current time. I use that in the sensor so that it updates the value whenever the time changes (every minute).
nate_tech_shop_end:
friendly_name: Tech Shop ends in 15 minutes
entity_id: sensor.date__time
value_template: >
{% if states.calendar.nathan_tech_shop %}
{% if is_state("calendar.nathan_tech_shop", "on") and as_timestamp(states.calendar.nathan_tech_shop.attributes.end_time) - as_timestamp(now()) < 900 %}on{% else %}off{% endif %}
{% else %}off{% endif %}
automation (I used meaningful names in the sensors so that I could just reuse them as part of the notification):
- alias: Time to pick up a kid notification
trigger:
- platform: state
entity_id:
- sensor.nate_tech_shop_end
- sensor.nora_mathcounts_end
- sensor.nora_chess_club_end
state: 'on'
action:
- service: shell_command.speak
data_template:
text: >
{%- for state in states.sensor -%}
{% if state.entity_id == trigger.entity_id -%}
{{ state.attributes.friendly_name | urlencode }}
{%- endif %}
{%- endfor -%}
- service: notify.scott_notifier
data_template:
title: "Time to go!"
message: >
{%- for state in states.sensor -%}
{% if state.entity_id == trigger.entity_id -%}
{{ state.attributes.friendly_name }}
{%- endif %}
{%- endfor -%}