How to list multiple calendar event in a template

Your sensor is listening for a different event than your automation is creating…

However, with today’s release of 2023.9, the intermediary automation is no longer necessary. Once you update, you can get rid of the automation and just use the following for your sensor.

template:
  - trigger:
      - platform: time_pattern
        minutes: /1
    action:
      - service: calendar.list_events
        data:
          duration:
            hours: 192
            minutes: 0
            seconds: 0
          start_date_time: "{{ today_at() }}"
        target:
          entity_id: calendar.travail
        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'
1 Like

Hello,

Thank you Zinken fr your guide and explanation.
Now it’s working very fine !

Drew, so nice. I have installed the 2023.9 release and test the simplied implementation.
IT’s working very fine too !

Thank you guys for your help.

impressive, looks good.

can you help, need the next 5 events as “state” for esphome…

  • here i see only 3
  • can i add location?
  • how can i get a clear time and date?
Friseur
11.09.2023
16:30 - 17:30

i think i need a format for esphome like this (sample)

  - platform: homeassistant
    id: event01_summary
    entity_id: sensor.xxxx

  - platform: homeassistant
    id: event01_startdate
    entity_id: sensor.xxxx

  - platform: homeassistant
    id: event01_starttime
    entity_id: sensor.xxxx

  - platform: homeassistant
    id: event01_enddate
    entity_id: sensor.xxxx

  - platform: homeassistant
    id: event01_endtime
    entity_id: sensor.xxxx

is it possible, to observe more as one calendar? this here works not:

#-------------------------------------------------------------
# Display Kalender Events
#-------------------------------------------------------------
  - trigger:
      - platform: time_pattern
        minutes: /1
    action:
      - service: calendar.list_events
        data:
          duration:
            hours: 192
            minutes: 0
            seconds: 0
          start_date_time: "{{ today_at() }}"
        target:
          entity_id:
            - calendar.one
            - calendar.two
            - calendar.three
        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'

10 comments up

You shouldn’t need to use namespace here for any of those. Just add the lists together, and you can use variables.

  - trigger:
      - platform: calendar
        event: end
        entity_id: calendar.a
      - platform: calendar
        event: end
        entity_id: calendar.b
      - platform: calendar
        event: end
        entity_id: calendar.c
      - platform: event
        event_type: event_template_reloaded
    action:
      - service: calendar.list_events
        data:
          duration:
            hours: 24
        target:
          entity_id:
            - calendar.a
        response_variable: agenda1
      - service: calendar.list_events
        data:
          duration:
            hours: 24
        target:
          entity_id:
            - calendar.b
        response_variable: agenda2
      - service: calendar.list_events
        data:
          duration:
            hours: 24
        target:
          entity_id:
            - calendar.c
        response_variable: agenda3
      - variables:
          all_events: >
            {{ agenda1.events + agenda2.events + agenda2.events }}
          events: >
            {{ all_events | selectattr('start', 'search', now().date() | string) | list }}
    sensor:
      - name: Next 24hr Events
        unique_id: custom_multical_todays_events
        state: "{{ events | count }}"
        attributes:
          scheduled_events: "{{ events }}"

and if you want to shorten the yaml, you can use anchors.

  - trigger:
      - <<: &cal_trigger
          platform: calendar
          event: end
        <<: &cal_a
          entity_id: calendar.a
      - <<: *cal_trigger
        <<: &cal_b
          entity_id: calendar.b
      - <<: *cal_trigger
        <<: &cal_c
          entity_id: calendar.c
      - platform: event
        event_type: event_template_reloaded
    action:
      - <<: &cal_action
          service: calendar.list_events
          data:
            duration:
              hours: 24
        target: *cal_a
        response_variable: agenda1
      - <<: *cal_action
        target: *cal_b
        response_variable: agenda2
      - <<: *cal_action
        target: *cal_c
        response_variable: agenda3
      - variables:
          all_events: >
            {{ agenda1.events + agenda2.events + agenda2.events }}
          events: >
            {{ all_events | selectattr('start', 'search', now().date() | string) | list }}
    sensor:
      - name: Next 24hr Events
        unique_id: custom_multical_todays_events
        state: "{{ events | count }}"
        attributes:
          scheduled_events: "{{ events }}"
2 Likes

thx for this code snipped. not easy for me to understand… i tried it with my calendar, but my entity is empty.

You’d have to post what you tried.

my code: (same as yours, only with my calendars)

#-------------------------------------------------------------
# Display Kalender Events
#-------------------------------------------------------------
  - trigger:
      - platform: calendar
        event: end
        entity_id: calendar.mil
      - platform: calendar
        event: end
        entity_id: calendar.ab
      - platform: calendar
        event: end
        entity_id: calendar.geburtstage
      - platform: event
        event_type: event_template_reloaded
    action:
      - service: calendar.list_events
        data:
          duration:
            hours: 24
        target:
          entity_id:
            - calendar.mil
        response_variable: agenda1
      - service: calendar.list_events
        data:
          duration:
            hours: 24
        target:
          entity_id:
            - calendar.ab
        response_variable: agenda2
      - service: calendar.list_events
        data:
          duration:
            hours: 24
        target:
          entity_id:
            - calendar.geburtstage
        response_variable: agenda3
      - variables:
          all_events: >
            {{ agenda1.events + agenda2.events + agenda2.events }}
          events: >
            {{ all_events | selectattr('start', 'search', now().date() | string) | list }}
    sensor:
      - name: Next 24hr Events
        unique_id: custom_multical_todays_events
        state: "{{ events | count }}"
        attributes:
          scheduled_events: "{{ events }}

no idea whats wrong ?

You’re missing a closing quote on the final line.

only a copy/paste error.

#-------------------------------------------------------------
# Display Kalender Events
#-------------------------------------------------------------
  - trigger:
      - platform: calendar
        event: end
        entity_id: calendar.mi 
      - platform: calendar
        event: end
        entity_id: calendar.abfall
      - platform: calendar
        event: end
        entity_id: calendar.geburtstage
      - platform: event
        event_type: event_template_reloaded
    action:
      - service: calendar.list_events
        data:
          duration:
            hours: 24
        target:
          entity_id:
            - calendar.mi 
        response_variable: agenda1
      - service: calendar.list_events
        data:
          duration:
            hours: 24
        target:
          entity_id:
            - calendar.abfall
        response_variable: agenda2
      - service: calendar.list_events
        data:
          duration:
            hours: 24
        target:
          entity_id:
            - calendar.geburtstage
        response_variable: agenda3
      - variables:
          all_events: >
            {{ agenda1.events + agenda2.events + agenda2.events }}
          events: >
            {{ all_events | selectattr('start', 'search', now().date() | string) | list }}
    sensor:
      - name: Next 24hr Events
        unique_id: custom_multical_todays_events
        state: "{{ events | count }}"
        attributes:
          scheduled_events: "{{ events }}" 

perhaps this syntax isnt correct. i dont know.
has anyone a other solution? thx in advance.

Hi, I have a question regarding the days I see.

This line gives me 3 days of events. today, tomorrow and the day after tomorrow:

state_attr('sensor.calendar_scheduled_events', 'scheduled_events') 

How would I be able to limit this to only see today and tomorrow ?

Actually I was putting chatgpt to work and got this as a solution:

{% set today = now().date() %}
{% set tomorrow = now().date() + timedelta(days=2) %}
{% set items = state_attr('sensor.calendar_scheduled_events', 'scheduled_events') | selectattr('start', 'ge', today.strftime('%Y-%m-%d')) | selectattr('start', 'lt', tomorrow.strftime('%Y-%m-%d')) | sort(attribute='start') %}

Seems to work. Any comments ?

When you create your sensor sensor.calendar_scheduled_events you use a service call calendar.list_events
There you can specify start_date_time, end_date_time or duration

That way you don’t have to filter the created sensor ‘sensor.calendar_scheduled_events’ it will already have the events for today and tomorrow if you use duration days: 2

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