I did a bit of work on this last night. Couldn’t get any API calls or a screen scrape from my council’s website to work, so I went with something a bit simpler.
To calculate the next collection date:
import datetime
import holidays
import json
today = datetime.date.today()
# Normal collection day is Thursday, day 3 of a zero-indexed week.
if today.weekday() > 3:
# If this Thursday has passed, we only care about next week.
today = today + datetime.timedelta((0 - today.weekday()) % 7)
this_week = dates = [today + datetime.timedelta(days=i) for i in range(0 - today.weekday(), 7 - today.weekday())]
uk_holidays = holidays.CountryHoliday('UK')
is_bank_holiday = False
for weekday in this_week:
if weekday in uk_holidays:
is_bank_holiday = True
# Set the collection date to next Thursday.
next_collection = today + datetime.timedelta((3 - today.weekday()) % 7)
if is_bank_holiday:
# Rubbish is collected a day later on weeks with a bank holiday.
next_collection = next_collection + timedelta(days=1)
print(next_collection)
And then, to work out what the next type of collection is:
import datetime
today = datetime.date.today()
if today.weekday() > 3:
# If this Thursday has passed, we only care about next week.
today = today + datetime.timedelta((0 - today.weekday()) % 7)
# Get the ISO week number (1-52~53)
week_number = today.isocalendar()[1]
if (week_number % 2) == 0:
# Even weeks are for Landfill waste
collection_type = 'Landfill'
else:
# Odd weeks are for Recycling and Garden Waste.
collection_type = 'Recycling & Garden Waste'
print(collection_type)
Finally, hook up some sensors:
- platform: command_line
name: Next Bin Collection
command: "python3 /config/Scripts/waste_collection_date.py"
- platform: command_line
name: Bin Collection Type
command: "python3 /config/Scripts/waste_collection_type.py"
And voila!