Check if calendar.list_events returns any tasks

Heyhey
I use the new calendar.List_events event to give a telegram message showing my tasks for today.
This looks like this:

alias: AAAA Kalender GanztagesereignisseTest
description: ""
trigger: []
condition: []
action:
  - service: calendar.list_events
    data:
      duration:
        hours: 24
        minutes: 0
        seconds: 0
    target:
      entity_id: calendar.bestellen
    response_variable: calendartasks
  - service: notify.telegramflorian
    data:
      message: |-
        {% for event in calendartasks.events %} 
        {{event.summary}} {% endfor %}
      title: "*tasks today:*"
mode: parallel
max: 10

Ofcourse, if there are no tasks in this calendar, the message will be kinda useless.
Is there a way to check the amount of calendar.list_events return, to form an if statement?

Yes, you can check the number of events contained in the response variable with a count filter. Add the following to any If/Then, Choose, or standalone Condition action after your calendar.list_events action:

  - condition: template
    value_template: "{{ calendartasks.events | count > 0 }}"
1 Like

Thank you really mutch! its workin perfectly fine!
For others with same problems like me, here is my full code:

alias: AAAA Kalender GanztagesereignisseTest
description: ""
trigger: []
condition: []
action:
  - service: calendar.list_events
    data:
      duration:
        hours: 24
        minutes: 0
        seconds: 0
    target:
      entity_id: calendar.bestellen
    response_variable: calendartasks
  - if:
      - condition: template
        value_template: "{{ calendartasks.events | count > 0 }}"
    then:
      - service: notify.telegramflorian
        data:
          message: |-
            {% for event in calendartasks.events %} 
            {{event.summary}} {% endfor %}
          title: "*heute bestellen:*"
      - repeat:
          for_each: "{{ calendartasks.events }}"
          sequence:
            - service: shopping_list.add_item
              data:
                name: "{{ repeat.item.summary }}"
mode: parallel
max: 10

1 Like

You don’t need an if statement. Just do what Didgeridrew suggested.

I think i did? Oo

But I have another question.
In future ill completely remove my google calendar and switch to home assistant.
So ill have more calendars to check for events and give me a telegram message.
Is there a way to get only 1 telegram message for all calendars?
Lets say, ill run the code above twice. Telegram will send me 2 messages.
Is it possible to get this inside 1 message? So store the message in a variable or sth?

The Template Condition can be placed in line with the other actions. If the condition evaluates to true, the actions following it are executed, otherwise they are not.

alias: AAAA Kalender GanztagesereignisseTest
description: ""
trigger: []
condition: []
action:
  - service: calendar.list_events
    data:
      duration:
        hours: 24
        minutes: 0
        seconds: 0
    target:
      entity_id: calendar.bestellen
    response_variable: calendartasks
  - condition: template
    value_template: "{{ calendartasks.events | count > 0 }}"
  - service: notify.telegramflorian
    data:
      message: |-
        {% for event in calendartasks.events %} 
        {{event.summary}} {% endfor %}
      title: "*heute bestellen:*"
  - repeat:
      for_each: "{{ calendartasks.events }}"
      sequence:
        - service: shopping_list.add_item
          data:
            name: "{{ repeat.item.summary }}"
mode: parallel
max: 10