Template: calendar.list_events -> calendar.get_events

Hey guys,

I use several templates to count calendar events for the actual day (as well adding the event titles to the attribute).

template.yaml

- trigger:
    platform: time_pattern
    minutes: /30
  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: "{{ (now() + timedelta(days=1)).strftime('%Y-%m-%d 00:00:00') }}"
    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

As announced the service ‘calendar.list_events’ is deprecated and will stop working in Home Assistant 2024.6. So I need to use ‘calendar.get_events’.

The output seems to be changed.

calender.list_events

events:
  - start: "2023-12-19T09:10:00+01:00"
    end: "2023-12-19T10:10:00+01:00"
    summary: Zahnarzt

calender.get_events

calendar.privat:
  events:
    - start: "2023-12-19T09:10:00+01:00"
      end: "2023-12-19T10:10:00+01:00"
      summary: Zahnarzt

I did not get it working with the calender.get_events service.
Can some one help me out here what I need to change in my template to get it working with the new service?

Thanks :slight_smile:

- trigger:
    platform: time_pattern
    minutes: /30
  action:
  - service: calendar.get_events
    target:
      entity_id: calendar.privat
    data:
      start_date_time: "{{ now().strftime('%Y-%m-%d 00:00:00') }}"
      end_date_time: "{{ (now() + timedelta(days=1)).strftime('%Y-%m-%d 00:00:00') }}"
    response_variable: scheduled_events_privat
  sensor:
  - name: Scheduled Events PRIVAT 24h
    unique_id: scheduled_events_privat_24h
    state: "{{ scheduled_events_privat['calendar.privat'].events | count() }}"
    attributes:
      scheduled_events_privat: "{{ scheduled_events_privat['calendar.privat'].events }}"
    icon: mdi:calendar
1 Like

oh wow, that was fast :star_struck:
works!
thank you very much @petro :handshake:t2:

oh, im not sure why but now i get an error and the sensor is not available anymore.

Logger: homeassistant.helpers.template
Source: helpers/template.py:2260
First occurred: 20:59:00 (5 occurrences)
Last logged: 21:00:00

Template variable error: ‘dict object’ has no attribute ‘calendar.privat’ when rendering ‘{{ scheduled_events_privat[‘calendar.privat’].events | count() }}’

Logger: homeassistant.helpers.sensor
Source: helpers/trigger_template_entity.py:208
First occurred: 20:59:00 (5 occurrences)
Last logged: 21:00:00

Error rendering state template for sensor.scheduled_events_privat_24h: UndefinedError: ‘dict object’ has no attribute ‘calendar.privat’

Sorry, didn’t change the service call from calendar.list_events to calendar.get_events. Make that change and it’ll work.

1 Like

For future reference:

1 Like

works now as promised :wink:
Thanks again :pray:t2:

Thank you, this was very helpful.
I want to format it a bit more

start - end summary | example: 10:00-11:00 Work
and put in place of the entity name, which would give me a list of all the events.
Is that possible?

That would have to be done in your notification.

perhaps you might find something here you can use: How to list multiple calendar event in a template - #128 by Didgeridrew

1 Like

Hi Petro, I think you helped me a year or so ago to get my automation going, however now its changed to get.events I can’t seem to get it right. I have an automation that gets the cal data for the last 24hrs and places it in a sensor that is in the templates directory, every morning piper gives me the cal events for the day I seem to be missing something? Not sure if there is a simpler way of doing it now with all the development, but this is what I have today. Any help would be appreciated.
Automation:

Sensor in templates directory