Calendar Trigger in Automation: Getting Event Start Time in Template

I have a simple automation that is using a calendar event to trigger a notification. While I could hard-code the event start time, ideally, I’d really like to have the notification dynamically tell me the start time using ‘trigger.calendar_event.start’ (both so I can copy/paste this automation for other uses and also just because I’d like to learn how), however I can’t figure out how to do this. I’ve spent a fair amount of time searching and trying variations I’ve found, but nothing seems to work. Specifically this:

{{ as_timestamp(strptime(state_attr('', 'start_time'), '%Y-%m-%d %H:%M:%S')) | timestamp_custom("%-I:%M %p") }}

gets me exactly what I want when using a calendar entity (i.e. it returns 9:00 AM), but I haven’t figured out how to reformat it to work with ‘trigger.calendar_event.start’. I BELIEVE it’s because it doesn’t seem to output a normal timestamp, but rather for instance: “2023-08-16T15:50:00-05:00”. Obviously it’s extra-difficult to iterate on variations for testing since I have to setup test calendar events to get it to trigger since I can’t just use Developer Tools.

Any help would be greatly appreciated, thanks!

It looks like it’s a datetime object.

{{ trigger.calendar_event.start.strftime('%H:%M') }}

If it’s a datetime string then you’ll need to do this:

{{ (trigger.calendar_event.start | as_datetime).strftime('%H:%M') }}

That one yielded this error in the trace: “Error: Error rendering data template: UndefinedError: ‘str object’ has no attribute ‘strftime’”

When I tried to enter this and saved, whenever I re-opened the automation it had replaced it with: “”[object Object]“: null”

So it’s a datetime string.

Sounds like a problem with the visual Automation Editor.

Post your automation in YAML format.

Weird, I tried it a couple times formatted slightly differently and it finally lasted through a save/reload of the automation (but it added the quotation marks). Been doing it in YAML the entire time. I’m sure I was just making a silly copy/paste error that I couldn’t see. Ended up with this:

service: notify.ben_s_devices
  message: "{{ (trigger.calendar_event.start | as_datetime).strftime('%H:%M') }}"

And it worked! Thanks a ton, definitely couldn’t have figured it out otherwise! I changed the final time format to meet my needs: strftime(’%-I:%M %p’).

1 Like

It was unclear to me where you intended to use the template so I omitted outer quotes.

Now that I see where it was used, it requires outer quotes. The alternative would be like this:

service: notify.ben_s_devices
  message: >
    {{ (trigger.calendar_event.start | as_datetime).strftime('%H:%M') }}

Yeah, I actually started off with the multi-line approach but it didn’t like it for some reason. Probably did the indents wrong, but regardless, ultimate success. Thanks!