Bin / Waste Collection

Well, I tried :disappointed:

On a virgin pi, I did
git clone https://github.com/robbrad/UKBinCollectionData.git

which seemed to work ok.
Then…

python collect_data.py LeedsCityCouncil https://www.leeds.gov.uk/residents/bins-and-recycling/check-your-bin-day -p "LS1 2JG" -n 41
Traceback (most recent call last):
  File "/home/pi/UKBinCollectionData/uk_bin_collection/uk_bin_collection/collect_data.py", line 6, in <module>
    from uk_bin_collection.uk_bin_collection.get_bin_data import \
ModuleNotFoundError: No module named 'uk_bin_collection'

So not being a dev, I went back to the docs Setup · robbrad/UKBinCollectionData Wiki · GitHub , and saw that I needed poetry, so I did.

curl -sSL https://install.python-poetry.org | python3 -

Then after a long while I got

Installing Poetry (1.5.1): An error occurred. Removing partial environment.
Poetry installation failed.
See /home/pi/UKBinCollectionData/poetry-installer-error-kefnwtfm.log for error logs.

I’m no way a HA noob and despite working in IT and playing around with pi’s at home for years, it seems there’s a level of assumed knowledge that I can’t find.

Appreciate all the work people have put in and its a really useful feature to have. I’m happy to tinker around and play with stuff, and can usually get things going after a bit of googling, but this has got me stumped.

I thought I’d try run this on a clean pi before I look at how to run it on my ‘production’ :sweat_smile: HA setup.

Thanks for any pointers.

Can someone help me in trying to set things up. I’m new to this and my I can’t seem to get the sensor to work.

Sensor

sensor:
  - platform: rest
    unique_id: "bin_collection"
    name: "Collection Sensor"
    resource: https://bcprdapidyna002.azure-api.net/bcprdfundyna001-alloy/NextCollectionDates
    method: POST
    payload: '{"uprn":"000000######"}'
    headers:
      Ocp-Apim-Subscription-Key: 47ffd667d69c4a858f92fc38dc24b150
      Ocp-Apim-Trace: true
    json_attributes:
      next_collection_date: "data[0].collection[0].nextCollectionDate"
      general_waste_included: "data[0].containerName == '180L General Waste'"
    value_template:
      "Next Collection: {{next_collection_date}}"

Response

  "status": "OK",
  "data": [
    {
      "containerName": "180L General Waste",
      "collection": [
        {
          "lastCollectionDate": "2023-09-11T00:00:00",
          "nextCollectionDate": "2023-09-25T00:00:00",
          "roundCode": "B",
          "roundDescription": "Monday B Every Other Week"
        }
      ]
    },
    // shortened
  ],
  "error": ""
}```

pip install uk_bin_collection

^ I had to install the module

Why am I seeing this error when trying to run the python script?

Hi
Does any one know why this has stopped working in the latest hA - all i get is rest sensor not ready (UPRN changed for security):

- platform: rest
    name: Green Bins Collection
    resource: https://citizen.westberks.gov.uk/apiserver/ajaxlibrary
    method: POST
    scan_interval: 14260
    device_class: timestamp
    payload: '{ "jsonrpc" : "2.0" , "id" : "16947265" , "method" : "goss.echo.westberks.forms.getNextRecyclingCollectionDate" , "params" : { "uprn" : "1000816" } }'
    #value_template: "{{ as_timestamp(value_json.result.nextRecyclingDateText) | timestamp_custom('%Y-%m-%dT%H:%M:%S%z') }}"    
    value_template: "{{ value_json.result.nextRecyclingDateText }}"
    headers:
      Content-Type: application/json
      User-Agent: Home Assistant REST sensor

using a post man query the JSON looks like this:

{

    "id": "1691265",

    "result": {

        "recyclinghText": "next ",

        "nexttRecyclingDateText": "Wednesday 27 September",

        "nextRecyclingDateSubText": ""

    },

    "jsonrpc": "2.0"

}

I’ve opened an issue West Berkshire Council · Issue #366 · robbrad/UKBinCollectionData · GitHub for this to be looked into for you.

If you can submit a council request here for your council we’ll try to implement it when we can and you can utilise the new Home Assistant Custom Component

On behalf of @Robbrad, @dp247 and everyone else who’s been involved in this project, I’m pleased to announce there’s now a Home Assistant Custom Component for the UK Bin Collection Data project.

We’d recommend installing with HACS.

But there’s also the ability to Install manually if needed.

@James12 @Killa-V @Kvasir15 @S0200356 @Zylent @ArNx @Kitkat If you’re still looking to get your council data into your Home Assistant this may be an easier solution :smiley:

3 Likes

Unfortunately the HACC integration isn’t working for me. It installs ok, I’m able to add my council (chelmsford) and the URL for my property - but beneath the integration it shows ‘no devices or entities’.

I’m using HA core (Home Assistant 2023.10.3) docker on an Unraid server.

Is there any information I can capture in order to raise an issue to help resolution please let me know.

BTW great work on this, many thanks.

2 Likes

I also have this issue through HACS

Added my UPRN and postcode. Broxtowe BC. No devices or entities.

@bobbinz It looks like you may have run into the issue reported here which is affecting some councils including Broxtowe Borough Council

We have a specific issue template for Home Assistant issues in the list here if you can fill out all the fields that should give us a good place to start

Ahh thanks,

I’ll stay tuned for a fix!

Looks like the fix deployed yesterday solved the initial problem, entities added. However unfortunately after several hours they become unavailable so I’ve raised another case. Thanks for fixing the original problem, hopefully this one can be resolved soon, thanks

This looks really promising - I had a homemade PHP integration working previously for Bucks, but a website update broke it and I couldn’t get it working again (see higher up this thread).

Looking at the docs, my council (Bucks) requires a remote selenium webdriver. During setup for HACS, there are 3 inputs - postcode, house number and the third input has no label/helper text. I am assuming this is for the selenium url, but I cannot find any documentation as to how to set this up with HA.

Can anyone point me in the right direction for setting up a selenium webdriver to use with the integration?

Did you already see : Councils · robbrad/UKBinCollectionData Wiki · GitHub

And for the Web driver you could use a docker container

Something like: Docker

Yes - I saw in instructions for Bucks in the WIKI and have tried to run the script on a separate machine.

I was looking for a way to run everything from within HA - any idea how to run the docer container from within home assistant - e.g. as an add-on?

In update, after much playing, I can get data back manually via a remote webdriver running in an ubuntu LXC container on Proxmox. I followed the instructions here to set up the webdriver:

Then on another machine with python and dependancies installed:

python3 collect_data.py BuckinghamshireCouncil https://chiltern.gov.uk/collection-dates -s -p "HP11 XXX" -n "4 STREET NAME, HIGH WYCOMBE" -w http://192.168.0.179:4444/wd/hub

The key for bucks seems to be that you need the full address with house humber, street and town, as shown in the dropdown on the chiltern site.

However, the same data in the HA integration does not seem to return any entities:

image

Is this an input problem? Any way I can troubleshoot?

Long post incoming!

Having struggled with this for a few days, I will post my experience/solutions here in case it helps someone else.

I live in Bucks, using the Chiltern waste calendar. The website uses a multistage process to identify the correct address and show data, with a cookie set from one page to the next. The only way to scrape the data therefore, is to use a headless browser to navigate between pages. This requires the use of a remote webdriver, and I couldn’t find any good documentation as to how to set this up. I also hit problems with the HA integration for Bucks, that I couldn’t get to create any entities.

I use Proxmox on my home server environment, so my solution is as follows:

  1. Set up an ubuntu LXC container, install docker, run a headless selenium webdriver
  2. Use the excellent script from @Robbrad to get the JSON data from the Chiltern site
  3. Serve this data via NGINX on my local network
  4. Create REST sensors in HA to parse and display the information

Setting up the webdriver

After trying a few ways of doing this, I settled on a docker container as the easiest and quickest solution.

Install docker:

apt install docker.io

Install standalone chrome. The following command installs it, runs it as a daemon and starts on boot, and forwards port 4444 to the host:

docker run -d -p 4444:4444 —restart=always selenium/standalone-chrome

Get bin data in JSON format and save it to the NGINX webserver directory

Install NGINX:

apt install nginx

Visit the IP of your container in a broswer to check it is up and running.

Next, clone the GIT repository to your home folder:

mkdir bins
cd bins
git init
git clone https://github.com/robbrad/UKBinCollectionData

Install python dependencies:

apt install pip
pip install pandas
pip install selenium
pip install uk_bin_collection

Navigate to the directory containing the collect_data.py script, and test it out:

python3 collect_data.py BuckinghamshireCouncil https://chiltern.gov.uk/collection-dates -s -p "HP11 XXX” -n "4 STREET NAME, HIGH WYCOMBE" -w http://localhost:4444/wd/hub

You should see some JSON returned:

{
    "bins": [
        {
            "type": "Domestic Garden Collection",
            "collectionDate": "27/11/2023"
        },
        {
            "type": "Domestic Paper/Card Collection",
            "collectionDate": "27/11/2023"
        },
        {
            "type": "Domestic Refuse Collection",
            "collectionDate": "20/11/2023"
        },
        {
            "type": "Domestic Mixed Dry Recycling Collection",
            "collectionDate": "27/11/2023"
        },
        {
            "type": "Domestic Food Collection",
            "collectionDate": "20/11/2023"
        }
    ]
}

Once you are happy it is working, modify the collect_data.py script to save the output to the webserver directory. Replace the last line of the file:

print(data)

with:

f = open(‘/var/www/html/bins.json', 'w')
f.write(data)
f.close()

Now if you navigate to http://xxx.xxx.xxx.xxx/bins.json, you your get the json returned to the browser.

You’ll also need to add the script to a cron job to update regularly:

crontab -e

And add the line:

0 */6 * * * python3 /home/simon/bins/UKBinCollectionData/uk_bin_collection/uk_bin_collection/collect_data.py BuckinghamshireCouncil https://chiltern.gov.uk/collection-dates -s -p "HP11 XXX" -n "4 STREET NAME, HIGH WYCOMBE" -w http://localhost:4444/wd/hub

This updates the JSON file every 6 hours.
Sensor config in HA

This just requires some REST sensors to be defined:


- platform: rest
  name: General Refuse Collection
  resource: http://192.168.0.179/bins.json
  value_template: "{{ value_json['bins'] | selectattr('type', 'match', 'Domestic Refuse Collection') | map(attribute='collectionDate') | list | first }} "
  icon: mdi:trash-can
- platform: rest
  name: Garden Waste Collection
  resource: http://192.168.0.179/bins.json
  value_template: "{{ value_json['bins'] | selectattr('type', 'match', 'Domestic Garden Collection') | map(attribute='collectionDate') | list | first }} "
  icon: mdi:sprout
- platform: rest
  name: Paper Recycling Collection
  resource: http://192.168.0.179/bins.json
  value_template: "{{ value_json['bins'] | selectattr('type', 'match', 'Domestic Paper/Card Collection') | map(attribute='collectionDate') | list | first }} "
  icon: mdi:newspaper-variant
- platform: rest
  name: Mixed Dry Recycling Collection
  resource: http://192.168.0.179/bins.json
  value_template: "{{ value_json['bins'] | selectattr('type', 'match', 'Domestic Mixed Dry Recycling Collection') | map(attribute='collectionDate') | list | first }} "
  icon: mdi:recycle
- platform: rest
  name: Food Waste Collection
  resource: http://192.168.0.179/bins.json
  icon: mdi:food
  value_template: "{{ value_json['bins'] | selectattr('type', 'match', 'Domestic Food Collection') | map(attribute='collectionDate') | list | first }} "

The value_template finds each bin type and extracts the next collection date. Put it all in a card:
image

Hope someone finds this useful!

3 Likes

Thanks, guys super grateful for you guys doing this!

Thanks @dp247 for the kick up the bum to revisit this post

Update: Leeds confirmed working.

1 Like