How to list multiple calendar event in a template

How can I trigger the scheduled_events by time and a specific ‘summary’ (e.g. Event-Name)?

By using the normal calendar trigger.

Hi all !

I’m having fun with the calendar integration. I’m stuck on a little problem, is there a way, in the template, to exclude an event by targetting the ‘summary’ ?

Here’s my template code :

- trigger:
      - platform: time_pattern
        minutes: /1
  action:
      - service: calendar.list_events
        data:
          duration:
            hours: 24
            minutes: 0
            seconds: 0
          start_date_time: "{{ (now()+ timedelta(days=1)).strftime('%Y-%m-%d') }}"
        target:
          entity_id: calendar.ben_boulot
        response_variable: ben_events_tomorrow
  sensor:
      - name: Calendar Ben Boulot Tomorrow
        unique_id: calendar_ben_boulot_tomorrow
        state: "{{ ben_events_tomorrow.events | count() }}"
        attributes:
          scheduled_events: "{{ ben_events_tomorrow.events }}"
        icon: mdi:calendar'

Thanks in advance and sorry for my bad english ^^

Yes, you can use Jinja filters to select or reject events based on the summary.

...
    sensor:
      - name: Calendar Ben Boulot Tomorrow
        unique_id: calendar_ben_boulot_tomorrow
        state: |
          {{ ben_events_tomorrow.events 
          | rejectattr('summary', 'search', 'key word', true) 
          | list | count() }}
        attributes:
          scheduled_events: |
            {{ ben_events_tomorrow.events 
            | rejectattr('summary', 'search', 'key word', true) 
            | list }}
        icon: mdi:calendar'
1 Like

FYI…

- trigger:
      - platform: time_pattern
        minutes: /1
  action:
      - service: calendar.list_events
        data:
          duration:
            hours: 24
            minutes: 0
            seconds: 0
          start_date_time: "{{ (now()+ timedelta(days=1)).strftime('%Y-%m-%d') }}"
        target:
          entity_id: calendar.ben_boulot
        response_variable: ben_events_tomorrow

      - variables:
          filtered_events: >
            {{ ben_events_tomorrow.events | rejectattr('summary', 'search', 'key word', true) | list }}
   
  sensor:
      - name: Calendar Ben Boulot Tomorrow
        unique_id: calendar_ben_boulot_tomorrow
        state: "{{ filtered_events | count() }}"
        attributes:
          scheduled_events: "{{ filtered_events }}"
        icon: mdi:calendar'
1 Like

@Didgeridrew @petro big thanks for your help, exactly what i needed <3

1 Like

Sorry, i’m just a beginner but i see a trigger and an action so this is an automation.
What is the sensor doing there ?
I don’t understand.

If you aren’t comfortable splitting your configuration, the easiest place to add them is your configuration.yaml file (see “Editing configuration.yaml”).

That’s so great :star_struck:. Thanks a lot @Garyw . Ive spent so much time to find an solution for counting my calendar events for the current day. Thats my code in the template.yaml file

#Kalender PRIVAT
- trigger:
    platform: time_pattern
    minutes: /15
  action:
  - service: calendar.list_events
    target:
      entity_id: calendar.privat
    data:
      start_date_time: "{{ now().strftime('%Y-%m-%d 00:00:00') }}"
      end_date_time: "{{(as_timestamp(now()) + (24*3600)) | timestamp_custom('%Y-%m-%d 00:00:00', True)}}"
    response_variable: scheduled_events_privat
  sensor:
  - name: Scheduled Events PRIVAT 24h
    unique_id: scheduled_events_privat_24h
    state: "{{ scheduled_events_privat.events | count() }}"
    attributes:
      scheduled_events_privat: "{{ scheduled_events_privat.events }}"
    icon: mdi:calendar

So i only place the sensor in the configuration ?

When you ask vague questions it’s hard to give you accurate and helpful answers.
What do you mean by “only… the sensor”? You need to place the entire trigger-based Template sensor configuration in your configuration. It needs to have a top level key template:. Some of the examples above (like #88) show this.

You may wish to consider changing this:

end_date_time: "{{(as_timestamp(now()) + (24*3600)) | timestamp_custom('%Y-%m-%d 00:00:00', True)}}"

to this more concise version:

end_date_time: "{{ (now() + timedelta(days=1)).strftime('%Y-%m-%d 00:00:00') }}"
1 Like

Sorry if i made myself not clear enough.
So i did what you told me but this is the part i don’t understand…

Schermafbeelding 2023-11-04 171911

You’re using Visual Studio Code with Config Helper for Home Assistant.

Config Helper is what checks the YAML syntax and confirms it complies with Home Assistant’s scripting language.

The action key word was recently added to Template entities (like your Trigger-based Template Sensor). However, it appears that Config Helper doesn’t know about this recent addition so it flags it as an error. You can safely ignore that error.

The most recent version of Config Helper is 1.39. If you are running that version and it’s flagging the action key word then it means that (hopefully) the next version will accept the action key word.

Hmmm…that explains a lot. Thank you so much.
One more question…
I’m using this sensor with google calender to display a message on my tablet when garbage will be collected.

I just don’t know how to display the different events from a single day.
My wish is to use a markdown card or a custom button card that displays the different events.

Refer to the example for a Markdown Card at the bottom of this post:

That did the trick :grinning_face_with_smiling_eyes:
Now just some tweaking and it looks great.
Thanks again.

I know that calendar.list_events has been changed with get_events

But now below working template became unavailable

template:
  - trigger:
      - platform: time_pattern
        minutes: /1
    action:
      - service: calendar.get_events
        data:
          duration:
            hours: 2191
            minutes: 0
            seconds: 0
          start_date_time: "{{ today_at() }}"
        target:
          entity_id: calendar.dogum_gunleri
        response_variable: scheduled_events
    sensor:
      - name: Calendar Scheduled Events
        unique_id: calendar_scheduled_events
        state: "{{ scheduled_events.events | count() }}"
        attributes:
          scheduled_events: "{{ scheduled_events.events }}"
        icon: mdi:calendar'

Do you have any idea about where I am making the mistake ? The above was used to work fine before.

I’m in the same situation, it seems that the response variable is a bit different with calendar entity as first “level”.

calendar.family:
  events:
    - start: "2023-12-14T20:30:00+01:00"

I’ve tried to modify my variable with all sorts of combination:

    - variables:
        events: >
          {{ scheduled_events[0][events] | sort(attribute='start') | list }}

Dot notation, bracket notation, name, postition etc. but I can’t get it to work

edit:
This is working after several attempts:

- trigger:
    - platform: time_pattern
      minutes: /5
  action:
    - service: calendar.get_events
      data:
        duration:
          hours: 168
          minutes: 0
          seconds: 0
      target:
        entity_id:
          - calendar.family
          - calendar.holidays
          - calendar.garbage
      response_variable: scheduled_events
    - variables:
        events: >
          {{ scheduled_events["calendar.family"].events + scheduled_events["calendar.holidays"].events + scheduled_events["calendar.garbage"].events }}
  sensor:
    - name: Calendar Scheduled Events
      unique_id: calendar_scheduled_events
      state: "{{ events | count() }}"
      attributes:
        scheduled_events: "{{ events }}"
      icon: mdi:calendar
template:
  - trigger:
      - platform: time_pattern
        minutes: /1
    action:
      - service: calendar.get_events
        data:
          duration:
            hours: 2191
            minutes: 0
            seconds: 0
          start_date_time: "{{ today_at() }}"
        target:
          entity_id: calendar.dogum_gunleri
        response_variable: raw_events
      - variables:
          scheduled_events: "{{ raw_events['calendar.dogum_gunleri'] }}"
    sensor:
      - name: Calendar Scheduled Events
        unique_id: calendar_scheduled_events
        state: "{{ scheduled_events.events | count() }}"
        attributes:
          scheduled_events: "{{ scheduled_events.events }}"
        icon: mdi:calendar'
1 Like