Calendar date offset

So I’m trying to create a notification that pings me 9 hours before I have to go to work(so it makes me go to bed in time ;))

The problem is i have no idea how to substract 9 hours from the start_time. This is what i got so far(the -9h is ofc not working:)).

### WORK NOTIFICATIONS ###

- alias: Work Notifications
  trigger:
    platform: time
    minutes: /10

  condition:
    condition: and
    conditions:
      - condition: template
        value_template: '{{ calendar.treinbestuurderbe.attributes.start_time - 9h}}'
      - condition: time
        after: '19:00:00'
        before: '23:30:00  
      - condition: state
        entity_id: input_boolean.sleep_mode
        state: off

   action:
    service: notify.allnotifiers
    data:
      title: "Bedtime!"
      message: "Werken binnen 9u. Huphup Naar Bed!"
1 Like

So, I’m guessing you’re using the Google Calendar Event component. Yes?

If so, I’m not familiar with it, nor ever used it, but from what I read in the docs, wouldn’t it be better to use an offset string in the title to get a pre-trigger? Something like !!-540 to get a pre-trigger 540 minutes (or 9 hours) before the start time? If it works the way I think it does, then you could use this for your trigger:

  - alias: Work Notifications
    trigger:
      platform: template
      value_template: "{{ is_state_attr('calendar.treinbestuurderbe', 'offset_reached', 'on') }}"
1 Like

It would be easier but impossible, its imported from work;)
I have found a possible solution,

Will test tonight

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?