Calendar date offset

Heyho!

I get some problems to get one of the two templates working. I’m pretty sure I have to replace the “work” or in your case “treinbestuurder_be” with my calendar. But I don’t know how exactly. Replacing it with the calendar ID gives me an error, using the entityid of a calendar sensor makes the template sensor to always say “unavailable” even when there is no event planned.

Thanks for your help!

If you need more information I am able to share it.

You just replace the part

calendar.treinbestuurder_be

with calendar.whateverthenameis.

Did you check if your calendar sensor is even working?

Thanks for the quick response!

Yeah, that’s exaclty what I did, here’s my config:

sensor:
  - platform: time_date
    display_options:
      - 'time'
  - platform: template
    sensors:
      privatnachstesevent:
        friendly_name: Privat - Nächstes Event
        entity_id: sensor.time
        value_template: >-
          {% if states.calendar.calendar.attributes.start_time %}
            {{((as_timestamp(states.calendar.(...)_gmail_com.attributes.start_time) - as_timestamp(now())) / 60) | default(99) | int }}
          {%- else -%}
            0
          {%- endif %}

But the sensor doesn’t give me any information except “unavailable”, even when there is no entry in my calendar:

And yes, the calendar sensor is working correctly:

NVM, was a small mistake, sorry, but thanks for your help!

for my calendar titled appointments, I had to make these changes to get it to work:

sensor:
  - platform: time_date
    display_options:
      - 'time'
  - platform: template
    sensors:
      timetoleave:
        friendly_name: Time to Leave
        value_template: >-
          {% if states.calendar.appointments.attributes.start_time %}
            {{((as_timestamp(states.calendar.appointments.attributes.start_time) - as_timestamp(now())) / 60) | default(99) | int }}
          {%- else -%}
            0
          {%- endif %}

Many, many thanks to the above posters for this.
I’m relatively new to HA and writing my own automations and was stuck with parsing calendar data for just his kind of issue. I just wanted a quick reminder - on the day - that a football match was scheduled.
My original thought was to check the date of start_time against the date portion of {{now()}}, but my limited knowledge of syntax/command structure just floored me so I left it. Came back today with a different search-term which led me here.
Here’s what I ended up with: ( calendar is a google calendar )

#Footy Today
  - platform: template
    sensors:
      footy_today:
        friendly_name: "Footy Alert"
        value_template: >-
          {% if states.calendar.manchester_united.attributes.start_time %}
            {{((as_timestamp(states.calendar.manchester_united.attributes.start_time) - as_timestamp(now())) / 60)/60 | default(99) | int <12 }}
          {%- else -%}
           false
          {%- endif %}

The idea of the ‘<12’ is that triggering this as any time before 8pm on a matchday would result in the condition being true and therefore send the message. I run the automation @10:30am and therefore should pick up any kickoff up to 10pm the same day.

- id: '1635593344340'
  alias: Football Reminder
  description: Telegram reminder on the day of a match with time of match
  trigger:
  - platform: time
    at: '10:30'
  condition:
  - condition: state
    entity_id: binary_sensor.footy_today
    state: 'on'
  action:
  - service: notify.[MY-TELEGRAM_BOT]_tg_bot
    data:
      message: Footy today {{as_timestamp(strptime(state_attr("calendar.manchester_united",
        "start_time"),"" ))| timestamp_custom('%H:%M') }}
  mode: single
1 Like

Know this is an old one, am setting this up so I can set trigger x mins before school, x mins before school ends, then will do the same for work, have the following sensors set up which seem to work, only issue I seem to be getting is if I change start/finish time of an event on my google calendar, it updates on calendar integration within HA but the mins left on sensors dont seem to change until I restart server, is this normal? Any way to make it automatically update with new times without needing to do a restart? thanks

- platform: template

    sensors:

      timeuntilschool:

        friendly_name: Time until school

        value_template: >-

          {% if states.calendar.school.attributes.start_time %}

            {{((as_timestamp(states.calendar.school.attributes.start_time) - as_timestamp(now())) / 60) | default(99) | int }}

          {%- else -%}

            0

          {%- endif %}

  - platform: time_date

    display_options:

      - 'time'

  - platform: template

    sensors:

      timeuntilschoolends:

        friendly_name: Time until school ends

        value_template: >-

          {% if states.calendar.school.attributes.end_time %}

            {{((as_timestamp(states.calendar.school.attributes.end_time) - as_timestamp(now())) / 60) | default(99) | int }}

          {%- else -%}

            0

          {%- endif %}
1 Like

For all using this, we can now do this cleanly inside the calendar integration by just using offset :grinning:

automation:
  trigger:
    - platform: calendar
      # Possible values: start, end
      event: start
      # The calendar entity_id
      entity_id: calendar.personal
      # Optional time offset to fire a set time before or after event start/end
      offset: -00:15:00
2 Likes