0Bin collections in Home Assistant

I’ve been developing my Home Assistant based smarthome setup for around a year now (since leaving SmartThings) and after an initial steep learning curve I’ve been veyr happy with home assistant so I have started working to expand the things it does as I am conifdent it offers a long term basis for my smarthome. One thing I wanted to add was bin collection data to remind me which bin is due when. As I live in Sheffield I needed to pull this data from the local authorities out-sources provider (veolia). The following details how I did this, building very much on the work of others who have done it already for other regions

Update 20-Dec-2021

I’ve made a change to both ths bash script AND the template – the first to make sure required packages are available and the second to fix a breaking change made by recent releases of home assistant

I used a script which i modified from various examples on a HA support thread by RobBrad. For this script to run I needed to install the beautifulsoup module on home-assistant.

Add a script

  • I use the ‘Samba Share’ supervisor addon to access my home assistant files directly from my windows PC – unless you already have a preferred access method I recommend doing the same
  • browse to \\ha-ip-address\config
  • create a python-scripts folder
  • Create a new python script in this folder – I called mine bin_collection.py
  • Insert the following script
    import sys
    import subprocess
    import pkg_resources
    required = {'beautifulsoup4', 'python-dateutil'}
    installed = {pkg.key for pkg in pkg_resources.working_set}
    missing = required - installed
    if missing:
        python = sys.executable
        subprocess.check_call([python3, '-m', 'pip3', 'install', *missing], stdout=subprocess.DEVNULL)
    from bs4 import BeautifulSoup
    import datetime
    from dateutil import parser
    import requests
    import json
    url = 'https://wasteservices.sheffield.gov.uk/property/############'
    page = requests.get(url)
    if page.status_code != 200:
    soup = BeautifulSoup(page.text, 'html.parser')
    out = {}
    bh3s = soup.find_all('td', class_="service-name")
    bpds = soup.find_all('td', class_="next-service")
    for i in range(len(bpds)):
        bin_colour = str(bh3s[i].contents[3]).lower().split('>')[1].split(' ')[0]
        out[bin_colour] = parser.parse(bpds[i].contents[2].lstrip().split(',')[0]).strftime('%Y-%m-%d')
  • You will need to replace the ############ with the unique number for your property which I will explain how to get in the next step

Get your unique address

  • Browse to https://wasteservices.sheffield.gov.uk/property/
  • Enter your post code and select your address from the list
  • Make a note of the new url. Use it in the url line in the script above – the only part you will need to change is the 12 digit number at the end

Call your script from configuration.yaml

  • A simple entry in configuration.yaml is all that is needed to make the script run
  • You may want to adjust the frequency of the run. Mine runs once per day.
     - platform: command_line
       name: "Bin collections"
       command: "python3 /config/python-scripts/bin_collection.py"
       scan_interval: 86400
       command_timeout: 60 #needed as the website is slow to respond and will often timeout if left at default

Create entities for each type of bin using configuration.yaml

  • Immediately under the above section (so still under the sensor: section) add the following
     - platform: template
           device_class: timestamp
           value_template: '{{ strptime((states("sensor.bin_collections")|from_json())["black"], "%Y-%m-%d") | as_local }}'
           unique_id: "black_bin"
           device_class: timestamp
           value_template: '{{ strptime((states("sensor.bin_collections")|from_json())["brown"], "%Y-%m-%d") | as_local }}'
           unique_id: "brown_bin"
           device_class: timestamp
           value_template: '{{ strptime((states("sensor.bin_collections")|from_json())["blue"], "%Y-%m-%d") | as_local }}'
           unique_id: "blue_bin"

Display the results on the front end

  • To show the results on the front end simply add a card with the 3 sensors on it (specifically sensor.black_bin, sensor.blue_bin, and sensor.brown_bin)
  • Note you may need to restart home assistant to load the new sensors you added to configuration.yaml

Got some thoughts of your own? Indulge yourself below by commenting! If you would like to subscribe please use the subscribe link on the menu at the top right. You can also share this with your friends by using the social links below. Cheers.

Leave a Reply