Calendar based triggers

HA sees my iCal iCloud calendars. I have a school year calendar which I would like to use to trigger events rather than work days since there are days/weeks/months where there is no work day. I’ve been reading all day and trying to get an event to fire when there is “work day” as an all day event in the calendar. The automation “trigger” only has a start/stop. How do I set “work days (or whatever) as the condition before firing actions? I tried the template condition {{ ‘workday’ in trigger.calendar_event.summary }} but that doesn’t work. I get an error “

In get this: UndefinedError: ‘trigger’ is undefined”

Here is the code:

alias: Work day
description: “”
trigger:

  • platform: calendar
    event: start
    offset: “0:0:0”
    entity_id: calendar.rhs_sy22_23
    condition:
  • condition: template
    value_template: “{{ ‘workday’ in trigger.calendar_event.summary }}”
    action: []
    mode: single

yaml

calendar:

  • platform: caldav
    url: https://caldav.icloud.com
    username: !secret apple_user
    password: !secret apple_specific
    calendars:
    • Family
    • RHS SY22-23

How are you testing the automation?

The trigger variable exists only if your automation was triggered by its Calendar Trigger. If you use another means to test it (the Run command or automation.trigger service call) the trigger variable will be undefined.

Reference: Testing your automation

alias: Work day
description: ""
trigger:
  - platform: calendar
    event: start
    offset: "0:0:0"
    entity_id: calendar.rhs_sy22_23
condition:
  - condition: template
    value_template: "{{ 'workday' in trigger.calendar_event.summary }}"
action:
  - service: notify.persistent_notification
    data:
      message: "It's a workday."
mode: single

Thanks so much for the quick reply.

You’re welcome! Did it help explain the cause of the error message you received?

This worked perfectly. Now I just have to tweak it. The flat part of my forehead (“doh”) thanks you too! :man_facepalming:

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic is resolved. This helps other users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.

Tip:
It’s the post that answers/solves the question/problem that is marked with the Solution tag. You marked your own post but it doesn’t contain information that explains why your automation failed to trigger.

Thanks again!

1 Like

Hi again - I’m sorry but the automation really isn’t working. Two things:

  1. Doesn’t automatically fire at midnight

  2. It’s always a “workday” regardless if that text is in the current calendar day.

Solutions are much appreciated!

What changed to make it go from
“This worked perfectly.”
to
“Doesn’t automatically fire at midnight”

I think I was too eager and when I was originally testing, it was a workday and the notification said it was a workday. Poor troubleshooting on my part, I should have waited.

I don’t think it’s reading today’s calendar_event.summary.

Basically, I’m trying to do the same thing here as this thread. I have a separate calendar that my wife uses for school (teacher.) There are 4 kinds of “days” she works. When those days are not present in the calendar, she isn’t working. All the morning automations should be turned off or other “vacation” automations should fire.

Her work days are “Red Day”, “Blue Day”, “CCR Day”, and “Purple Day”. These are all day events in the calendar.

How do I best do this automation? I see the link below has a relatable situation but am not sure how to proceed.

The automation I posted above only works if the calendar event’s summary contains the word “workday”. It won’t match “Red Day”, “Blue Day”, etc.

Did you modify the automation to match those words? Please post the exact automation you are using with whatever modifications you made.

Yes. Work day was just something I used to make it easy to understand.

Post the modifications you made to the example.

Not sure how to “query” the calendar and tell me what it thinks today is. Here is the automation and sample calendar screenshot.

alias: Work day
description: “”
trigger:

  • platform: calendar
    event: start
    offset: “-0:0:0”
    entity_id: calendar.rhs_sy22_23
    condition:
  • condition: or
    conditions:
    • condition: template
      value_template: “{{ ‘Red Day’ in trigger.calendar_event.summary }}”
    • condition: or
      conditions:
      • condition: template
        value_template: “{{ ‘Blue Day’ in trigger.calendar_event.summary }}”
    • condition: or
      conditions:
      • condition: template
        value_template: “{{ ‘Purple Day’ in trigger.calendar_event.summary }}”
    • condition: or
      conditions:
      • condition: template
        value_template: “{{ ‘CCR Day’ in trigger.calendar_event.summary }}”
  • condition: time
    before: “00:00:00”
    after: “05:00:00”
    weekday:
    • mon
    • tue
    • wed
    • thu
    • fri
      action:
  • service: notify.persistent_notification
    data:
    message: Today is a work day
    mode: parallel
alias: Work day
description: ""
trigger:
  - platform: calendar
    event: start
    offset: "0:0:0"
    entity_id: calendar.rhs_sy22_23
condition:
  - condition: template
    value_template: >
      {% set days = ['Red Day', 'Blue Day', 'Purple Day', 'CCR Day'] %}
      {{ days | select('in', trigger.calendar_event.summary) | list | count > 0 }}
action:
  - service: notify.persistent_notification
    data:
      message: "It's a workday."
mode: single

Are the scheduled events All Day events? Or are they events with start and stop times?

If they are All Day events, the trigger occurs at midnight (start of the new day) so you can’t add a Time Condition restricting the time for ‘after 05:00’ because then you won’t get a notification.

Yes they are all day events. Okay, that makes sense. I will try that and get back to you.

No notification. I’m wondering if it’s because it’s an Apple Calendar and not a Google Calendar? Should that even matter? It’s an iCal format and HA sees it.

At this point I would eliminate the Template Condition and see if the Calendar Trigger works.

Yeah, nothing has triggered. I’m going to change the name to Schoolday and copy the code there.

alias: Schoolday
description: “”
trigger:

  • platform: calendar
    event: start
    offset: “0:0:0”
    entity_id: calendar.rhs_sy22_23
    condition:
  • condition: template
    value_template: >
    {% set days = [‘Red Day’, ‘Blue Day’, ‘Purple Day’, ‘CCR Day’] %}
    {{ days | select(‘in’, trigger.calendar_event.summary) | list | count > 0 }}
    action:
  • service: notify.persistent_notification
    data:
    message: “It’s a school day.”
    mode: single