Problem with calendar

hello everyone,

I made a sensor for a calendar. The code looks like this:

- trigger:
      - platform: time_pattern
        seconds: /10
        
      - platform: homeassistant
        event: start
  action:
      - variables:
            Person: |
                {{'bart'}}
      - action: calendar.get_events
        data:
          start_date_time: |
            {{(now() + timedelta(days = -7)).strftime('%Y-%m-%d %H:%M:%S')}}
          end_date_time: |
            {{(now() +  timedelta(days = 365)).strftime('%Y-%m-%d %H:%M:%S')}}  
        target:
          entity_id: |
            calendar.agenda_{{Person}}
        response_variable: agenda
      - variables: 
            Jaar_Events_Unsort: |
                {{ agenda['calendar.agenda_' + Person].events }} 
            Jaar_Events: |
                {% for event in agenda['calendar.agenda_' + Person].events|sort(attribute='start') %}
                start: {{ (event.start|as_datetime).strftime('%d-%m-%Y %H:%M') }} - {{ event.summary }}
                einde: {{ (event.end|as_datetime).strftime('%d-%m-%Y %H:%M') }} - {{ event.summary }}
                {% endfor %}
            Vandaag_Events: |
                {{ Jaar_Events_Unsort   
                    | selectattr('start', 'search', as_timestamp(now())| timestamp_custom('%Y-%m-%d'))
                    | selectattr('end', 'search', as_timestamp(now())| timestamp_custom('%Y-%m-%d'))
                    | list}}
            Morgen_Events: |
                {{ Jaar_Events_Unsort   
                    | selectattr('start', 'search', (now() + timedelta(days = 1)).strftime('%Y-%m-%d'))
                    | selectattr('end', 'search', (now() + timedelta(days = 1)).strftime('%Y-%m-%d'))
                    | list}}         
  sensor:
      - name: |
            Agenda Bart
        unique_id: Agenda-Bart
        state: |
            {{Vandaag_Events|count()}}
            
        attributes:
            Vandaag_Events: |
                {{Vandaag_Events}} 
            Morgen_Events: |
                {{Morgen_Events}}    
            Jaar_Events: |
                {{Jaar_Events}}     
            

This actually works very well. But what I find strange is that if an activity starts today (22-03-2026)at 22:00 and lasts until 00:30 tomorrow (23-03-26), for example, it no longer appears in today’s (Vandaag_Events) activities. Where is the error here?

thank you for your response

Your selection criteria requires that the end be in the current day…

| selectattr('end', 'search', as_timestamp(now())| timestamp_custom('%Y-%m-%d'))

… so the event that goes from 22:00-00:30 gets rejected.

Thank you for your response.

Is there a solution for this? If so, how? I can’t figure it out.

What I actually mean is that, for example, I still list the activities that last until 03:00 under today’s date.

It depends on whether you want to include all events that start today, no matter when they end or if you want to set a cutoff time for things that start today but end tomorrow.

If you want to keep everything, just remove that second selectattr filter.

Vandaag_Events: |
  {{ Jaar_Events_Unsort   
    | selectattr('start', '>=', today_at().isoformat() )
    | list}}

For a cutoff, you would change the test the filter uses, for example to cut it off at events that end before 05:00 tomorrow you could use:

Vandaag_Events: |
  {{ Jaar_Events_Unsort   
    | selectattr('start', '>=', today_at().isoformat() )
    | selectattr('end', '<=', (today_at('5:00') + timedelta(days=1)).isoformat() )
    | list}}

Thank you for your response. I will try it shortly.

Great, this works perfectly. I always try out a lot of things, but sometimes I just don’t know what to do anymore. Fortunately, there are people like you who really know a lot about it.

Actually, I have another quick question. If I want to set this up for this one as well

Morgen_Events: |
            {{ Jaar_Events_Unsort   
                | selectattr('start', 'search', (now() + timedelta(days = 1)).strftime('%Y-%m-%d'))
                | selectattr('end', 'search', (now() + timedelta(days = 1)).strftime('%Y-%m-%d'))
                | list}} 

How do I do it? (I’ve tried everything, but I can’t get it to work.) :innocent:

You need to apply a timedelta() to the first selection as well as the second.

Morgen_Events: |
  {{ Jaar_Events_Unsort   
    | selectattr('start', '>=', (today_at() + timedelta(days=1)).isoformat() )
    | selectattr('end', '<=', (today_at('5:00') + timedelta(days=2)).isoformat() )
    | list}}

Sorry, but this isn’t working for me. My sensor becomes unavailable and I don’t see why?

- trigger:
      - platform: time_pattern
        seconds: /10
        
      - platform: homeassistant
        event: start
  action:
      - variables:
            Person: |
                {{'bart'}}
      - action: calendar.get_events
        data:
          start_date_time: |
            {{(now() + timedelta(days = -7)).strftime('%Y-%m-%d %H:%M:%S')}}
          end_date_time: |
            {{(now() +  timedelta(days = 365)).strftime('%Y-%m-%d %H:%M:%S')}}  
        target:
          entity_id: |
            calendar.agenda_{{Person}}
        response_variable: agenda
      - variables: 
            Jaar_Events_Unsort: |
                {{ agenda['calendar.agenda_' + Person].events }} 
            Jaar_Events: |
                {% for event in agenda['calendar.agenda_' + Person].events|sort(attribute='start') %}
                start: {{ (event.start|as_datetime).strftime('%d-%m-%Y %H:%M') }} - {{ event.summary }}
                einde: {{ (event.end|as_datetime).strftime('%d-%m-%Y %H:%M') }} - {{ event.summary }}
                {% endfor %}
            Vandaag_Events: |
                  {{ Jaar_Events_Unsort   
                    | selectattr('start', '>=', today_at().isoformat() )
                    | selectattr('end', '<=', (today_at('3:00') + timedelta(days=1)).isoformat() )
                    | list}}
            Morgen_Events: |
                  {{ Jaar_Events_Unsort   
                    | selectattr('start', '>=', (today_at() + timedelta(days=1)).isoformat() )
                    | selectattr('end', '<=', (today_at('3:00') + timedelta(days=2)).isoformat() )
                    | list}
            
                             
  sensor:
      - name: |
            Agenda Bart
        unique_id: Agenda-Bart
        state: |
            {{Vandaag_Events|count()}}
            
        attributes:
            Vandaag_Events: |
                {{Vandaag_Events}} 
            Morgen_Events: |
                {{Morgen_Events}}
            Jaar_Events: |
                {{Jaar_Events}}     

You’re missing a } at the end of the variables.

Damn, :rage: I hadn’t seen it. Thanks for your reply. It works now. :+1: