Skip to content

Instantly share code, notes, and snippets.

@bruxy70
Last active August 3, 2023 14:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bruxy70/fc31c7695541e94d0215a075aaa49521 to your computer and use it in GitHub Desktop.
Save bruxy70/fc31c7695541e94d0215a075aaa49521 to your computer and use it in GitHub Desktop.
Create Calendar Events with Offset Holidays
blueprint:
name: Create Calendar Events with Offset Holidays
description: >-
Copy Events (in the next 365 days) from one local calendar to another.
Look in the holidays calendar and shift events colliding with a public
holiday forward.
domain: script
source_url: https://gist.github.com/bruxy70/fc31c7695541e94d0215a075aaa49521
fields:
source_calendar:
name: Source calendar
description: Calendar with the events to be copied from
required: true
selector:
entity:
filter:
integration: local_calendar
destination_calendar:
name: Destination calendar
description: Calendar for the events to be copied to
required: true
selector:
entity:
filter:
integration: local_calendar
public_holidays:
name: Public Holidays
description: Calendar with the public holidays to be avoided
required: true
selector:
entity:
filter:
integration: holidays
sequence:
- service: calendar.list_events
data:
duration:
days: 365
target:
entity_id: "{{ source_calendar }}"
response_variable: source
- service: calendar.list_events
data:
duration:
days: 365
target:
entity_id: "{{ destination_calendar }}"
response_variable: destination
- service: calendar.list_events
data:
duration:
days: 365
target:
entity_id: "{{ public_holidays }}"
response_variable: holidays
- variables:
holiday_dates: |-
{%- set ns = namespace(dates={}) %}
{%- if holidays.events %}
{%- for event in holidays.events %}
{%- set ns.dates = dict(ns.dates, **{event.start:event.summary}) %}
{%- endfor %}
{%- endif %}
{{ ns.dates }}
destination_dates: |-
{%- set ns = namespace(dates={}) %}
{%- if destination.events %}
{%- for event in destination.events %}
{%- set ns.dates = dict(ns.dates, **{event.start:event.summary}) %}
{%- endfor %}
{%- endif %}
{{ ns.dates }}
- repeat:
for_each: "{{ source.events }}"
sequence:
- variables:
offset: >-
{%- set ns = namespace(offset=0, found=false) %}
{# Increase offset until we find a date that is not public holiday
#}
{%- for _ in range(7) if not ns.found %}
{%- set start = strptime(repeat.item.start,"%Y-%m-%d").date() %}
{%- set d = ( start + timedelta( days=ns.offset) ).isoformat() %}
{%- if d in holiday_dates %}
{%- set ns.offset = ns.offset + 1 %}
{% else %}
{%- set ns.found = true %}
{%- endif %}
{% endfor %}
{{ ns.offset }}
start_date: >-
{%- set start = strptime(repeat.item.start,"%Y-%m-%d").date() %}
{{ (start + timedelta(days=offset)).isoformat() }}
end_date: >-
{%- set end = strptime(repeat.item.end,"%Y-%m-%d").date() %}
{{ (end + timedelta(days=offset)).isoformat() }}
summary: "{{ repeat.item.summary }}"
- if:
- condition: template
value_template: "{{ start_date not in destination_dates }}"
then:
- service: calendar.create_event
data:
summary: "{{ summary }}"
start_date: "{{ start_date }}"
end_date: " {{ end_date }}"
target:
entity_id: "{{ destination_calendar }}"
mode: single
icon: mdi:calendar-blank-multiple
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment