Calendar.get_events for tomorrow, shows all day event of current day

Hi everyone,

I want to store all events of the following day in a sensor, so that every evening a tts notification tells me what events I have for tomorrow. This logic worked fine, but I think since two updates, if I have a full day event today, it is also part of tomorrow’s list.

- trigger:
    - platform: time_pattern
      minutes: /1
  action:
    - service: calendar.get_events
      data:
        duration:
          hours: 0
          minutes: 1439
          seconds: 0
        start_date_time: "{{ today_at() + timedelta(days=1) }}"
      target:
        entity_id: 
          - calendar.gemeinschaftskonto_gk
          - calendar.barbara_barbara
          - calendar.standardkalender_markus
      response_variable: calendars
    - variables:
          ev_list_t: |
            {% set ns = namespace(cal_events=[]) %}
            {%- for key, value in calendars.items() %}
              {%- for event in value.events %}
                {%- set ns.cal_events = ns.cal_events + [event] %}
              {%- endfor %}
            {%- endfor %}
            {{ ns.cal_events | sort(attribute='start') | list }}
  sensor:
      - name: Calendar Scheduled Events Tomorrow
        unique_id: calendar_scheduled_events_tomorrow
        state: "{{ ev_list_t | count }}"
        attributes:
          scheduled_events: "{{ ev_list_t }}"
        icon: mdi:calendar'

The sensor has the following value:

scheduled_events: 
- start: '2024-08-09'
  end: '2024-08-10'
  summary: Grüne Tonne
- start: '2024-08-10T10:00:00+02:00'
  end: '2024-08-10T12:00:00+02:00'
  summary: 'Auto putzen '

icon: mdi:calendar'
friendly_name: Calendar Scheduled Events Tomorrow

As you can see “Grüne Tonne” has as start date today.

Do you have an idea how to fix this?

Thanks!

if i read it right, the code is correctly returning what you asked it to, and it should not have been any different 2 updates ago.

gruene tonne is an all day event that spans two days. it starts on 2024-08-09 (today) and it continues and includes tomorrow (2024-08-10). therefore your query for all events that occur tomorrow properly includes this because it is also happening tomorrow. if you only want to include events that start tomorrow, then you’d have to filter it out in your ev_list_t template. something like {% if value.events.start > today_at() + timedelta(days=1) %}… (warning, quick coded w/o testing, but hopefully it communicates the essence… and if you wrote the current template, i’m guessing this is enough to get you going)

To avoid false positives from today’s or the day-after-tomorrow’s all-day events, avoid “00:00:00”:

...
  action:
    - service: calendar.get_events
      data:
        start_date_time: "{{ today_at('0:01') + timedelta(days=1) }}"
        end_date_time: "{{ today_at('23:59') + timedelta(days=1) }}"
      target:
        entity_id: 
          - calendar.gemeinschaftskonto_gk
          - calendar.barbara_barbara
          - calendar.standardkalender_markus
      response_variable: calendars
....

I did exactly as you described @Didgeridrew , but I still get the all day event of the previous day:

scheduled_events: 
- start: '2024-08-10'
  end: '2024-08-11'
  summary: Test
  description: ' '
- start: '2024-08-11'
  end: '2024-08-12'
  summary: Test2
  description: ' '

icon: mdi:calendar'
friendly_name: Calendar Scheduled Events Tomorrow
- trigger:
    - platform: time_pattern
      minutes: /1
  action:
    - service: calendar.get_events
      data:
        start_date_time: "{{ today_at('0:01') + timedelta(days=1) }}"
        end_date_time: "{{ today_at('23:59') + timedelta(days=1) }}"
      target:
        entity_id: 
          - calendar.gemeinschaftskonto_gk
          - calendar.barbara_barbara
          - calendar.standardkalender_markus
      response_variable: calendars2
    - variables:
          ev_list_t: |
            {% set ns = namespace(cal_events=[]) %}
            {%- for key, value in calendars2.items() %}
              {%- for event in value.events %}
                {%- set ns.cal_events = ns.cal_events + [event] %}
              {%- endfor %}
            {%- endfor %}
            {{ ns.cal_events | sort(attribute='start') | list }}
  sensor:
      - name: Calendar Scheduled Events Tomorrow
        unique_id: calendar_scheduled_events_tomorrow
        state: "{{ ev_list_t | count }}"
        attributes:
          scheduled_events: "{{ ev_list_t }}"
        icon: mdi:calendar'

In my calendar it is a one day all day event.

I played around with your suggestion and the following works:

            {% set ns = namespace(cal_events=[]) %}
            {%- for key, value in calendars2.items() %}
              {%- for event in value.events %}
                {% if as_timestamp(event.start) >= as_timestamp(today_at() + timedelta(days=1)) %}  
                {%- set ns.cal_events = ns.cal_events + [event] %}
                {%- endif %} 
              {%- endfor %}
            {%- endfor %}
            {{ ns.cal_events | sort(attribute='start') | list }}

However I could have sworn that the old solution worked before. I display the number of events for today and number of events for tomorrow prominently in a dashboard and they were always correct in regards to the all day events and just a while ago they started to show wrong numbers.

I just opened a bug report for this: Calendar - get_events action returns all day events that at not in the period specified · Issue #124884 · home-assistant/core · GitHub

The problem seems to have something to do with timezones - funnnn!

For example, if I do:

action: calendar.get_events
target:
  entity_id:
    - calendar.family
data:
  start_date_time: "2024-08-29 00:00:00"
  end_date_time: "2024-08-29 23:59:59"

I get Response:

calendar.family:
  events:
    - start: "2024-08-30"
      end: "2024-08-31"
      summary: No School

My timezone is UTC-4, so I tested subtracting 4 hours from my end_date_time, so I set it to 19:59:59 and this fixes it.
If I move forward another hour (20:59:59), the next day’s events start showing up again.

My guess would be that all day calendar events(?) get interpreted as UTC?
2024-08-30 00:00:00 then becomes 2024-08-29 20:00:00 in my timezone. Then the filter is applied and catches it.