Calendar date offset

As an alternative approach, I think you can use the offset: parameter to create a trigger before the actual event - although you need to change the event title.

I have only used it with the caldav component, so I don’t know if it works with the google calendar component.

I cannot enter an offset as its a calendar not made by me, only imported, with my daily work schedule
To get the offset, without the ‘offset’ title thingie :slight_smile:

  - condition: template
    value_template:'{% set diff = as_timestamp(states.calendar.treinbestuurder_be.attributes.start_time) -as_timestamp(now()) %}# create a timestamp of the calendar time attr and substract current time
                    {{ diff<32400 and diff > 18000  }}'  # compare difference in seconds, will match condition when true

Other solution is creating a sensor to use as trigger like Robdyi did in his post:

Hope this can be of any use to someone else!

2 Likes

Thanks for this, works great for me

I had to change to this to make it not appear on the actual day

{{ diff<32400 and diff>0 }}

1 Like

Hey @Wim_L
Can you help me with this? Im trying to create an automation that will trigger when an event is 3o minutes away. I also cant edit my work calendar.

How would I go about using this as a trigger, rather then a condition? Or am I misunderstanding something

Not sure if this helps but I use this sensor to tell me the time to the next event. I use it to get alexa to announce when we should be leaving the house.

  - platform: template
    sensors:
      timetoleavealexamin:
        friendly_name: Time to go Alexa min
        entity_id: sensor.time
        value_template: >-
          {% if states.calendar.calendar.attributes.start_time %}
            {{((as_timestamp(states.calendar.xxxxxgmailcom.attributes.start_time) - as_timestamp(now())) / 60) | default(99) | int }}
          {%- else -%}
            0
          {%- endif %}
1 Like

Youre the best. This actually does exactly what I need but in a better fashion. Thank you!

Wondering if I screwed something up. I assume the state is the minutes until the next event? It doesnt seem to be updating and just showing 4 even though the event is in 1 minute.

- platform: template
  sensors:
    upcoming_event:
        friendly_name: Time Until Next Event
        entity_id: sensor.time
        value_template: >-
          {% if states.calendar.work.attributes.start_time %}
            {{((as_timestamp(states.calendar.work.attributes.start_time) - as_timestamp(now())) / 60) | default(99) | int }}
          {%- else -%}
            0
          {%- endif %}

NVM. needed sensor.time. Works perfectly now!

Hello, I am trying to do a automated heating for the office. It schuld trigger 2h before the Google Calendar event starts. It would be very helpful to see a full automation (trigger, condition, action). I would highly appreciate if @halpplz or @RobDYI could help me out here and share the complete working code. Furthermore I am not sure how to create the sensor timetoleavealexamin / upcoming_event. Thanks in advance and have a nice day!

You need to create a google calendar, timetoleave sensor, a sensor.time and an automation, all in your configuration yaml.


google:
 client_id: !secret client_id
 client_secret: !secret client_secret

sensor:
  - platform: time_date
    display_options:
      - 'time'
  - platform: template
    sensors:
      timetoleavealexamin:
        friendly_name: Time to go Alexa min
        entity_id: sensor.time
        value_template: >-
          {% if states.calendar.calendar.attributes.start_time %}
            {{((as_timestamp(states.calendar.work.attributes.start_time) - as_timestamp(now())) / 60) | default(99) | int }}
          {%- else -%}
            0
          {%- endif %}
automation event_in_120min:
  alias: Event in 120 minutes for Google Calendar
  trigger:
    platform: state
    entity_id: sensor.timetoleavealexamin
    to: '120'
  action:
6 Likes

:partying_face: Thank you so much. Now I have it also working!
And I also find it very genius using the tts to announce events - Thanks for the inspiration as well as the help @RobDYI

Would this also be possible to do with Node Red?
I want to trigger everything based on my calendar!

I don’t know how to template things with Node Red

Ofcourse, make a template sensor like the one @RobDYI did a few posts above, and use that in nodered.

So the sensor is going to on 60 minutes before start next event?
I don’t understand.
Is this also working with CalDav?

I’m trying to do this with CalDav and Node Red but still without result :disappointed:!

I was having a little trouble understanding this for a while, but I get it now…This is for anyone else who is a little dense with this stuff.

The template sensor that RobDYI suggests creates a sensor that gives the number of minutes until the next calendar event in the calendar feed you provide. Then you create an automation (through lovelace, yaml, or node red) and specify how many minutes before the event you want to trigger your automation.

This works perfect.
I just removed the “entity_id: sensor.time” line completely, since it’s deprecated in HA now and not needed anymore.

Mine takes about 15 minuttes or so from then I add a new event in the calendar/change an event to the sensor “timetoleavealexamin” updates, is this normal?

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!