Google Calendar event offset

The thread I linked to wasn’t as convoluted as I first thought (sorry), but I’ll explain how I got this working.

The idea of @ih8gates is to use a template sensor and compare the current time to the offset (without using the event naming)

Set up a date_time sensor (which will probably get entity_id of sensor.date__time) to trigger our sensor every minute:

- platform: time_date
  display_options:
    - 'date_time'

Then we can reference that entity_id to make sure the sensor updates. I also referenced the relevant calendar as an extra update:

- platform: template
  sensors:
    my_event_offset:
      friendly_name: "My Event offset"
      entity_id:
        - calendar.myevents
        - sensor.date__time
      value_template: >
        {% if as_timestamp(states.calendar.myevents.attributes.start_time) - as_timestamp(now()) < 21600  %}on{% else %}off{% endif %}

The value 21600 is chosen here as 6 hours (6 * 60 * 60).

Edit: Apparently the entity_id in a template sensor shouldn’t be used anymore. I’m not sure what to replace this with as it works for now.

Edit 2: I sorted how to work around the entity_id issue. The sensor template should look like below:

- platform: template
  sensors:
    my_event_offset:
      friendly_name: "My Event offset"
      value_template: >
        {% if as_timestamp(states.calendar.myevents.attributes.start_time) - as_timestamp( strptime(states.sensor.date__time.state, "%Y-%m-%d, %H:%M" ) ) < 21600  %}on{% else %}off{% endif %}

Since sensor.date__time changes each minute, so too should this offset detector.

5 Likes