I’ve seen other threads with people migrating from list_events to get_events, and I’ve tried to follow many; however, I feel like I’m “stuck” at a very basic place that is preventing me from even getting to the “real logic” that I’d like to get to.
My goal:
Create a template that has the start time of the first event tomorrow from a specific calendar that can then be used in automation related to setting an alarm on my phone.
I’ve got my integration to a Google Calendar working fine. I am using “Atomic Calendar Revive Card” to display on one of my dashboards.
I can use Developer Tools → Services → and get the response from my “Get Events” that includes my test calendar items.
service: calendar.get_events
data:
duration:
hours: 48
minutes: 0
seconds: 0
start_date_time: "{{ today_at() }}"
target:
entity_id: calendar.test_calendar
This gets me an output:
calendar.test_calendar:
events:
- start: "2024-02-02T06:00:00-08:00"
end: "2024-02-02T07:00:00-08:00"
summary: Work!
- start: "2024-02-02T20:00:00-08:00"
end: "2024-02-02T21:00:00-08:00"
summary: Test Event 1
- start: "2024-02-03T08:00:00-08:00"
end: "2024-02-03T09:00:00-08:00"
summary: Test Event Tomorrow
- start: "2024-02-03T12:30:00-08:00"
end: "2024-02-03T13:30:00-08:00"
summary: Test Two Event Tomorrow
I have a Template Sensor (Settings → Devices & Services → Helpers → Create Helper → of type Template)
I’ve tried multiple ways to trigger - right now I believe it is set for every minute.
My problem (appears to be) with my “response_variable” and subsequently using it.
template:
- trigger:
- platform: time_pattern
minutes: "/1"
action:
- service: calendar.get_events
data:
duration:
hours: 21
minutes: 0
seconds: 0
start_date_time: "{{ today_at() }}"
target:
entity_id: calendar.test_calendar
response_variable: raw_events
- variables:
scheduled_events: "{{ raw_events['calendar.test_calendar'] }}"
sensor:
- name: First Alarm Tomorrow
unique_id: first_alarm_tomorrow
state: "{{ scheduled_events.events | count() }}"
attributes:
scheduled_events: "{{ scheduled_events.events }}"
icon: mdi:calendar'
I know the logic isn’t right within the sensor - I pulled this from an example I found elsewhere hoping I could “start simple” and then get it right - but even a version copied that works for other people, fails for me.
The log tells me:
TemplateError(‘UndefinedError: ‘raw_events’ is undefined’) while processing template ’
Some questions…
Do I need to define this variable first somewhere?
(And if not, what else am I missing?)
At the end, in my “sensor:” - Do I give it the same name & id that I’ve already defined in the sensor itself? Or does this need to be a separate name? Or do I even put it here since it’s filled out in the template?
Above and beyond… any help with the end result of pulling out just the “earliest start time, of a meeting that starts tomorrow”?
Thank you!