Simple Google Calender Event Offset

switched sections as advised by @rpitera original post below

Hey there,
im just started diving into this whole home automation / home assistant stuff. Since I only have a switch and a shutter as hardware components for now, I’ve been looking into the other sensors like Google Calendar Event integration. Like many other people, I am not a fan of the offset feature that requires the data to be in the calendar title, especially since im subscribing to calendars that I cannot edit. I spent quite some time researching on this forum and on GitHub repositories for a workaround, but did not find a complete solution that worked for me.

So after putting together the bits and pieces I found, I feel like I have a very beginner friendly offset script. Since more than a few people seemed to be looking for this, I wanted to share this to spare some other beginners the research work.

SENSORS

 sensor:
   - platform: time_date
     display_options:
       - 'date_time'
   - platform: template
     sensors:
      cal_alert_time:
        entity_id: sensor.date__time
        value_template: '{{ (as_timestamp(states.calendar.abfallkalender.attributes.start_time) - as_timestamp(now()) ) }}'
        friendly_name: 'Alert Time'

I created a time_date sensor, which refreshes once every minute. This is needed to have a constant refresh on our cal_alert_time sensor
The cal_alert_time sensor calculates the time between the event start and right now in seconds and is refreshed every minute due to the sensor.date__time

AUTOMATION

- alias: Calendar Alert
  initial_state: true
  trigger:
    - platform: numeric_state
      entity_id: sensor.cal_alert_time
      below: '21600'
      above: '21500'
  action:
    - service: notify.SMTP_Notifier
      data:
          title: 'Reminder: {{states.calendar.YOURCALENDAR.attributes.start_time}} {{states.calendar.YOURCALENDAR.attributes.message}}'
          message: ' '

A simple numeric_state trigger that is executed, when we hit the desired offset (value between below and above).In this case, we would be reminded 15 minutes (60 seconds * 15 = 900) before the event starts via email.
The ‘above’ statement is only there to make sure we dont get the mail again after a HA restart, not sure its necessary.

I hope this helps maybe one or two beginners like me somewhere down the road. Sorry for the formatting, the auto format in here somehow mixes things up.

Feel free to point out mistakes or possible improvements.

4 Likes

In regards to the formatting; delete the code you posted here and copy and paste it in again. Then select the code block, and press the preformatted text button as indicated in the image below:

I would also suggest changing the category to Share Your Projects as I think you put together a nice implementation and this way more people will see it. I’d like to try it myself; thanks for sharing.

Thanks man! I’ve been attempting to do the exact same thing for the exact same reason! But couldn’t quite work out the template to my satisfaction. (It didn’t work hehe). But I’ll try this instead looks good!

Here’s mine in case anyone wants to see (I have the colored bubls in my home change from green --> yellow --> red --> according to when I should get ready/leave. So no matter what I’m doing I have a visual cue. The lights then return to their previous state by matching my lighting mode input select.

alias: 'Traffic lights warning for work'
trigger:
  - platform: numeric_state
    entity_id: sensor.work_alarm
    below: 1800
condition:
  - condition: state
    entity_id: device_tracker.dereks_phone
    state: 'home'
action:
  - service: notify.pushbullet
    data:
      message: Time to get ready for work!
      target: device/Galaxy S7
  - service: light.turn_on
    entity_id: light.office_light, light.bathroom_light, light.bar_light, light.living_room_light
    data:
      transition: 1
      brightness: 255
      color: green
  - delay: '00:06:00'
  - service: light.turn_on
    entity_id: light.office_light, light.bathroom_light, light.bar_light, light.living_room_light
    data:
      transition: 1
      brightness: 255
      color: yellow
  - delay: '00:03:00'
  - service: light.turn_on
    entity_id: light.office_light, light.bathroom_light, light.bar_light, light.living_room_light
    data:
      transition: 1
      brightness: 255
      color: red
  - delay: '00:03:00'
  - service: scene.turn_on
    data_template:
      entity_id: >
        {% if is_state('input_select.lighting_mode', 'Movie') %}
          scene.movie
        {% elif is_state('input_select.lighting_mode', 'Evening Normal') %}
          scene.evening_normal
        {% elif is_state('input_select.lighting_mode', 'Evening Dim') %}
          scene.evening_dim
        {% elif is_state('input_select.lighting_mode', 'Lights Off') %}
          scene.lights_off
        {% else %}
        {% endif %}

Thanks for sharing.

But if I am not completely wrong the “below: 21600” would be 1h not 15min?

Hey there, I am not sure where my 15 minutes remark is coming from. The 21600 means 6 hours before (21600 /60 / 60 = 6). As these calendars contain allday events, they “start” on midnight, I get my reminders to get the trash out at 6pm the day before.

Hope that clarified it.

Thanks, sometimes elementary school maths are too much…

Your numeric state trigger won’t work for me if define the value in apostrophes…

I’ve made package if anybody want to use it: WasteReminder

As always comments and improvements are highly welcome.

Thanks again @d0nnae for pointing me in the right direction and the idea itself