Bin / Waste Collection


#41

Can you share the packet you sniffed? I’d suspect there is a pre-auth, maybe when the app opens(another packet sent before the api call)


#42

I will, once I get it off my phone!


#43

Ahh, see here it’s very simple. If there’s a public holiday, your bin is collected the next day.

My council website literally just works out if the week number is odd or even (in JS) and injects an img tag into a div based on that.


#44

I wish they kept it that simple in the UK - we do like to make things complicated :wink:

I’ve never been able to predict what the hell happens to the collections over Easter and Christmas, I’ve not spotted a pattern yet :smile:


#45

Haha, I think it’s the Birmingham schedule with 5 different bins over 1/2/3 week cycles that I saw… what a mess.

I get notifications from council via an app of the colour and if they’re changing the date, just wanted to chuck colours into HA - not particularly fussed about day.


#46

So is the council putting out dodgy info on the website - or is it that there is just no rhyme or reason to the logic?

Maybe i’m over simplifying it but our council just says Bin X on Day Y - and I can derive logic from that based on my current day.

As stated above I have (two sensors) one sensor that’s always there that fluctuates from Other bin to Recycle Bin and a sensor that appears and disappears on alternate weeks for the Green Waste.


#47

Frankly, it is probably easier to look at what bins the neighbours put out!


#48

This is the one I hinted at. Edinburgh not Birmingham…

I think your fluctuating sensor is pretty much what I need, will scroll up!

Whoops, missed this. How does it handle if HA goes down - assuming you set the initial value manually.


#49

Omg - that looks my daughters sticker chart!

This is what it looks like at the moment

As you can see the second sensor is hidden


#50

Actually I pretty much think this about weather sensors too. Look out the window, if it is wet, it’s raining. If it is bright with sharp shadows it is sunny. Open the door and walk out, measure temperature with body, etc.


#51

It’s a fair point @nickrout, but you can spawn different automations from data once you have it. Eg a notification to take the specific bin out.

We have found we use the waste sensor, and it’s not just a gimmick. We have a tablet in the lounge that wakes when you walk past it and the bin sensor is right there. Also same effort as looking out the window, but with a guaranteed result :+1:


#52

I know, but it is also good to be cynical sometimes!


#53

I’m as salty as they come, for me this was also a little bit about learning how to do stuff with hassio other than preinstalled plugins. I figured that most councils in Australia are similar (week about for the green/recycling) so it could easily be shared once done.

As it turns out, I also asked on the Discord and mihalski came up with a solution:

sensor:
  - platform: template
    sensors:
      binday:
        value_template: '{% if (((now().strftime("%W") | int) % 2) == 0)%}Even{%else%}Odd{%endif%}'

Just testing it now…


#54

My wife calls me a grumpy old man :older_man:


#55

So I tried to write something similar, turns out my local council’s waste collection page is nowhere near as easy to scrape data from. After some investigation with my browser’s dev tools, it looks like the API calls that it makes under the covers are very arcane and seem to rely on unique IDs generated at runtime in order to return any data. On top of that the whole page uses AJAX so it’s not like I can link directly to a result page for my address.

Our government makes a lot of noise about providing open, easily accessible data and APIs for public services, it’s just a shame that none of it is standardized and each council seems to have its own implementation!


#56

Hi @Robbrad, you helped me with this on another thread curl 'http://ci.draftserver.com/penrith/webservice/location/search' -H 'Connection: keep-alive' -H 'Cache-Control: max-age=0' -H 'Origin: http://ci.draftserver.com' -H 'Upgrade-Insecure-Requests: 1' -H 'Content-Type: application/x-www-form-urlencoded' -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8' -H 'Referer: http://ci.draftserver.com/penrith/webservice/location/search?st_number=38&suburb=penrith&address=mulgoa+road&offset=' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: en-US,en;q=0.9' --data 'st_number=38&suburb=penrith&address=mulgoa+road&offset=' --compressed

which gives me {"code":"0001","msg":"Success","offset":1000,"total":"1","results":[{"unit_number":"0","house_number":"38","street":"Mulgoa","street_type":"Road","suburb":"PENRITH","postcode":"2750","collection_day":"TuesA2","bin_type":"3","dwelling":"single","date_time":"0000-00-00 00:00:00","current_week_type":"EVEN","bin_colors":"1,2,3","collection_frequencies":"2,1,2","area":"2","item_garbage":"2018-06-26","item_recycling":"2018-06-19","item_organic":"2018-06-19"}]}

What’s the best way to get this running and import the data into HA?


#57

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!

bin_card


#58

Could you try using the https://www.home-assistant.io/components/sensor.rest/

This sensor can read your data - (JSON)


#59

Thanks @Robbrad, I finally got something working it’s a bit of a messy solution but it’s natively getting the data so I can eventually remove the google calendar.

I’m using hass.io so I tried to use the commandline tool to run the curl script, but that didn’t work so in the end I converted the curl string to a python script. This runs on a Windows machine running python and apache. The rest sensor then reads the data and imports to hass.io.

Any suggestions on how I could just run the python script in hass.io and the rest sensor reading it from there?

For now I can display the information but no automation. Which is the next new thing I’m trying to work out. Currently I’m using google calendar which I have entries for eg: 2018-07-09 00:00:00, I then compare this with the current date time but I trim the time and the output will be the current date with 00:00:00 as the time (as_timestamp / 86400).

Is there an easier way to just compare the date and get the value? 2018-07-09 - 2018-07-03? Otherwise is it possible to combine the date that I have from my sensor and attach 00:00:00 to it?


#60

Have a look how I achieved it just using bash. See my original post above.

My script just returns a value based on parsing the JSON

Should be no need for a python script.