Quick Project: Rubbish Collection Panel for Home Assistant

With my local council rolling out an increasingly complex system of rubbish bins and collection, I’ve been thinking about getting this integrated with Home Assistant so that we don’t have to remember which bins go out when. I was presently surprised to find the garbage_collection custom component whilst browsing HACS the other day. This component does exactly what I need, so I decided to give it a go.

Rubbish Collection Configuration

After installing the component via HACS, I set about configuring it. Here is the configuration I came up with:

sensor:
  - platform: garbage_collection
    name: Food Scraps
    frequency: "weekly"
    collection_days: !secret rubbish_collection_day
  - platform: garbage_collection
    name: General Recycling
    frequency: "odd-weeks"
    collection_days: !secret rubbish_collection_day
  - platform: garbage_collection
    name: Glass Recycling
    frequency: "even-weeks"
    collection_days: !secret rubbish_collection_day
  - platform: garbage_collection
    name: Landfill
    frequency: "even-weeks"
    collection_days: !secret rubbish_collection_day

That was easy! The only non-obvious thing is working out which bins are collected on even and odd weeks, which is easy to look up online.

Getting it into Lovelace

The garbage_collection component page in HACS has a nice screenshot of the sensors in Lovelace (which doesn’t seem to be in the repository readme). However, the sensors themselves have a state based on whether the bin is due to be put out. The state is nice and machine readable, but I wanted to recreate the panel from the screenshot for the humans that have to look at it. In the end I actually decided to simplify it down to just show “today”, “this week” or “next week” plus the number of days, since the actual date is pretty irrelevant.

home assistant rubbish collection
The finished rubbish collection panel in my Lovelace UI

This proved to be more difficult than I’d expected, since Lovelace doesn’t support templates in cards natively. I had to install the Lovelace Card Templater plugin via HACS. This plugin in turn requires the card-tools plugin, which I couldn’t find in HACS. I ended up installing it by adding it as a git submodule to my configuration repository. I then added the following to my Lovelace config to load the plugins:

resources:
  - type: js
    url: /local/plugins/lovelace-card-tools/card-tools.js?v=1
  - type: js
    url: /community_plugin/lovelace-card-templater/lovelace-card-templater.js

Full Panel YAML

The panel itself is made up of a vertical stack card in which I put two horizontal stack cards. These in turn contain two of the templater cards. The configuration of the templater cards is a little involved since you need to specify the entity twice (which seems to be due to some internal limitation of Lovelace). My template cards are based on the sensor card to show just the data from the template. I use a state_template to do this.

Anyway, here’s the full YAML:

cards:
  - cards:
      - card:
          entity: sensor.food_scraps
          type: sensor
        entities:
          - entity: sensor.food_scraps
            state_template: >-
              {% if state_attr("sensor.food_scraps", "days") == 0 %} Today {%
              elif state_attr("sensor.food_scraps", "days") < 7 %} This Week {%
              else %} Next Week {% endif %} ({{ state_attr("sensor.food_scraps",
              "days") }} days)
        type: 'custom:card-templater'
      - card:
          entity: sensor.general_recycling
          type: sensor
        entities:
          - entity: sensor.general_recycling
            state_template: >-
              {% if state_attr("sensor.general_recycling", "days") == 0 %} Today
              {% elif state_attr("sensor.general_recycling", "days") < 7 %} This
              Week {% else %} Next Week {% endif %} ({{
              state_attr("sensor.general_recycling", "days") }} days)
        type: 'custom:card-templater'
    type: horizontal-stack
  - cards:
      - card:
          entity: sensor.glass_recycling
          type: sensor
        entities:
          - entity: sensor.glass_recycling
            state_template: >-
              {% if state_attr("sensor.glass_recycling", "days") == 0 %} Today
              {% elif state_attr("sensor.glass_recycling", "days") < 7 %} This
              Week {% else %} Next Week {% endif %} ({{
              state_attr("sensor.glass_recycling", "days") }} days)
        type: 'custom:card-templater'
      - card:
          entity: sensor.landfill
          type: sensor
        entities:
          - entity: sensor.landfill
            state_template: >-
              {% if state_attr("sensor.landfill", "days") == 0 %} Today {% elif
              state_attr("sensor.landfill", "days") < 7 %} This Week {% else %}
              Next Week {% endif %} ({{ state_attr("sensor.landfill", "days") }}
              days)
        type: 'custom:card-templater'
    type: horizontal-stack
type: vertical-stack

Each of the templater cards is pretty much the same. I just change the entity ID for each. The only improvement I would make would be to add a line break between the “Today”/”This week”/Next Week” text and the day count, since this would look slightly better. I couldn’t work out how to do that however.

Conclusion

I think I’ve achieved my goal of simplifying the task of remembering which bins go out when. Now I can quickly check that info with a glance at my HASS UI. Of course, now that I have the rubbish collection data in Home Assistant I can use it for other things such as notifications or reminder lights. I already have some ideas for status lighting, so that might become part of a larger project.

I’d like to say thanks to the authors of the custom components and plugins that I’ve used to achieve this. The HASS community really is thriving will all these third party addons at the moment!

4 responses to “Quick Project: Rubbish Collection Panel for Home Assistant”

  1. Dan Avatar
    Dan

    Hey man – thanks for a great post. Gave me what I needed to get this done for myself.

    Quick comment, though: your code block for the YAML doesn’t have the ‘0’ after the ‘==’ when evaluating the “today” condition for each sensor. Without this, hass throws an (annoying) error.

    Also, your ‘<'s have rendered to html '<'s. Not sure if that's a breaking typo, but it looks a little cleaner with brangles. 😉

    Cheers!

  2. Rob Connolly Avatar
    Rob Connolly

    Hi Dan,

    Thanks for the feedback and heads up. The code block breakage is something to do with WordPress and/or the syntax highlighting plugin I use (including the missing zeros, which is really odd). Not sure what the issue is yet, but it’s on my list to investigate.

    Thanks again!

  3. Lee Avatar
    Lee

    Hi Thanks for your post. Its exactly what im trying to do now however im stumped and cant work out what I have done wrong. Im getting a error in the card config screen stating “end of the stream or a document separator is expected at line 49, column 1: type: vertical-stack ^
    Cannot read property ‘setConfig’ of undefined”

    – cards:
    – card:
    entity: sensor.garbage
    type: sensor
    entities:
    – entity: sensor.garbage
    state_template: >-
    {% if state_attr(“sensor.garbage”, “days”) == 0 %} Today {%
    elif state_attr(“sensor.garbage”, “days”) < 7 %} This Week {%
    else %} Next Week {% endif %} ({{ state_attr(“sensor.garbage”,
    “days”) }} days)
    type: ‘custom:card-templater’
    – card:
    entity: sensor.recycling
    type: sensor
    entities:
    – entity: sensor.recycling
    state_template: >-
    {% if state_attr(“sensor.recycling”, “days”) == 0 %} Today
    {%elif state_attr(“sensor.recycling”, “days”) < 7 %} This
    Week {% else %} Next Week {% endif %} ({{
    state_attr(“sensor.recycling”, “days”) }} days)
    type: ‘custom:card-templater’
    type: horizontal-stack
    – cards:
    – card:
    entity: sensor.garden_waste
    type: sensor
    entities:
    – entity: sensor.garden_waste
    state_template: >-
    {% if state_attr(“sensor.garden_waste”, “days”) == 0 %} Today {%
    elif state_attr(“sensor.garden_waste”, “days”) < 7 %} This Week {%
    else %} Next Week {% endif %} ({{ state_attr(“sensor.garden_waste”,
    “days”) }} days)
    type: ‘custom:card-templater’
    – card:
    entity: sensor.hard_rubbish
    type: sensor
    entities:
    – entity: sensor.hard_rubbish
    state_template: >-
    {% if state_attr(“sensor.hard_rubbish”, “days”) == 0 %} Today {%
    elif state_attr(“sensor.hard_rubbish”, “days”) < 7 %} This Week {%
    else %} Next Week {% endif %} ({{ state_attr(“sensor.hard_rubbish”, “days”) }}
    days)
    type: ‘custom:card-templater’
    type: horizontal-stack
    type: vertical-stack

    Any ideas ?

  4. Rob Connolly Avatar
    Rob Connolly

    Hi Lee,

    Sorry for the delay in replying, I wanted to reply after I’d had chance to try out your code.

    I’ve given it a try and I’m afraid I didn’t get very far due to the lack of indentation! Would you be able to send me a better formatted version? You can use the email address on the contact page. Alternatively you can ask on the HASS forums where you’ll have access to the expertise of the whole community. Reply with a link to your thread here and I’ll take a look too.

Leave a Reply

Your email address will not be published. Required fields are marked *

Bad Behavior has blocked 300 access attempts in the last 7 days.