Calendar: entry longer ago than x from today = trigger (or template sensor?)

Hi everybody,

I have an automation that will create a calendar entry when pressing a physical button. This is entry is always named đźš— in garage.

Let’s say we have an input_number set to 7 and an input_datetime set to 12:30.

How can I trigger an automation at 12:30 if the most recent car in garage entry has occurred longer than 7 days ago? Ideally without actually considering the current time, just the day.

Example: today is May 19th, the automation will fire at 12:30. If most recent calendar entry I am looking for was on May 15th, do nothing (because it’s been less than 7 days). If the entry is dated May 12th, 16:30, do trigger the notification (even though May 19th 12:30 to May 12th 16:30 is not yet 7 days ago, but May 19th to May 12th without considering the exact time is 7 days ago in this case).

I got this so far

service call:

service: calendar.list_events
data:
  start_date_time: "{{ now() - timedelta(days=7) }}"
  end_date_time: "{{ now() }}"
target:
  entity_id: calendar.garage

reply:

events:
  - start: "2024-05-14T08:48:29+02:00"
    end: "2024-05-14T09:08:29+02:00"
    summary: đźš— outside
    description: ""
  - start: "2024-05-14T11:53:29+02:00"
    end: "2024-05-14T12:13:29+02:00"
    summary: đźš— car in garage
    description: ""

So we are looking for the second entry, because only that one matches đźš— car in garage. But how do I continue?

Since there might be different titles (i.e., đźš— outside), the automation needs to go through each entry first to determine which events summary matches; then it needs to pick either the start or end date, check whether it has been 7 days, and then (if true) notify me.

It does no matter which detail (start/end) is picked. Start is always 10 minutes before pressing the physical button to create the event, end is always 10 minutes after. It’s just to get a time frame in there.

Perhaps this could even be a template sensor? So this template sensor would just be a number, namely the number of days from today looking back to the last calendar entry (again, not precisely counting seconds, just the amount of days; even if the car has technically just been in the garage for 6 days and 4 hours, I’d like this number to be 7 already).

I cannot figure this out, but I’m sure it is simple for those jinja2 ninjas here :slight_smile:
Thank you in advance for your help

What is the question that you are actually asking in the condition?

Is it

  • “Has the car been in the garage at all in the last 7 days?”
  • “Has the car been in the garage at any time prior to 7 days ago?”
  • “Has the car been in the garage continuously for at least 7 days?”
  • Something else entirely?

It is “Has the car been in the garage at all in the last 7 days?”.

If this is the case, HA will send a notification like “please get car out of garage”.

trigger:
  - platform: time
    at: "12:30:00"
condition: []
action:
  - service: calendar.get_events
    data:
      start_date_time: "{{ today_at() - timedelta(days=7) }}"
      end_date_time: "{{ now() }}"
    target:
      entity_id: calendar.garage
    response_variable: agenda
  - condition: template
    value_template: |
      {% set events = agenda['calendar.garage']['events'] %}
      {{ events | selectattr('summary', 'eq', 'đźš— car in garage') | list | count == 0 }}
  - service: notify.example
    data:
      message: Please get car out of garage

EDIT: Template modified to address OP’s updated requirement.

1 Like

Thank you. Unfortunately, this doesn’t work. When I trace the automation, it always sees the template as false.

I just realized, we need to go the other way. If the summary has not happened in the last 7 days, I need to run the notification service.

I tried using list | count == 0 }} instead, but that didn’t work, either.

Sorry for the confusion. What I meant / need is this:

check the calendar for the past X days; if the summary does not contain the car trigger, then send a notification.

So basically, if the car has not been in the garage in this time frame, notify me.

Sorry, still not working.

Can we try a different approach?

If there were a way to simply count backwards from today to the last time the summary has occurred, then put this in a template sensor… then I could simply use this template sensor as a trigger and also display on the dashboard, when it’s been the last time the car was in the garage.

So change two things

  • instead of checking whether the event has been in the calendar in the past x days, check how long ago was the most recent event
  • instead of sending a notification, write this value in a template sensor as int; so if it’s been ten days, set the sensor to 10

this way, not only can I check this sensor every day at time x, but also have it on the garage dashboard.

template:
  - trigger:
      - platform: time_pattern
        hours: /1
      - platform: homeassistant
        event: start
    action:
      - service: calendar.get_events
        data:
          start_date_time: "{{ today_at() - timedelta(days=60) }}"
          end_date_time: "{{ now() }}"
        target:
          entity_id: calendar.garage
        response_variable: agenda
    sensor:
      - name: Days since Car in Garage Event
        state: |
          {% set events = agenda['calendar.garage']['events'] | selectattr('summary', 'eq', 'đźš— car in garage') | list %}
          {% set recent = events | sort(attribute='start', reverse=1) | first | default({'start':today_at()}, 1) %}
          {{ (now() - recent['start'] | as_datetime | as_local).days }}
1 Like

Thank you!

I switched to “contains” instead of “eq” and removed the car symbol. Now it seems to work :slight_smile: