Days until event I google Calendar

Hi!

How can I do a sensor that gives me days or hours (not both) until the next event in a google calendar? Someone that can help me?

Thanks in advances

Here’s a guide for days.

2 Likes

Thanks foe you answer and of course this works. But it’s not really what I am looking for. I am looking for just a number of days or hours. 1, 2, 3 etc to ba able to use it in conditions etc

Then just change the template to output days an use the template in your condition.

The reason I asked in the forum is because I don’t know how to write templates, so for many it’s probably just to change and you describe but it’s not easy when you don’t know how to. That’s way I asked :wink:

But I hear you, searching elsewhere

1 Like

But if you can’t write a template, how are you going to create a condition that uses the value? You’ve provided little to no information other than “I want the number of days”. Even if I were to write the condition template for you, you’d still need to manipulate it to your needs (that you haven’t stated).

Sorry it wasn’t at al my intention to step on your tows, my bad.

But I don’t rely follows your logic here, just because I can’t write Jinja2 doesn’t mean I can’t use condition card in LL, write a automation in yaml or GUI. So what I asked for help with was the template sensor in Jinja2 that says how many days it’s until next event in a calendar. With that I would have made a template sensor that got a state like 1 or 2. So what shouldn’t I use the sensor for a condition in say LL. Show this markdown card if this sensor is 2

Sorry I didn’t give you all the information you needed, but before you go to hard on me i deleted the post not long after I posted it and before anyone answered it because I I got help and solved it in a little different way.

But this doesn’t rely help or support anyone and toke another direction so will delete it again.

Thanks for your time anyway @petro

Not going hard on you, just trying to get this information:

There are about 800 ways to do a condition. And when you want to get number of days until a calendar event, you need a template. Meaning you can only use a template condition or a template sensor to get that information out. This is all I was trying to get out of you. No one here is a mind reader. You could have wanted to get the number of days to your calendar event and then add 10 days for all I know.


This sensor here will provide number of days until an event, where 0 is today.

sensor:
  - platform: time_date
    display_options:
      - 'date'

  - platform: template
    sensors:
      event_day:
        friendly_name: Event Day
        entity_id: sensor.date
        unit_of_measurement: days
        value_template: >
          {% set midnight = now().replace(hour=0, minute=0, second=0, microsecond=0).timestamp() %}
          {% set event = states('calendar.xxx') | as_timestamp %}
          {{ ((event - midnight) // 86400) | int }}
4 Likes

Could anyone help me modify this to be used as a holiday scheduler for an alarm automation. I’ve got it working that it detects an event but I would like it to only detect if the message is “Holiday” rather than every next event as I may put “Half Day” in this field as well and I don’t want those shown

This is my config entry

      work_holiday:
        friendly_name: "Work Holiday Countdown"
        value_template: >
          {% set midnight = today_at() %}
          {% set event = state_attr('calendar.work_holidays','start_time') | as_datetime | as_local %}
          {% set delta = event - midnight %}
          {% if delta.days == 0 %}
            Today
          {% elif delta.days == 1 %}
            Tomorrow
          {% elif delta.days == 2 %}
            Day After Tomorrow
          {% else %}
            In {{ delta.days }} Days
          {% endif %}

Thank you

1 Like

It’s not possible. Calendars only show the next event. So your # of days calculation will be incorrect as you cant look beyond the next event.

Aaaah right that makes sense, I’ve been trying for hours and got nothing lol!

If you install the Hass Calendar Add-on you can access future events from Google and CalDav calendars.

template:
  - sensor:
      - unique_id: work_holiday
        name: "Work Holiday Countdown"
        state: >
          {% set event = state_attr('sensor.work_holidays', 'data')
          | selectattr('summary', 'search', 'Holiday')
          | map(attribute='startDateISO') | map('as_datetime')
          | map('as_local') | first %}
          {% set delta = event - today_at() %}
          {% if delta.days == 0 %}
          Today
          {% elif delta.days == 1 %}
          Tomorrow
          {% elif delta.days == 2 %}
          Day After Tomorrow
          {% else %}
          In {{ delta.days }} Days
          {% endif %}
1 Like

I have a local calendar with entries for several types of wast: GFT, Rest and Paper.
I like to use one calendar for all these types of waste instead of one calender for each so I tried the above solution but I get an error.

My code is:

  - platform: template
    sensors:
      afval_counter:
        friendly_name: "Afval Countdown"
        value_template: >
          {% set midnight = today_at() %}
          {% set event = state_attr('calendar.afval', 'data') | selectattr('summary', 'search', 'GFT') | map(attribute='startDateISO') | map('as_datetime') | map('as_local') | first %}
          {% set delta = event - midnight %}
          {% if delta.days == 0 %}
            Vandaag
          {% elif delta.days == 1 %}
            Morgen
          {% elif delta.days == 2 %}
            Overmorgen
          {% else %}
            In {{ delta.days }} Dagen
          {% endif %}

In the error log i find:

Error while processing template: Template<template=({% set midnight = today_at() %} {% set event = state_attr(‘calendar.afval’, ‘data’) | selectattr(‘summary’, ‘search’, ‘GFT’) | map(attribute=‘startDateISO’) | map(‘as_datetime’) | map(‘as_local’) | first %} {% set delta = event - midnight %} {% if delta.days == 0 %} Today {% elif delta.days == 1 %} Tomorrow {% elif delta.days == 2 %} Day After Tomorrow {% else %} In {{ delta.days }} Days {% endif %}) renders=2>
Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 568, in async_render
render_result = _render_with_context(self.template, compiled, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 2198, in _render_with_context
return template.render(**kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/site-packages/jinja2/environment.py”, line 1301, in render
self.environment.handle_exception()
File “/usr/local/lib/python3.11/site-packages/jinja2/environment.py”, line 936, in handle_exception
raise rewrite_traceback_stack(source=source)
File “”, line 1, in top-level template code
jinja2.exceptions.UndefinedError: No first item, sequence was empty.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 694, in async_render_to_info
render_info._result = self.async_render(
^^^^^^^^^^^^^^^^^^
File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 570, in async_render
raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: No first item, sequence was empty.

All help is appreciated.

Local Calendar has evolved rapidly, you can now get future events using the calendar.list_events service call. This can even be done in a trigger-based template sensor.

template:
  - trigger:
      - platform: time
        at: "00:00:00"
    action:
      - service: calendar.list_events
        data:
          duration:
            hours: 144  
        target:
          entity_id:
            - calendar.afval
        response_variable: agenda
    sensor:
      - name: "Afval Countdown"
        state: >
          {% set midnight = today_at() %}
          {% set event = agenda.events | selectattr('summary', 'search', 'GFT') | map(attribute='start') | map('as_datetime') | first %}
          {% set delta = (event - midnight).days %}
          {% if delta == 0 %}
            Vandaag
          {% elif delta == 1 %}
            Morgen
          {% elif delta == 2 %}
            Overmorgen
          {% else %}
            In {{ delta }} Dagen
          {% endif %}
1 Like

I tried to make use of this code, but it does not work. Any suggestions?

template:
  - trigger:
      - platform: time_pattern
        minutes: "/10"
    action:
      - service: calendar.list_events
        data:
          duration:
            hours: 144
        target:
          entity_id:
            - calendar.home_assistant
        response_variable: agenda
    sensor:
      - name: "Afval PMD"
        state: >
          {% set midnight = today_at() %}
          {% set event = agenda.events | selectattr('summary', 'search', 'PMD-zak') | map(attribute='start') | map('as_datetime') | first %}
          {% set delta = (event - midnight).days %}
          {% if delta == 0 %}
            Vandaag
          {% elif delta == 1 %}
            Morgen
          {% elif delta == 2 %}
            Overmorgen
          {% else %}
            Over {{ delta }} dagen
          {% endif %}

When I run the list_events service manually i get:

events:
  - start: "2024-02-14"
    end: "2024-02-15"
    summary: Groene container
    description: De groene container wordt elke woensdag geleegd.
  - start: "2024-02-14"
    end: "2024-02-15"
    summary: PMD-zak
    description: PMD wordt elke woensdag ingezameld.
1 Like