A Garbage Day automation using Google Calendar and pre-warning that actually works

Sure, its always good to know why its not working, this is my full template sensor:

binary_sensor:
  # Garbage Day sensors
  - platform: template
    sensors:
      graue_tonne:
        friendly_name: 'Graue Tonne'
        icon_template: >
          {% if is_state('binary_sensor.graue_tonne', 'on') %}
            mdi:delete-alert
          {% else %}
            mdi:delete-outline
          {% endif %}
        value_template: >-
          {% set update = 'sensor.date_time' %}
          {% set calendar = 'calendar.graue_tonne' %}
          {# Adapt these for your needs #}
          {% set seconds_before_all_day = 25200 %}
          {% set seconds_before_end_all_day = 50400 %}
          {% set seconds_before_single = 3600 %}
          {% set start = state_attr(calendar,'start_time') %}
          {# Prevent error when no calendar event (we won't have a start_time then) #}
          {% if start != None %}
            {% set _now = now().timestamp() %}
            {% set start = as_timestamp(start) %}
            {% set end = as_timestamp(state_attr(calendar,'end_time')) %}
            {% if _now < end %}
              {% if state_attr(calendar,'all_day') %}
                {{ _now < end - seconds_before_end_all_day and start - _now < seconds_before_all_day }}
              {% else %}
                {{ start - _now < seconds_before_single }}
              {% endif %}
            {% else %}
              false
            {% endif %}
          {% else %}
            false
          {% endif %}

Any advise is much appreciated.

1 Like

Change the first line to this:

          {% set update = states('sensor.date_time') %}

If you paste the entire template into the Template Editor, it will tell you which entities it will listen to for state-changes. For example, here is your corrected template and it indicates that it will listen to calendar.graue_tonne and sensor.date_time.

2 Likes

As far as I can see, that did the trick!
Thank you very much :slight_smile:

well crap. Forgot rest…

Sorry about that. Had an old person moment there I guess.

No worries, thank you anyway as well!

1 Like

had to update Garbage calendar for a new year but this automation is still working for me very well!

PS: Colorado

Just followed your guide and all worked perfectly! Thanks!

Hi ,
Thanks for this fantastic tutorial , I’ve implement this this day.
I have a question about this , for now I have state of sensors in entity card and I want to know If it’s possible to display remaining time instead ?

I followed this guide to set up binary sensors that occurred at specific times during a meeting and after a meeting as opposed to before. This worked well for firing off events/automations during the window of the meeting, but I couldn’t get it to work for when the meeting was over (e.g., Airbnb calendar shows end of meeting as midnight day of checkin vs. checkout which is at 10:00 AM so the meeting is already over).
Not sure if the most elegant solution, but I ended up doing the following:

  • Google calendar that turns on when reservations starts. For Airbnb this starts at midnight day of checking.
  • When this turns I have an automation that creates a new calendar event on a separate Airbnb Checkin/Checkout calendar to create point in time calendar events for when I want to trigger things (e.g,: set Ecobee to home to 2 hours prior to check-in so Airbnb is comfortable and set Ecobee to away after checkout to save on heating/cooling costs).
  • I used the attributes of the original Airbnb calendar to set the new start/end times of the newly created checkin/checkout events like so by adding the appropriate number of hours to the start/end times that align with my actual check-in/checkout times
      start_date_time: '{{ (as_timestamp(state_attr(''calendar.reserved'', ''start_time'')) + (12 | float)*60*60) | timestamp_local }}'
      end_date_time: '{{ (as_timestamp(state_attr(''calendar.reserved'', ''start_time'')) + (13 | float)*60*60) | timestamp_local }}'
  • I then have automations set up that run when these events turn from off to on

Instead of creating a google calender and importing the .ics file manually on an annual basis do this instead:

  1. For this to work your city or location must provide a recollect.net URL for creating and updating your google calendar. Once you “add” the url your waste calendar will stay current always. So for example cities like Vancouver, Edmonton, Calgary, Saskatoon, Ottawa (and many USA cities) use the recollect.net system.
  2. Locate your waste collection calendar provided by your city. Should look something like this:

Type your address in and should find the calendar for your address.
3. Click on “get a calendar”. At this point should be prompted for calendar type; select “Add to google calendar”. You should see:

.
4. Copy the URL .
5. Open google calendar and on the right hand side click the “+” by “other calendar”. Select the option “from URL”.
6. Copy/paste URL and done.
7. Make sure the new waste calendar is selected.
8. Follow steps for Google calendar intergration as outlined in the above HOW-To by Matthias,
9. Your waste calendar and sensors should now show up in home assistant. Something like this:

10. Note that the birthday and other calendar show up because I selected them too. You don’t have to do this if you only want the waste calendar.

Just wanted to say Thanks for this tutorial… it gave me some ideas to get my own sensor up and running.

My garbage collection agency doesn’t offer an online calendar and only holidays that fall Mon-Thur affect collection. Luckily, they take the same 5 holidays off every year… and all of those holidays are available through Google’s “US Holidays” calendar. I used the Google Calendar Events integration to create a calendar for each of those holidays and then put them in a group.

The following trigger-based binary sensor runs on Sunday evenings to determine if the next active holiday from the group is this week and if it will affect collection:

template:
  - trigger:
      - platform: template
        value_template: >-
          {{ (is_state('sensor.time', '19:00')) and (is_state('sensor.today_is', 'Sunday')) }}
    binary_sensor:
      - name: "Weekly Trash Holiday Check"
        state: >-
          {%- for s in expand('group.trash_holiday_calendars') | sort(attribute="attributes.start_time") | selectattr('state', 'eq', 'off') | list %}
          {%- if loop.first %}
            {%- set start = state_attr(s.entity_id,'start_time')|as_datetime|as_local %}
            {%- set full_days_til = (start - now()).days %}
            {{ ( 3 >= (full_days_til | int) >= 0) and (start.strftime("%A") is in ["Monday","Tuesday","Wednesday","Thursday"]) }}
          {%- endif %}
          {%- endfor %}

I then use this as a condition in any of my notification automations or other sensors.

Is it possible to have the search be more explicit? I am splitting a calendar based on two searches - “Trash” and “Recycling and yard waste”. However, every once in a while these happen to line up on the same day, which causes an entry for “Trash, recycling and yard waste” to be listed on both calendars. Ideally, I’d like to split this off into a separate 3rd calendar.

Here’s what I have so far:

- cal_id: [email protected]
  entities:
  - device_id: Trash
    ignore_availability: true
    name: Trash
    track: true
    search: "Trash"
  - device_id: Recycling
    ignore_availability: true
    name: Recycling
    track: true
    search: "Recycling and yard waste"

And here’s a screenshot of what I end up with:

Can you create a sensor showing number of days till “Trash” or “Recycle” on calendar?

Something like this?

sensor:
  - platform: time_date
    display_options:
      - 'date'

  - platform: template
    sensors:
      event_day:
        friendly_name: Event Day
        entity_id: sensor.date
        unit_of_measurement: days
        value_template: >
          {% set midnight = now().replace(hour=0, minute=0, second=0, microsecond=0).timestamp() %}
          {% set event = states('calendar.xxx') | as_timestamp %}
          {{ ((event - midnight) // 86400) | int }}

Clipboard01

Hello.
Thanks for a great job. I am only asking what to do to be able to get notifications such as in the garbage collection?

I was looking for one and i am glad i found this, i don’t use google calendar, my township have the .ics calendar and i subscribed to it so i have the calendar.recycle on my HA. the recycle items show 6:00am every other week on Mon.

I did the binary_sensor as you mentioned, and no errors so far, i do see the sensor under developer tools. now i want to do the automation in node-red (as i use for others). how do i go about it, what do i expect to happen, also mine is a calendar subscribed from internet i have no way of adding test event entries so i can test it. I wish to do a sensor that shows me how many days is left for this binary_sensor to turn on.

how would i do that?

thanks for the awesome snippet.

Hi!

I have found a problem when there is an all day event. There were a few brackets that needed to be inserted :slight_smile:
in your example:

{{ _now < end - seconds_before_end_all_day and start - _now < seconds_before_all_day }}
{{ ((_now < end - seconds_before_end_all_day) and ((start - _now) < seconds_before_all_day)) }}

also i found something regarding the end time.

{% set end = as_timestamp(state_attr(calendar,'start_time')) %}

should be

{% set end = as_timestamp(state_attr(calendar,'end_time')) %}

So google_calendars.yaml doesnt get created anymore or at least not in config folder. anyone have an idea where it is now located? my automations work well but I would like a sensor to display whats coming up, green, red or yellow bins

1 Like

I’m getting this error when i try to add the binary sensor:

In the Netherlands we got afvalwijzer ( GitHub - xirixiz/homeassistant-afvalwijzer: Provides sensors for some Dutch waste collectors ) and there is this integration ( GitHub - mampfes/hacs_waste_collection_schedule: Home Assistant integration framework for (garbage collection) schedules ) so why use a calendar that might not be updated?

Move everything under garbage_day: to the right 2 spaces i.e.

sensors:
  garbage_day:
    friendly_name: 'garbage_day'
    icon_template: ...