Calendar.get_events doesn't return any values to response variable

Heyhey
I want to use the calendar.get_events Event to build an automation that does the following:
Check the calendar “vertreter” (and two other calendars) for events.

If there is one or more events
publish them in ToDo list
and if its a workday (workday sensor ‘on’) give me a telegram message telling me those events.

if there is no event:
give me a telegram message saying “Vertreter: none”, but only if its a workday.

Here is my automation, but it always tells me there are no events (“Vertreter: none”).
I dont know why.

alias: AA Kalender Test
description: ""
trigger: []
condition: []
action:
  - service: calendar.get_events
    target:
      entity_id:
        - calendar.vertreter
      device_id: []
      area_id: []
    data:
      duration:
        hours: 15
        minutes: 0
        seconds: 0
    response_variable: vertretertasks
  - if:
      - condition: template
        value_template: "{{ vertretertasks.events | count > 0 }}"
    then:
      - alias: Vertreter in ToDo hinzufügen
        repeat:
          for_each: "Vertreter: {{ vertretertasks.events }}"
          sequence:
            - service: todo.add_item
              data:
                item: "{{repeat.item.summary }}"
              target:
                entity_id: todo.todo
      - if:
          - condition: state
            entity_id: binary_sensor.workday_sensor
            state: "on"
        then:
          - service: notify.telegramflorian
            data:
              message: >-
                {% for event in vertretertasks.events %}  {{
                as_timestamp(event.start) | timestamp_custom('%H:%M', true) }}
                Uhr: {{event.summary}} {% endfor %}
              title: "*Vertreter:*"
            enabled: true
    else:
      - if:
          - condition: state
            entity_id: binary_sensor.workday_sensor
            state: "on"
        then:
          - service: notify.telegramflorian
            data:
              title: Vertereter
              message: keine
    enabled: true
mode: single

The response variable does not have a property named events. The service calendar.get_events can return values from multiple source calendars, so its properties are named for each source calendar. You need to specify the source in you templates. This can be done for each instance of vertretertasks like:

{{ vertretertasks['calendar.vertreter'].events | count > 0 }}

Or you can set the value to a new variable prior to your first If/Then action.
Also, your for each needs to be a dictionary or list. You can’t just append the string "Vertreter: " to the list of events, you need to move that to the todo.add_item action.

alias: AA Kalender Test
description: ""
trigger: []
condition: []
action:
  - service: calendar.get_events
    target:
      entity_id:
        - calendar.vertreter
    data:
      duration:
        hours: 15
    response_variable: vertretertasks
  - variables:
      events: "{{ vertretertasks['calendar.vertreter'].events }}"
  - if:
      - condition:  template
        value_template: "{{ events | count == 0 }}"
    then:
      - condition: state
        entity_id: binary_sensor.workday_sensor
        state: "on"
      - service: notify.telegramflorian
        data:
          title: Vertereter
          message: keine
    else:
      - alias: Vertreter in ToDo hinzufügen
        repeat:
          for_each: "{{ events }}"
          sequence:
            - service: todo.add_item
              data:
                item: "Vertreter: {{repeat.item.summary }}"
              target:
                entity_id: todo.todo
      - condition: state
        entity_id: binary_sensor.workday_sensor
        state: "on"
      - service: notify.telegramflorian
        data:
          message: >-
            {%- for event in events %}
            {{ as_timestamp(event.start) | timestamp_custom('%H:%M', true) }}
            Uhr: {{event.summary}} {%- endfor %}
          title: "*Vertreter:*"
mode: single
1 Like

Thank you very much man!
This is exactly what I wanted to achieve!

I only changed the messaging part a bit - only for formatting purposes (to have all events below each other):

service: notify.telegramflorian
data:
  message: >-
    {% for event in events %}{{ as_timestamp(event.start) |
    timestamp_custom('%H:%M', true) }} Uhr: {{event.summary}}
    
    {% endfor %}
  title: "*Vertreter:*"
enabled: true

But I still got one question.
You are using the “condition:state” (Confirm workday sensor is an testen) which I never did before.
But obviously it makes the code much more readable than my version.

Is this condition:state ment as following:
If the condition is not true all following lines (within a section) will be ognored?
Like this:

If {{today is wednesday} then
sendtelegrammessage
condition:state = true
do x
do y
condition:state = false
do z

In this silly example all commands are done (sending telegram message, do x, do z) and the last one (do z) - and all that follows up here, are skipped?

So basically if the condition is not true its exact the same as a stop command?

Yes, it’s essentially a guard clause just like those in the Conditions block of the automation. Any actions after the failed condition will not be executed.