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
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.EDITIt’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.
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:
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.
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?