Checking a Google Calendar

I’ve got the following code that will check a calendar, but when testing the code, it always fails. The calendar looks like the following - I’m so confused why this always fails

alias: Remind to charge Chromebooks
triggers:
  - at: "07:00:00"
    trigger: time
conditions:
  - condition: template
    value_template: >
      {% set tomorrow = (now() + timedelta(days=1)).date() %} {% set events =
      state_attr('calendar.school_calendar', 'entries') %}

      {% if events is none or events | length == 0 %}
        {{ true }}  # No events exist for tomorrow, send reminder
      {% else %}
        {% for event in events %}
          {% if event.all_day and tomorrow.isoformat() in event.start_time and 'closed' in event.summary | lower %}
            {{ false }}  # Found 'closed' in an all-day event, do NOT send reminder
          {% endif %}
        {% endfor %}
        {{ true }}  # If no 'closed' event was found, send reminder
      {% endif %}
actions:
  - data:
      target: media_player.office_echo_dot
      message: This is your reminder, charge your school Chromebooks for tomorrow.
      data:
        type: tts
    action: notify.alexa_media
mode: single

I don’t use the Google Calendar integration so I have zero personal experience, but the documentation makes no mention of an entries attribute. Does it really exist on your calendar entity or did you just make it up…?

Also you cannot use YAML comments inside Jinja templates. The correct syntax for a Jinja comment is to put it between {# and #}.

I’m pretty sure ‘entries’ is the attributes of the calendar entity that contains the list of calendar events. state_attr(‘calendar.burlington_twp_school_calendar’, ‘entries’) fetches all the calendar events (entries) from the specified calendar.

[
  {
    "start": "2025-02-24T08:00:00",
    "end": "2025-02-24T16:00:00",
    "message": "School closed"
  },
  {
    "start": "2025-02-25T08:00:00",
    "end": "2025-02-25T16:00:00",
    "message": "Parent-teacher conferences"
  }
]

I’ve modified the Jinja2 script to the following - the interesting part is that it passes under Templates (in Developer Tools), but it always returns CONDITION NOT PASSED in automation. WHY!?!?!?!?! OMG so frustrating!

{% set tomorrow = (now() + timedelta(days=1)).strftime('%Y-%m-%d') %}
{% set start_time = state_attr('calendar.school_calendar', 'start_time') %}
{% set start_date = start_time.split(' ')[0] if start_time else None %}
{% set message = state_attr('calendar.school_calendar', 'message') %}

{% if start_date == tomorrow %}
  {% if message and 'closed' in message|lower %}
    FALSE
  {% else %}
    TRUE
  {% endif %}
{% else %}
  TRUE
{% endif %}

Your date comparison feels like it could be prone to errors, seems safer to let Jinja/Python handle all the conversions. Try this:

{{ not ((state_attr('calendar.school_calendar', 'start_time')
| as_datetime).date() == (now() + timedelta(days=1)).date() and 
'closed' in state_attr('calendar.school_calendar', 'message') | lower) }}

I gave up and created a template sensor in my sensor.yaml file - then i use that in my automation

That’s interesting.

I use the Google Calendar integration and the CalDAV Calendar integration. Neither produces calendar entities with an entries attribute.

Now I am curious to know why there’s a difference.

1 Like