How to list multiple calendar event in a template

Sorry, I’m still in the process of thinking on how to implement my ePaper Calendar.
Would it be possible to convert it into a string in the process? So for example a string like this:
“Startdate-Starttime-Enddate-Endtime-Subject-Calendarname;”
→ Then I could append all strings, transfert that to the esp, split it there and show it on the display?

Did you get the answer for this question?. I am in the the same process as yours.
I have a ecalendar in a epaper display run by an ESP32. I communicate with HA through MQTT. I am trying to find a way to convert the calendar events into a JSON (?) to send it through the MQTT.

Unfortunatley no. I’m struggeling to get the touchscreen working of my epaper display. This is more or les on hold right now. Nevertheless, if you find a way I’m more than happy to hear it!

Thank you for this wonderful streamlined way to get the results from multiple calendars.

I am successfully using the calendar.get_events service call to retrieve events from a number of calendars. Then, I format the information and send it along to an epaper dashboard. So far, all is good.

However, I would like to do some formatting next to the event depending on which calendar generated it. For example perhaps I put a little star icon next to the events that come from the “family” calendar.

As I understand the docs, get_events returns summary, description, start, end, location.

In situations where more than one calendar is targeted, can the returned data include the calendar_id?

Not without heavy modifications to the template, or just output the raw response.

I have this template:

- trigger:
    - platform: time_pattern
      minutes: /1
  action:
    - service: calendar.get_events
      data:
        duration:
          hours: 2160
          minutes: 0
          seconds: 0
        start_date_time: "{{ today_at() }}"
      target:
        entity_id:
          - calendar.booking1
          - calendar.airbnb1
      response_variable: calendars
    - variables:
        ev_list: |
          {% 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: reservas
      unique_id: reservas
      state: "{{ ev_list | count }}"
      attributes:
        scheduled_events: "{{ ev_list }}"
      icon: mdi:calendar'

and I get the following values:

scheduled_events:
  - start: "2024-09-03"
    end: "2024-09-04"
    summary: a1
  - start: "2024-09-03"
    end: "2024-09-04"
    summary: b1
  - start: "2024-09-04"
    end: "2024-09-06"
    summary: b2
  - start: "2024-09-20"
    end: "2024-09-21"
    summary: a2
icon: mdi:calendar'
friendly_name: reservas

How can I exclude repeated blocks of “star” values?
I want this:

scheduled_events:
  - start: "2024-09-03"
    end: "2024-09-04"
    summary: a1
  - start: "2024-09-04"
    end: "2024-09-06"
    summary: b2
  - start: "2024-09-20"
    end: "2024-09-21"
    summary: a2
icon: mdi:calendar'
friendly_name: reservas

Thanks

1 Like
{%- set ns = namespace(unique_ev_list=[]) %}
{%- for item in ev_list %}
{%- if item.start not in ns.unique_ev_list|map(attribute='start')|list %}
{%- set ns.unique_ev_list = ns.unique_ev_list + [item] %}
{%- endif %}
{%- endfor %}
{{ ns.unique_ev_list }}

Hi,

this solution worked for me fine.
But since this October may template didn’t show any events.
I didn’t change anything in the configuration.yaml.
I think the problem starts with the core update 2024.9.3
What have I missed?

post your template

here is my template:

calendar:
  - platform: caldav
    username: !secret icloud_user
    password: !secret icloud_password
    url: https://caldav.icloud.com
    calendars:
      - Spielplan_U17_24-25
      - Spielplan_Damen_24-25
      - Spielplan_U20_24-25
template:
  - trigger:
      - platform: time_pattern
        minutes: /1
    action:
      - service: calendar.get_events
        data:
          duration:
            days: 9
        target:
          entity_id:
            - calendar.spielplan_u17_24_25
            - calendar.spielplan_damen_24_25
            - calendar.spielplan_u20_24_25
        response_variable: agenda
      - variables:
          ev_list: |
            {% set ns = namespace(cal_events=[]) %}
            {%- for key, value in agenda.items() %}
              {%- for event in value.events %}
                {%- set ns.cal_events = ns.cal_events + [event] %}
              {%- endfor %}
            {%- endfor %}
            {{ ns.cal_events | sort(attribute='start') | list }}

  #             {{ agenda.values() | map(attribute='events')
  #               | sum(start=[]) | sort(attribute='start') }}

  - sensor:
      - name: Naechste_Spiele
        unique_id: "naechste_spiele"
        state: "{{ ev_list | count }}"
        attributes:
          list: "{{ ev_list }}"
          test: "0.10"
        icon: mdi:calendartype or paste code here

Everything looks fine, are there errors in your logs?

no, There are no errors or warnings in the log for this sensor.

and what does the state look like then?

then it’s working, and you have no events

But I know, there are events. The Calendar-Card in the dashboard shows the events:

Verify the template works in the template editor and the get_events service call are getting events

Remove the commented out lines or use Jinja comment delimiters {# #}, since you are using multi-line quote indicators.

Edited config.
calendar:
  - platform: caldav
    username: !secret icloud_user
    password: !secret icloud_password
    url: https://caldav.icloud.com
    calendars:
      - Spielplan_U17_24-25
      - Spielplan_Damen_24-25
      - Spielplan_U20_24-25
template:
  - trigger:
      - platform: time_pattern
        minutes: /1
    action:
      - service: calendar.get_events
        data:
          duration:
            days: 9
        target:
          entity_id:
            - calendar.spielplan_u17_24_25
            - calendar.spielplan_damen_24_25
            - calendar.spielplan_u20_24_25
        response_variable: agenda
      - variables:
          ev_list: |
            {% set ns = namespace(cal_events=[]) %}
            {%- for key, value in agenda.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: Naechste_Spiele
        unique_id: "naechste_spiele"
        state: "{{ ev_list | count }}"
        attributes:
          list: "{{ ev_list }}"
          test: "0.10"
        icon: mdi:calendar

service call get_events works in dev-tools and copied this output into the template editor I get the error: ‘ev_list’ is undefined

the variable ‘ev_list’ isn’t get by the sensor.

Then there would be errors in your logs about that