Last active
May 18, 2025 17:45
-
-
Save Didgeridrew/491fa955ef2fe7c4f16bc503ac121db8 to your computer and use it in GitHub Desktop.
HA - Create calendar events for all trash nights
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ############################################################## | |
| # All-Day Events for Food Waste Collection | |
| # Collection every Wed from April 1 - Nov | |
| # Collection every other Wed Nov- Apr (skipping first week in Nov) | |
| # | |
| # Designed to be run annually on Apr 1 | |
| ############################################################# | |
| alias: Set Food Waste Calendar Dates | |
| sequence: | |
| - variables: | |
| date_list: > | |
| {%- set food_waste = namespace(dates=[]) %} | |
| {%- set year = today_at().year %} | |
| {%- set april = (year~"-04-01") | as_datetime | as_local%} | |
| {%- set first_wed = april + timedelta(days = 2 - april.weekday()) %} | |
| {%- for i in range(0, 33)%} | |
| {%- set food_waste.dates = food_waste.dates + | |
| [first_wed + timedelta(days=7 * i)] %} | |
| {%- endfor %} | |
| {%- set food_waste.dates = food_waste.dates|reject('<', april) | |
| | reject('>', now().replace(month=10, day=31)) | list %} | |
| {%- set first_nov = food_waste.dates | last + timedelta(days=14) %} | |
| {%- for i in range(0, 13)%} | |
| {%- set food_waste.dates = food_waste.dates + | |
| [first_nov + timedelta(days=14 * i)] %} | |
| {%- endfor %} | |
| {% set next_april = today_at().replace(month=4, | |
| day=1, year= (year +1)) %} | |
| {{- food_waste.dates | reject('>', next_april) | map('string') | |
| | map('truncate', 14, False, '') | unique | list }} | |
| - repeat: | |
| for_each: "{{date_list}}" | |
| sequence: | |
| - service: calendar.create_event | |
| data: | |
| summary: Food Waste Collection Day | |
| start_date: "{{ repeat.item|string }}" | |
| end_date: > | |
| {% set new = repeat.item|string|as_datetime + timedelta(days=1) %} | |
| {{ new.date()|string }} | |
| target: | |
| entity_id: calendar.food_waste | |
| - delay: 1 | |
| mode: single | |
| icon: mdi:calendar-edit-outline | |
| ######################################################################## | |
| # Event from 18:00-20:00 every Wednesday that does not have a trash holiday | |
| # on Mon-Thurs of that week. | |
| # Trash holidays on Mon, Tues, Wed, or Thursday move the event to Thursday. | |
| # | |
| # Designed to be run annually on Jan 1 | |
| ######################################################################## | |
| alias: Calendar - Set Trash Nights | |
| variables: | |
| date_list: > | |
| {%- set ns = namespace(trash_nights= [], week=[] ) %} | |
| {%- set holiday = label_entities('trash collection holidays')|expand %} | |
| {%- for each in holiday %} | |
| {%- set date = each.state|as_datetime|as_local %} | |
| {%- set isoweekday = (date).isoweekday() %} | |
| {% If the holiday isn't on Fri, Sat, or Sun create a calendar event on Thursday for the trash collection %} | |
| {%- if isoweekday not in [5, 6, 7] %} | |
| {%- set day_offset = 4 - isoweekday %} | |
| {%- set ns.trash_nights = ns.trash_nights + | |
| [{"summary": "Trash Night", | |
| "description": "Offset due to " ~ each.attributes.friendly_name, | |
| "start_datetime": (date + timedelta(days=day_offset)).replace(hour=18).isoformat(), | |
| "end_datetime": (date + timedelta(days=day_offset)).replace(hour=20).isoformat() }] %} | |
| {# Add this event's week number to the list of weeks that now have events #} | |
| {%- set ns.week = ns.week + [date.isocalendar().week] %} | |
| {%- endif %} | |
| {% endfor %} | |
| {# Create a list of week numbers that still need an event #} | |
| {%- set last = now().replace(month=12,day=31).isocalendar().week + 1 %} | |
| {%- set week_range = (range(1,last))|list|reject('in', ns.week)|list %} | |
| {%- set d = today_at().replace(day=1, month=1) %} | |
| {%- set first = d + timedelta(days = 2 - d.weekday() ) %} | |
| {# Create a calendar event for each week in the previously genereated list of weeks that still need an event #} | |
| {%- for i in week_range %} | |
| {%- set date = first + timedelta(days = 7 * (i)) %} | |
| {%- set ns.trash_nights = ns.trash_nights + [ | |
| {"summary": "Trash Night", | |
| "description": "", | |
| "start_datetime": date.replace(hour=18).isoformat(), | |
| "end_datetime": date.replace(hour=20).isoformat() }] %} | |
| {%- endfor %} {{ ns.trash_nights }} | |
| sequence: | |
| - repeat: | |
| for_each: "{{date_list}}" | |
| sequence: | |
| - service: calendar.create_event | |
| data: | |
| summary: "{{ repeat.item.summary }}" | |
| description: "{{ repeat.item.description }}" | |
| start_date_time: "{{ repeat.item.start_datetime }}" | |
| end_date_time: "{{ repeat.item.end_datetime }}" | |
| target: | |
| entity_id: calendar.trash_nights | |
| - delay: 2 | |
| mode: single | |
| icon: mdi:calendar-edit-outline |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment