Calendar - Get Events Help

There are filter functions like selectattr and rejectattr that can be used to “edit” the list of events based on the values they contain.

...
attributes:
  events: >
    {% for event in agenda["calendar.my_calendar"]["events"] | rejectattr('summary', 'eq', 'Home') | list %}
      {{- (event.start | as_datetime | as_local).strftime("%H:%M") }}: {{ event.summary }}<br>
    {% endfor %}

One useful trick for calendar events is to select for : in the start time, since all-day events don’t have : in their start and end variable values.

...
attributes:
  events: >
    {% for event in agenda["calendar.my_calendar"]['events'] | selectattr('start', 'search', ':') | list %}
      {{- (event.start | as_datetime | as_local).strftime("%H:%M") }}: {{ event.summary }}<br>
    {% endfor %}

It could also be done as an inline if:

...
attributes:
  events: >
    {% for event in agenda["calendar.my_calendar"]["events"] if event.start is search(':') %}
      {{- (event.start | as_datetime | as_local).strftime("%H:%M") }}: {{ event.summary }}<br>
    {% endfor %}

The closest thing to an organized tutorial is Jinja code for Ninjas . Unfortunately, it hasn’t been updated in a while and it’s showing its age. The HA devs and community regularly add template functions, so there are often better/easier ways to do some of the examples you’ll find there, but it covers the basics well.

This year, the forum community has been working on creating and indexing useful examples and explanations which can be found in the Cookbook Index, but there’s still a lot of work to be done on it.

1 Like