#Set datetime helper to a value relative to next calendar event

1 messages · Page 1 of 1 (latest)

minor mantle
#

Looking to create an automation that will set a datetime helper (time only type) to a value relative to the next event from a selection of calendars.

My exact use case will be to set the helper to two hours before the first event found on either of two specified calendars.

I've had a good poke around and tried to find some relevant resources but my grasp on YAML syntax is too poor to have a chance of figuring this out in a reasonable amount of time. Any help would be greatly appreciated.

minor mantle
#

Bumpity bump

golden rapids
#

This ranges from easy to tricky depending on how you want to handle edge cases

#

The calendar entity has an attribute for the start time of the next or current event (if an event is going currently it displays that start time, otherwise the next one)

#

<datetime> - timedelta(hours=2) will give you a new datetime

minor mantle
#

That's done the job, thanks!

minor mantle
#

Ok reopening this because one of those edge-cases has cropped up. All-day events are a problem; I'm using the simple start_time attribute of the calendar entity itself at the moment, but that's useless if the next event is all-day, because if I want to ignore those, I don't have visibility of the next event 'after'.

That means I'm going to have to use the get_events action, and that is proving so much more complicated, my head hurts.

#

I think you'll need more context now so here's a full explanation of what I'm trying to do:

I have a datetime helper that holds the value of my next morning alarm, that's what we're trying to dynamically set.

I have two calendars that I'd like to monitor for this purpose, my personal calendar, and my work calendar.

The automation should look at both of these calendars, and if it finds a non-all-day event that is within tomorrow (After the next midnight but before the one after that), set the datetime helper to two hours before it.

If it doesn't find any events matching those criteria then it just sets the helper to 10AM.

#

This is my script sequence that works currently, but as I said it has the major problem of ignoring all further events on one of the calendars if it's next immediate event is all-day:

#

Any help from this point would be greatly appreciated as I am completely and utterly stumped.

golden rapids
#

I see what you're trying to do, and I don't have an easy solution for you for the whole thing but I can certainly help out with the calendar.get_events bit

golden rapids
#

This script will get the first non-all day event to occur tomorrow

sequence:
  - variables:
      start: "{{ today_at() + timedelta(days=1) }}"
      end: "{{ today_at() + timedelta(days=2) }}"
  - action: calendar.get_events
    target:
      entity_id: calendar.test_calendar
    data:
      start_date_time: "{{start}}"
      end_date_time: "{{end}}"
    response_variable: calendar_response
  - variables:
      non_day_events: >-
        {{ merge_response(calendar_response) | selectattr('start', 'search',
        'T') | sort(attribute='start') }}
      alarm_time: >-
        {{ today_at('10:00') + timedelta(days=1) if non_day_events | length == 0
        else (non_day_events | first).start | as_datetime | as_local - timedelta(hours=2)}}

Should just need to set your input_datetime.next_morning_alarm to bealarm_time
Now, a few things to note that you might want to change:

  1. end time set to noon tomorrow (checking the event happens in the morning) could be done with today_at('12:00') + timedelta(days=1)
  2. you can add multiple calendars to the action - the merge_response will handle that
  3. the events will have an attribute entity_id to say which calendar it's from, in case you wanted different offsets for work vs personal events
  4. If you wanted to run it at (e.g.) 2am day of the alarm rather than calculating it for tomorrow, you can obviously remove the timedeltas/reduce them by a day
  5. There's almost certainly a nicer way to eliminate all day events - but 'T' in the string indicates that it's a datetime (used by time events) rather than just a date (used by all day events) which seems to be fairly robust (it will discard an all day event, but keep an event that runs from midnight to 2am for example)
minor mantle
#

Wow that is so succinct, you are a wizard.

#

Thank you so much.