Create next pickup date entity for garbage collection

I’ve been researching how to create a set of entities and automations for garbage collection. Since the bruxy70 HACS integration has been deprecated, I was looking to create my own. However, using the automation editor, there’s no option for day; only time. So, for these cases, I try to figure it out in yaml because it’s more flexible.

My primary goal is to create a TTS automation to notify of garbage pickup the evening before. Pickups are on Tuesdays. But if a public holiday falls on a Monday, pickup is delayed until Wednesday.

I already have a workday sensor (binary_sensor.workday_sensor) that excludes holidays, so I planned on leveraging that.

The problem is I’m not sure how to code it. What platform should I use to trigger based on the Monday TTS notification? Should I trigger based on the state of binary_sensor.workday_sensor then increment a day if false?

Ideally, the flow should look like this:

  • If day is Monday, trigger evening TTS notification that Tuesday is the pickup day
  • Unless the Monday is a holiday
  • Then TTS notification that Wednesday is the pickup day
1 Like

Here’s one possible approach:

Create a Local Calendar named calendar.garbage_schedule. Leave it empty. An automation will be used to add events.

Create an automation with a Time Trigger at midnight and a Template Condition that confirms the current time day is a Monday. Create an action employing the calendar.create_event service call.

The service call will need to use a template to determine if the event should be scheduled for Tuesday or Wednesday. The decision is based on the state of binary_sensor.workday_sensor.

  • If today is a workday then the service call should create an event for the next day (Tuesday).
  • If today is a holiday then the service call should create an event in 2 days (Wednesday).

Currently, the calendar.create_event service call doesn’t allow you to create an All-day event, only a scheduled event with a start and stop time. EDIT It’s possible; see post below. For example purposes, let’s say you schedule it to begin at 06:00 and end at 18:00. Adjust it to meet your requirements.

Create another automation with a Calendar Trigger that monitors calendar.garbage_schedule. Specify an offset of -11:00:00 which means the Calendar Trigger will trigger 11 hours before the scheduled event. So if the garbage pickup event is scheduled for 06:00 on Wednesday, the Calendar Trigger will trigger at 19:00 on Tuesday. Adjust it to meet your requirements.

The automation’s action is simply to announce that you should put out the garbage this evening because tomorrow is a garbage pickup day.


EDIT

Example of the first suggested automation. This creates an All-day event.

alias: Set Garbage Event
trigger:
  - platform: time
    at: '00:00:01'
condition:
  - condition: template
    value_template: "{{ now().isoweekday() == 1 }}"
action:
  - variables:
      day: >
        {{ (now() + timedelta(days = iif(is_state('binary_sensor.workday_sensor', 'off'), 1, 2))).date()|string }}
  - service: calendar.create_event
    target:
      entity_id: calendar.garbage_schedule
    data:
      summary: Garbage Pickup Today 
      start_date: "{{ day }}"
      end_date: "{{ day }}"

Example of the second suggested automation. This creates a notification 5 hours before the All-day event (i.e. midnight - 5 hours = 19:00)

alias: Advance Notification Garbage Event
trigger:
  - platform: calendar
    event: start
    entity_id: calendar.garbage_schedule
    offset: -05:00:00
condition: []
action:
 - service: notify.persistent_notification
   data:
      title: Put out the garbage this evening
      message: Garbage pickup tomorrow.

If you use the start_date and end_date it will create an all-day event…

service: calendar.create_event
data:
  summary: Test 14
  description: Testing All Day
  start_date: "2023-03-20"
  end_date: "2023-03-20"
target:
  entity_id: calendar.home_assistant_events
1 Like

Hi Taras,

Thank you for the great suggestion here! I added both automations. However, the TTS event didn’t fire this evening. I checked the code in the template editor, and for the ‘Set Garbage Event’ it states UndefinedError: 'day' is undefined.

When I checked on it this morning, there was an error in the logs about HA not being able to find the entity calendar.garbage_schedule. Upon checking the integration, the entity was actually named calendar.calendar_garbage_schedule. I adjusted the entry in the YAML, but since it was after the trigger time (Monday at midnight), something probably didn’t run. I also checked the local calendar’s contents from the sidebar and it was empty.

My workday binary sensor is still in configuration.yaml for now (I still need to move it into its own file), but, in case it matters, it’s as follows:

  - platform: workday
    country: US
    workdays: [mon, tue, wed, thu, fri]
    excludes: [sat, sun, holiday]

What exactly did you check in the Template Editor?

If you copy-pasted the automation’s entire action, you can’t test it like that. The day variable is not a Jinja2 variable, it’s a script variable so it won’t be handled as a variable in the Template Editor.

Check the automation’s trace (if it exists).

That will definitely prevent the automation from triggering properly.

Gotcha. I thought the day variable error may have been a red herring. Let me give it another cycle and see what happens next Monday. I’ll let you know what happens when the flow restarts.

Edit: I’d checked the traces about an hour ago and nothing came up. Probably because I made edits after the midnight cut-off.

Any progress on this?

I was able to get the calendar to populate, but the TTS never worked. Unfortunately, the automation subsequently broke in the next March update. Calendar events stopped populating, and I never got back to investigating.

Coming back to this, I wanted to find out why the “Garbage Set Event” stopped working. It appears that calendar event stopped populating around mid April.

The “Garbage Set Event” automation is still running, but not populating the calendar as intended. And, in turn, the notification is not triggering. Is the following code still relevant, considering the updates that have been published since April?

- id: '1631027034111'
  alias: Garbage Set Event
  trigger:
    - platform: time
      at: '00:00:01'
  condition:
    - condition: template
      value_template: "{{ now().isoweekday() == 1 }}"
  action:
    - variables:
        day: >
          {{ (now() + timedelta(days = iif(is_state('binary_sensor.workday_sensor', 'off'), 1, 2))).date()|string }}
    - service: calendar.create_event
      target:
        entity_id: calendar.calendar_garbage_schedule
      data:
        summary: Garbage Pickup Today 
        start_date: "{{ day }}"
        end_date: "{{ day }}"