From ef25b0c54b23cc8b5fd1366efa3004096a508dd3 Mon Sep 17 00:00:00 2001 From: Tony Stork Date: Sat, 22 Oct 2022 18:34:31 -0400 Subject: [PATCH] Blueprints for updating garbage collection schedule --- .../automation/bruxy70/holiday_in_week.yaml | 67 ++++++++++++ .../move_on_holiday_with_include_exclude.yaml | 103 ++++++++++++++++++ 2 files changed, 170 insertions(+) create mode 100644 blueprints/automation/bruxy70/holiday_in_week.yaml create mode 100644 blueprints/automation/bruxy70/move_on_holiday_with_include_exclude.yaml diff --git a/blueprints/automation/bruxy70/holiday_in_week.yaml b/blueprints/automation/bruxy70/holiday_in_week.yaml new file mode 100644 index 0000000..7b18f0b --- /dev/null +++ b/blueprints/automation/bruxy70/holiday_in_week.yaml @@ -0,0 +1,67 @@ +blueprint: + name: Holiday in week + description: "This automation is triggered from the `garbage_collection` integration + by event `garbage_collection_loaded`, during the device state update. \nIt PROCESSES + DATA SENT WITH THE EVENT, and then FINISHES THE UPDATE. DO NOT CALL THIS SERVICE + MANUALLY, IT WON'T WORK (it won't have the data).\nAlso, make sure the INTEGRATION + IS CONFIGURED FOR `manual_update` (that will disable the state update by the integration + and pass it to this automation).\n\nIt loops through the calculated dates and + checks for a public holiday the week, before/on the calculated collection date. + If found, it will move the event to the next day.\n\nThe sensors are normally + updated once a day, or when Home Assistant starts. The update can be triggered + by updating the integration configuration (just hit configure and submit)." + domain: automation + source_url: https://github.com/bruxy70/Garbage-Collection/blob/master/blueprints/holiday_in_week.yaml + input: + garbage_collection_entity: + name: Garbage Collection Entity + description: Triggered by the event for this entity. + selector: + entity: + integration: garbage_collection + multiple: false + holiday_entity: + name: Holidays + description: Entity containing the holidays - an instance of Holidays custom + integration (available through HACS) + selector: + entity: + integration: holidays + multiple: false +mode: parallel +trigger: +- platform: event + event_type: garbage_collection_loaded + event_data: + entity_id: !input garbage_collection_entity +action: +- variables: + holiday_entity: !input holiday_entity +- alias: Iterate through the dates + repeat: + for_each: '{{ trigger.event.data.collection_dates }}' + sequence: + - alias: Is there a public holiday on the week? + condition: template + value_template: "{%- set collection_date = as_datetime(repeat.item) %} {%- set + ns = namespace(found=false) %} {%- for i in range(collection_date.weekday()+1) + %}\n {%- set d = ( collection_date + timedelta( days=-i) ) | as_timestamp + | timestamp_custom(\"%Y-%m-%d\") %}\n {%- if d in state_attr(holiday_entity,'holidays') + %}\n {%- set ns.found = true %}\n {%- endif %}\n{%- endfor %} {{ ns.found + }}" + - alias: Move the date + service: garbage_collection.offset_date + data: + entity_id: '{{ trigger.event.data.entity_id }}' + date: '{{ repeat.item }}' + offset: "{%- set collection_date = as_datetime(repeat.item) %} {%- set ns + = namespace(offset=1, found=false) %} {# Increase offset until we find a + date that is not public holiday #} {%- for _ in range(7) if not ns.found + %}\n {%- set d = ( collection_date + timedelta( days=ns.offset) ) | as_timestamp + | timestamp_custom(\"%Y-%m-%d\") %}\n {%- if d in state_attr(holiday_entity,'holidays') + %}\n {%- set ns.offset = ns.offset + 1 %}\n {% else %}\n {%- set + ns.found = true %}\n {%- endif %}\n{% endfor %} {{ ns.offset }}" +- alias: Update the garbage_collection entity + service: garbage_collection.update_state + data: + entity_id: '{{ trigger.event.data.entity_id }}' diff --git a/blueprints/automation/bruxy70/move_on_holiday_with_include_exclude.yaml b/blueprints/automation/bruxy70/move_on_holiday_with_include_exclude.yaml new file mode 100644 index 0000000..2ec9da1 --- /dev/null +++ b/blueprints/automation/bruxy70/move_on_holiday_with_include_exclude.yaml @@ -0,0 +1,103 @@ +blueprint: + name: Move on holiday with include and exclude dates + description: "This automation is triggered from the `garbage_collection` integration + by event `garbage_collection_loaded`, during the device state update. \nIt PROCESSES + DATA SENT WITH THE EVENT, and then FINISHES THE UPDATE. DO NOT CALL THIS SERVICE + MANUALLY, IT WON'T WORK (it won't have the data).\nAlso, make sure the INTEGRATION + IS CONFIGURED FOR `manual_update` (that will disable the state update by the integration + and pass it to this automation).\n\nFirst, it excludes events on the provided + dated. Enter them one date per line. For example:\n2022-01-03\n2022-04-04\n\nThen, + it loops through the calculated dates and checks for a public holiday on the calculated + collection date. If found, it will move the event to the next day. It will keep + moving if the new collection date also falls on a public holiday.\n\nFinally, + it will add the events provided in the second list. Agaon, one date per line.\n\nThe + sensors are normally updated once a day, or when Home Assistant starts. The update + can be triggered by updating the integration configuration (just hit configure + and submit)." + domain: automation + source_url: https://github.com/bruxy70/Garbage-Collection/blob/master/blueprints/move_on_holiday_with_include_exclude.yaml + input: + garbage_collection_entity: + name: Garbage Collection Entity + description: Triggered by the event for this entity. + selector: + entity: + integration: garbage_collection + multiple: false + exclude: + name: Set of dates to exclude + description: List of dates in format yyyy-mm-dd, one date per line. + selector: + text: + multiline: true + holiday_entity: + name: Holidays + description: Entity containing the holidays - an instance of Holidays custom + integration (available through HACS) + selector: + entity: + integration: holidays + multiple: false + include: + name: Set of dates to include + description: List of dates in format yyyy-mm-dd, one date per line. + selector: + text: + multiline: true +mode: parallel +trigger: +- platform: event + event_type: garbage_collection_loaded + event_data: + entity_id: !input garbage_collection_entity +action: +- variables: + holiday_entity: !input holiday_entity + exclude: !input exclude + include: !input include +- alias: Iterate through the list of dates to exclude + repeat: + for_each: '{{ exclude.split('' + + '') }}' + sequence: + - alias: Remove the date + service: garbage_collection.remove_date + data: + entity_id: '{{ trigger.event.data.entity_id }}' + date: '{{ repeat.item }}' +- alias: Iterate through the dates + repeat: + for_each: '{{ trigger.event.data.collection_dates }}' + sequence: + - alias: Is this date a public holiday? + condition: template + value_template: '{{ (repeat.item in state_attr(holiday_entity,''holidays'')) + and (repeat.item not in exclude) }}' + - alias: Move the date + service: garbage_collection.offset_date + data: + entity_id: '{{ trigger.event.data.entity_id }}' + date: '{{ repeat.item }}' + offset: "{%- set collection_date = as_datetime(repeat.item) %} {%- set ns + = namespace(offset=1, found=false) %} {# Increase offset until we find a + date that is not public holiday #} {%- for _ in range(7) if not ns.found + %}\n {%- set d = ( collection_date + timedelta( days=ns.offset) ) | as_timestamp + | timestamp_custom(\"%Y-%m-%d\") %}\n {%- if d in state_attr(holiday_entity,'holidays') + %}\n {%- set ns.offset = ns.offset + 1 %}\n {% else %}\n {%- set + ns.found = true %}\n {%- endif %}\n{% endfor %} {{ ns.offset }}" +- alias: Iterate through the dates to include + repeat: + for_each: '{{ include.split('' + + '') }}' + sequence: + - alias: Add the date + service: garbage_collection.add_date + data: + entity_id: '{{ trigger.event.data.entity_id }}' + date: '{{ repeat.item }}' +- alias: Update the garbage_collection entity + service: garbage_collection.update_state + data: + entity_id: '{{ trigger.event.data.entity_id }}'