Get all calendar day events of tomorrow

Hi,

I try to get all whole-day calendar events of tomorrow. Here is my template code:

- trigger:
    - platform: time_pattern
      minutes: /1
  action:
    - service: calendar.get_events
      data:
        duration:
          hours: 24
          minutes: 0
          seconds: 0
        start_date_time: "{{ (today_at('00:00') + timedelta(days=1)).strftime('%Y-%m-%d %H:%M:%S') }}"
      target:
        entity_id:
          - calendar.abfall
      response_variable: scheduled_events_abfall
    - variables:
        all_events: >
          {{ scheduled_events_abfall["calendar.abfall"].events }}
        filtered_events_restmuelltonne: >
          {{ scheduled_events_abfall["calendar.abfall"].events | selectattr('summary', 'search', 'Restmuelltonne', true) | list }}
        filtered_events_biotonne: >
          {{ scheduled_events_abfall["calendar.abfall"].events | selectattr('summary', 'search', 'Biotonne', true) | list }}
        filtered_events_papiertonne: >
          {{ scheduled_events_abfall["calendar.abfall"].events | selectattr('summary', 'search', 'Papiertonne', true) | list }}
        filtered_events_gelbetonne: >
          {{ scheduled_events_abfall["calendar.abfall"].events | selectattr('summary', 'search', 'Gelbe Tonne', true) | list }}
        filtered_events_glas: >
          {{ scheduled_events_abfall["calendar.abfall"].events | selectattr('summary', 'search', 'Glas', true) | list }}
  sensor:
    - name: Calendar Scheduled Events
      unique_id: calendar_scheduled_events
      state: "{{ all_events | count() }}"
      attributes:
        all_events: "{{ all_events }}"
        filtered_events_restmuelltonne: "{{ filtered_events_restmuelltonne }}"
        filtered_events_biotonne: "{{ filtered_events_biotonne }}"
        filtered_events_papiertonne: "{{ filtered_events_papiertonne }}"
        filtered_events_gelbetonne: "{{ filtered_events_gelbetonne }}"     
        filtered_events_glas: "{{ filtered_events_glas }}"           
      icon: mdi:calendar
    - name: Calendar Restmuelltonne
      unique_id: calendar_restmuelltonne
      state: "{{ filtered_events_restmuelltonne | count() }}"      
      icon: mdi:calendar
    - name: Calendar Biotonne
      unique_id: calendar_biotonne
      state: "{{ filtered_events_biotonne | count() }}"      
      icon: mdi:calendar
    - name: Calendar Papiertonne
      unique_id: calendar_papiertonne
      state: "{{ filtered_events_papiertonne | count() }}"      
      icon: mdi:calendar
    - name: Calendar Gelbe Tonne
      unique_id: calendar_gelbetonne
      state: "{{ filtered_events_gelbetonne | count() }}"      
      icon: mdi:calendar
    - name: Calendar Glas
      unique_id: calendar_glas
      state: "{{ filtered_events_glas | count() }}"      
      icon: mdi:calendar

here is the sensor output:

all_events: 
- start: '2024-01-12'
  end: '2024-01-13'
  summary: 'Restmuelltonne '
  description: Leerungserinnerung
  location: xxx
- start: '2024-01-13'
  end: '2024-01-14'
  summary: Biotonne

filtered_events_restmuelltonne: 
- start: '2024-01-12'
  end: '2024-01-13'
  summary: 'Restmuelltonne '
  description: Leerungserinnerung
  location: xxx

filtered_events_biotonne: 
- start: '2024-01-13'
  end: '2024-01-14'
  summary: Biotonne

filtered_events_papiertonne: 
filtered_events_gelbetonne: 
filtered_events_glas: 
icon: mdi:calendar
friendly_name: Calendar Scheduled Events

In this example you can see that I also get the whole day event of today. but I want only the whole day event of tomorrow.

Any idea what I could do? :slight_smile:

I don’t see how the sensor configuration you have posted can produce to example results you have shared.

Your calendar.get_events is non-functional. You must include either a duration or end_date_time.

ah sorry, that was a copy and paste mistake. I have corrected it.

I can’t reproduce the output you’re getting, the service call should only return tomorrow’s events. You can use a selectattr() or rejectattr() filter to select by the value of start:

- trigger:
    - platform: time_pattern
      minutes: /1
  action:
    - service: calendar.get_events
      data:
        duration:
          hours: 24
          minutes: 0
          seconds: 0
        start_date_time: "{{ today_at() + timedelta(days=1) }}"
      target:
        entity_id:
          - calendar.abfall
      response_variable: scheduled_events_abfall
    - variables:
        all_events: >
          {{ scheduled_events_abfall["calendar.abfall"].events 
          | rejectattr('start', 'eq', today_at().date() | string ) | list }}
        filtered_events_restmuelltonne: >
          {{ all_events | selectattr('summary', 'search', 'Restmuelltonne', true) | list }}
        filtered_events_biotonne: >
          {{ all_events | selectattr('summary', 'search', 'Biotonne', true) | list }}
        filtered_events_papiertonne: >
          {{ all_events | selectattr('summary', 'search', 'Papiertonne', true) | list }}
        filtered_events_gelbetonne: >
          {{ all_events | selectattr('summary', 'search', 'Gelbe Tonne', true) | list }}
        filtered_events_glas: >
          {{ all_events | selectattr('summary', 'search', 'Glas', true) | list }}
  sensor:
    - name: Calendar Scheduled Events
      unique_id: calendar_scheduled_events
      state: "{{ all_events | count() }}"
      attributes:
        all_events: "{{ all_events }}"
        filtered_events_restmuelltonne: "{{ filtered_events_restmuelltonne }}"
        filtered_events_biotonne: "{{ filtered_events_biotonne }}"
        filtered_events_papiertonne: "{{ filtered_events_papiertonne }}"
        filtered_events_gelbetonne: "{{ filtered_events_gelbetonne }}"     
        filtered_events_glas: "{{ filtered_events_glas }}"           
      icon: mdi:calendar
    - name: Calendar Restmuelltonne
      unique_id: calendar_restmuelltonne
      state: "{{ filtered_events_restmuelltonne | count() }}"      
      icon: mdi:calendar
    - name: Calendar Biotonne
      unique_id: calendar_biotonne
      state: "{{ filtered_events_biotonne | count() }}"      
      icon: mdi:calendar
    - name: Calendar Papiertonne
      unique_id: calendar_papiertonne
      state: "{{ filtered_events_papiertonne | count() }}"      
      icon: mdi:calendar
    - name: Calendar Gelbe Tonne
      unique_id: calendar_gelbetonne
      state: "{{ filtered_events_gelbetonne | count() }}"      
      icon: mdi:calendar
    - name: Calendar Glas
      unique_id: calendar_glas
      state: "{{ filtered_events_glas | count() }}"      
      icon: mdi:calendar

I don’t know if you are just using it for testing or it’s what you actually plan on using… but your Time pattern trigger seems a bit extreme for a value that’s only likely to change once a day on average.

1 Like

@Didgeridrew thanks, that is working as expected.And yes, the Tim pattern trigger was just for testing that short.