Template Sensor and Calendar Filtering help

I currently have a template that grabs calendar events from a Google Calendar. If there is only 1 event in the calendar it works great. If there are more than 1, not as great. There are (4) entries ; Neave Special, Religion, Lunch and random topics.
Somedays there are 1, no problem; when there are (2) or more, problems arise. It may not always be the same (2) so I cannot use the [0], [1], etc to parse out the event.

Here is the template:

  - trigger:
    - platform: time_pattern
      minutes: /30
    action:
      - service: calendar.list_events
        target:
          entity_id: calendar.neave
        data:
          duration:
            hours: 24
        response_variable: scheduled_events
    sensor:
      - name: Neave Scheduled Events
        unique_id: neave_scheduled_events
        state: "{{ scheduled_events.events | count() }}"
        attributes:
          scheduled_events: "{{ scheduled_events.events }}"
        icon: mdi:calendar

and here is what it typically spits out:

scheduled_events:
  - start: '2023-10-24'
    end: '2023-10-25'
    summary: Neave Special
    description: Wear a jersey day
  - start: '2023-10-25'
    end: '2023-10-26'
    summary: Religion
    description: Religion Class Today
icon: mdi:calendar
friendly_name: Neave Scheduled Events

Is there a way to parse/filter these, so each of the topics has it’s own entity that I can use in an announcement sensor for the next day. currently it will only take today’s instance.

Thank you!

for anyone looking, this seems to work (one for each of the items above)…

  - trigger:
    - platform: time_pattern
      minutes: /30
    action:
      - service: calendar.list_events
        target:
          entity_id: calendar.neave
        data:
          duration:
            hours: 24
        response_variable: neave_special_today
      - variables:
          neave_filtered_special: >
            {{ neave_special_today.events | selectattr ('summary', 'search', 'Neave Special') | list }}
    sensor:
      - name: Neave Extra Special Today
        unique_id: neave_extra_special_today
        state: "{{ neave_filtered_special | count() }}"
        attributes:
          scheduled_events: "{{ neave_filtered_special }}"
        icon: mdi:calendar
1 Like

Thank you for sharing your code which works fine. In my case the sensor spits out the following:

scheduled_events: 
- start: '2024-01-05'
  end: '2024-01-06'
  summary: Abfuhr blaue Papiertonne
  description: Die Abholung erfolgt ab 6:00 Uhr
  location: "***"

icon: mdi:calendar
friendly_name: Papiertonne Termine

Now I try to extract the start time from that event as I want to count the days from now until that event starts. Do you have any idea how I can do that?

Are you looking for a certain range from now? My case is today and tomorrow, so I have something like that, but not an event 3 days or more from now.

Thanks for your reply. I‘ve already found a solution. I extract the start time from the output of the above sensor with

state_attr('sensor.papiertonne_termine', 'scheduled_events')[0].start

For counting the days I use the macro easy_time.jinja. So my whole code looks like this:

{% from 'easy_time.jinja' import weekday %} {% from 'easy_time.jinja' import speak_the_days %} {% from 'easy_time.jinja' import count_the_days %}
{% set start_time = state_attr('sensor.papiertonne_termine', 'scheduled_events')[0].start %} {% set date_object = as_datetime(start_time) %} {% set letzte_leerung = states('input_datetime.papiertonne_letzte_leerung') %}
 {% set letzte_leerung_tage = count_the_days(letzte_leerung) | int *-1 %}


{% if 1 < count_the_days(start_time) | float %}
Leerung: vor {{ letzte_leerung_tage }} Tagen, nächste in {{ count_the_days(start_time) }} Tagen ({{ speak_the_days(start_time) }}).
{% elif 1 == count_the_days(start_time) | float %}
Leerung: vor {{ letzte_leerung_tage }} Tagen, nächste {{ speak_the_days(start_time) }} ({{ weekday("start_time") }}).
{% else  %}
Leerung: {{ speak_the_days(start_time) }}.
{% endif %}
1 Like