Getting a handle on the Calendar integration

The new’ish Calendar Integration looks exciting but I don’t quite have a solid grasp on how to leverage it best. I am looking for guidance on a use-case which currently seems a little convoluted in execution and perhaps could leverage the new integration.

Requirement

Set a phone alarm tomorrow based on the following requirements;

  • there is a meeting scheduled that starts on or after 8AM
  • this alarm should be set the night before

Conditions

  • I am set as required in the attendees
  • it is a workday

Actions

  • set the alarm 90 minutes prior to the meeting

The Challenge

I see that the Calendar Integration allows one to leverage trigger.calendar_event but I am unclear how I would parse this compared to current template (below).

What I use today.

The below template sensor works to output the time tomorrow that is 90 minutes before the first call. It works. It just doesn’t seem like the best approach.

    - name: "tomorrow_first_meeting_start"
      unique_id: tomorrow_first_meeting_start
      state: >-
        {% set data = namespace(entities=[]) %}
        {% set today_sec = 86400 %}
        {% set eightAM_sec = 28800 %}
        {% set fourtyEightHours_sec = 172800 %}
        {% set tomorrowEightAM_sec = 115200 %}

        {% for meeting in state_attr('calendar.calendar_me','data') %}
            {% set meeting_start = as_timestamp( meeting.start ) - as_timestamp(now().date()) %}
            {% if meeting_start > today_sec and meeting_start < fourtyEightHours_sec and
            meeting_start >= tomorrowEightAM_sec %}
              {% set data.entities = data.entities + [meeting] %}      
            {% endif %}
        {% endfor%}

        {% set tomorrow_meetings = namespace(entities=[]) %}
        {% for entry in data.entities %}
            {% for email in entry.attendees %}
                {% if email.email == '[email protected]' and email.type == 'required' %}
                  {% set tomorrow_meetings.entities = data.entities + [entry] %}   
                {% endif %}
            {% endfor %}
        {% endfor %}
        {{ (as_timestamp(tomorrow_meetings.entities[0].start) - 5400) | timestamp_custom ('%H%M') }}

Here is the automation that consumes it;

- id: '1655406948412'
  alias: '[ALARM] Set work alarm tomorrow'
  description: ''
  trigger:
  - platform: time
    at: '23:00:00'
  condition:
  - condition: time
    before: '23:05:00'
    after: '22:59:00'
    weekday:
    - mon
    - tue
    - wed
    - thu
    - sun
  action:
  - if:
    - condition: template
      value_template: '{{ is_state(''binary_sensor.workday_tomorrow'',''on'') }}'
    then:
    - service: notify.mobile_app_pixel_4a
      data:
        message: command_activity
        data:
          channel: com.google.android.deskclock
          tag: android.intent.action.DISMISS_ALARM
          group: android.intent.extra.alarm.SEARCH_MODE:android.label,android.intent.extra.alarm.MESSAGE:Home
            Assistant Alarm
    - service: notify.mobile_app_pixel_4a
      data:
        message: command_activity
        data:
          channel: com.google.android.deskclock
          tag: android.intent.action.SET_ALARM
          group: android.intent.extra.alarm.HOUR:{{states('sensor.tomorrow_first_meeting_start')[1:-2]}},android.intent.extra.alarm.MINUTES:{{states('sensor.tomorrow_first_meeting_start')[2:]}},android.intent.extra.alarm.MESSAGE:Home
            Assistant Alarm,android.intent.extra.alarm.SKIP_UI:true
    else:
    - service: notify.mobile_app_pixel_4a
      data:
        message: command_activity
        data:
          channel: com.google.android.deskclock
          tag: android.intent.action.DISMISS_ALARM
          group: android.intent.extra.alarm.SEARCH_MODE:android.label,android.intent.extra.alarm.MESSAGE:Home
            Assistant Alarm
  mode: single

Your thoughts on making this consumable via the Calendar Integration?

[edit] spelling/clarity

I have stumped the jinja-ninja’s? Or the Calendar Integration is too new?

I’ll let this simmer…

I’d suggest having a look at using a template binary sensor invoked from the calendar trigger with an offset, then combing that with other triggers. That is, have a binary sensor to keep some particular state based on the upcoming event, then have separate triggers for the actual notification.

Thanks Allen. I didn’t show my trigger, my bad. I trigger off time then read the first template sensor shown. So all good there. It just didn’t seems clean to run through two iterations. I suspect there is a better way to filter the events that I’m not aware of.

@allenporter - now that I have found the Calendar Automation Trigger Variables, I think I am closer to leveraging the Calendar Integration for my use-case.

I could trigger 9 hours before the earliest event, therefore meeting the requirement to trigger before bedtime.

eg

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

And I think I have the condition “on or after 8AM covered”;
{{ as_timestamp(trigger.calendar_event.start) | timestamp_custom ('%H') | int(0) >= 8 }}

What I don’t see listed in the available triggers is ‘attendees’ or ‘required’ of shared meetings.

Do you know if the Calendar Integration trigger strip these attributes?

Hi, the calendar integration now has a specific set of attributes supported, listed in the trigger data. We can extend them with new attributes based on the calendar RFC. The Google calendar library already does have support for l returning attendees for example. This requires changes to core to happen.

I am using an HACS O365 calendar so any changes to core for the Google calendar doesn’t guarantee attendees will work?

Either way, I’d upvote that feature. Gotta start somewhere…

OK so maybe then I would say it this way: it needs to be supported in core for any Calendar Entity, then other calendar integrations can implement it.

That helps, thanks!