Search for a specific calendar event in the future

Hi,

is there anybody who has an idea to search for the next calendar event with a specific topic?

It is a recurring event which occurs every two years and I wanted to show the upcoming event in a markdown card. The event is in Febr. 2027 but the code shows “no event”


type: markdown
content: |
  {% set events = state_attr('calendar.me.de, 'events') %}
  {% set tuev = None %}

  {% if events %}
    {% for e in events %}
      {% if e.summary is defined and 'TÜV Golf' in e.summary %}
        {% set tuev = as_datetime(e.start).strftime('%d.%m.%Y') %}
        {% break %}
      {% endif %}
    {% endfor %}
  {% endif %}

  {% if tuev %}
  **Nächster TÜV‑Termin:** {{ tuev }}
  {% else %}
  **Kein TÜV‑Termin gefunden**
  {% endif %}

Unless you are using an abnormal calendar integration, there’s no such attribute as events. That being said, your template is missing a closing ' after the entity ID.

You will likely need to set up a trigger-based template sensor that calls the calendar.get_events action.

is there anywhere an example which I can use and I am also struggling to copy the date into a input_datetime in order make it visible in the markdown card.

alias: UI_Tuev
description: ""
triggers:
  - trigger: time_pattern
    minutes: "5"
actions:
  - data:
      end_date_time: "2026-03-17 00:00:00"
    target:
      entity_id: calendar.my.de
    response_variable: agenda
    action: calendar.get_events
mode: single

I can’t get it right. How am I supposed to use the response variable here to copy the date of the next event named ‘TÜV’ into my input_datetime.golf_tuev_next.

yes, I know, copy&paste error, but it also doesn´t work with the correct ’ at the end.

i tried this, but I get the error, that agenda is not defined. I do not understand what is going wrong!

alias: UI_Tuev
description: ""
triggers:
  - trigger: time_pattern
    minutes: "5"
actions:
  - action: calendar.get_events
    target:
      entity_id: calendar.my.de
    data:
      end_date_time: "2033-03-17 00:00:00"
    response_variable: agenda
  - action: input_datetime.set_datetime
    target:
      entity_id: input_datetime.golf_tuev_next
    data:
      date: "{{ agenda['calendar.my.de']['events'][0].start }}"
mode: single

but the format of the data in my.de is there:

e.g.

    - start: "2033-02-07"
      end: "2033-02-08"
      summary: TÜV Golf HH - XX 1234
      description: Hauptuntersuchung Golf HH - XX 1234
      location: Hamburg

The only thing that looks weird is the object ID portion of the entity ID. In HA object ID are generally slugs containing only lower case letters, numbers, and _. Are you sure your calendar’s entity ID is actually calendar.my.de and not calendar.my_de?

Hallo @Didgeridrew ,

there is everything fine with my calendar´s entity ID. I changed the name in the chat-window. This is not the problem. But what is wrong with my automation? why does Homeassistant says that the response_variable is not defined? I am a little bit lost!

maybe you need to define the variable first using Define variable action and use the variable name in the action response.

I copy-pasted your automation into the Automation Editor on my system. It refused to save the automation and reported the value of entity_id is invalid.

.don’t get hung up on the entity ID. It’s correct in my automation. I only changed it here because my real name is in it. In my automation it’s correct: calendar.vorname_nachname_de.

It also doesn’t help to define the variable beforehand — I still get the error that agenda is not defined.”

When you create a fake value for entity_id, for example purposes, be sure to create a valid fake value. Otherwise, people trying to help you will be misled into believing it is your actual entity_id value and it’s invalid.

For future reference, a valid value for entity_id cannot contain more than one period character (it’s reserved for separating the domain from the object_id).

That error implies calendar.get_events either failed or returned nothing.

Have you inspected the automation’s trace? It will reveal exactly what happened during the automation’s execution of calendar.get_events.

Hm, the entity_id is my private calendar. Nobody has access to it, so nobody can test it. It must be replaced with an existing calendar in the specific environment.

I checked the trace, and the error is within that block, but I don´t know what it means:

action: input_datetime.set_datetime
target:
  entity_id: input_datetime.golf_tuev_next
data:
  date: '{{agenda[''calendar.vorname_nachname_de''][''events''][0].start }}'

That’s not the same error that you’ve been saying in the previous posts. That is saying that the events list is empty. Meaning that agenda is populated with a response, but the response didn’t return any events.

Are you sure you have events in your calendar that the calendar is providing to HA? Are you sure you have the correct calendar entity_id?

put this action into settings → developer tools → actions

  - action: calendar.get_events
    target:
      entity_id: calendar.my.de
    data:
      end_date_time: "2033-03-17 00:00:00"

and look at the response data, if it’s empty, then your calendar is not providing you any events.

I have it!
This works exactly as it should. It looks vor the Event TÜV Golf and extracts the date into ‘input_datetime.golf_tuev_next’

alias: Calendar_Events
description: ""
triggers:
  - trigger: time_pattern
    hours: /2
conditions: []
actions:
  - action: calendar.get_events
    target:
      entity_id: calendar.vorname_nachname_de
    data:
      duration: "26304:00:00" #3 years and 1 day
    response_variable: events
  - action: input_datetime.set_datetime
    target:
      entity_id: input_datetime.golf_tuev_next
    data:
      date: >-
        {% set ev = events["calendar.vorname_nachname_de"]["events"] %} {%
        set tuv = ev | selectattr("summary", "search", "TÜV Golf") | list %} {% if
        tuv | count > 0 %}
          {{ as_datetime(tuv[0].start).date() }}
        {% else %}
         1900-01-01
        {% endif %}
mode: single

So in this case, because the service returns events: at the top level, the response_variable in the automation must also be events. This is what I did not understood. You cannot define a free name for the variable, you must use the variable in the reply from calendar.get_events

Yes, when you assign the response variable the name events, that’s the name of the variable…