Garbage Collection Schedule with alternating types for recycling in a simple icon

I took a bunch of ideas from various posts here and made my own garbage pickup schedule sensor. We have an alternating schedule of any pickup vs just recycling and compost. It also changes when there are holidays or other missed days. Thankfully our city has an ical feed specific to each home which we use here.

This configuration will create two sensors as well as an automation that will create a notification on the day of.

I use the iCal sensor from https://github.com/tybritten/ical-sensor-homeassistant as well as Custom-UI.

You can make the sensor as small as this:
image
As the day gets closer it will use the day of the week or Today/Tomorrow instead of just how many days.

sensors:
  - platform: ical
    name: collect
    url: !secret garbage_collection_calendar
  - platform: template
    sensors:
      collect_next_date:
        friendly_name: "Next Pickup"
        #unit_of_measurement: 'Days' # Not using this since I make it friendlier with customui js
        value_template: '{{ (as_timestamp((states.sensor.collect_event_0.attributes.start|string)[0:19]) | timestamp_custom("%j") | int) - (as_timestamp(now()) | timestamp_custom("%-j") | int) }}'
        icon_template: >
          {% if 'Garbage' in states.sensor.collect_event_0.attributes.name %}
            mdi:trash-can
          {% else %}
            mdi:recycle
          {% endif %}

If you want an additional sensor to show the type of next pickup, use this:

      collect_next_type:
        friendly_name: "Next Pickup Type"
        value_template: >
          {% if 'Garbage' in states.sensor.collect_event_0.attributes.name %}
            Garbage, Compost & Recycling
          {% else %}
            Compost & Recycling
          {% endif %}
        icon_template: >
          {% if 'Garbage' in states.sensor.collect_event_0.attributes.name %}
            mdi:trash-can
          {% else %}
            mdi:recycle
          {% endif %}

For notifications, use the following. You can adjust this to push to your iPhone, etc.

automation:
  - id: 'collect_notification'
    alias: Collect Notification
    trigger:
    - at: '08:00:00'
      platform: time
    condition:
    - condition: state
      entity_id: sensor.collect_next_date
      state: '0'
    action:
    - service: persistent_notification.create
      data:
        title: "Curbside Collection"
        message: "Curbside Collection Today: {{ states('sensor.collect_next_type') }}"
        notification_id: collect

Friendly date view with custom-ui (https://github.com/andrey-git/home-assistant-custom-ui). This will use a number of days if greater than 7, the day of the week up until the day before. Then it says ‘Today’ or ‘Tomorrow’.

In customize:

sensor.collect_next_date:
  templates:
    state: >
      if (state == 0) return 'Today'; else if (state == 1) return 'Tomorrow';
      else if (state < 7) return (new Date(new Date().setDate(new Date().getDate()
      + Number(state))).toLocaleDateString('en-us', {weekday: 'short'})); else
      return state + ' Days';
7 Likes

Thank you for sharing, this was awsome!

The new ical code adds a timestamp which necessitated a change in the code to filter that out.

I’ve modified the original post, but the change is to the value_template of collect_next_date.

value_template: '{{ (as_timestamp((states.sensor.collect_event_0.attributes.start|string)[0:19]) | timestamp_custom("%j") | int) - (as_timestamp(now()) | timestamp_custom("%-j") | int) }}'

Notably the conversion to a string and only getting the first 20 characters of the start attribute.

Is the URL the download path to the iCal file provided by the city? I would need to realise this via Dropbox because we receive the iCal via a form.

For me it is provided by the city.

Also, the recent iCal integration update has an issue with the actual start time of the events. Seems to be time zone related. Wondering if anyone else is having the same issue.