Bin / Waste Collection

I’ll try and get my code cleaned up enough for github soon, but in the meantime, it’s just a take on this card ‘https://sharethelove.io/picture-elements-cards/temperature-glance’ with the image and sensor entities changed.

I’ve basically gone with the same kind of hacky way you have to end up with 2 sensors, one for each (potential) bin that is to be collected, so sometimes I end up with the ‘unknown’ 2nd sensor too and they aren’t centred in my lovelace card anymore so my OCD doesn’t like it lol.

2 Likes

I’ve been trying to do something with my local authority (Bradford Council) but they seem to have there data is some kinda strange database backend (eb). This is the end point they use:

ufs/COLLECTION_DATES.eb?ebd=0&ebp=10&ebz=1_XXXXXXXXXXXXXXX

I’ve tried a couple of web scraping options but the data needs to be searched first (via postcode) and that doesn’t seem to be acheable which the sites I’ve used.

Does anybody have any pointers for this?

like i said a few days ago, save yourselve the trouble from trying to find a way to scrape it.
collect the data once a year, put it in an ical file and use that for your automations.

That sounds like a good solution, however there are a few limitations I can see:

My local authority only release their schedule for a few months ahead not an entire year. The above method also doesn’t take into account any changes/cancellations of the collection.

i dont know how its at your place but if its anything like here they change very little.
we get a new schedule every year, but i now before the schedule is there which dates it will be.

does it happen regularly that they change or cancel?
we never had a cancelation in 13 years. (and even if they did noone would know in advance)

After much searching I decided to take your advice and integrate the Google Calendar solution:

00%20pm

As you can see if works well but one issue I have is getting it to display the date suffix ‘rd’, ‘th’, ‘nd’

This is how I’ve created the date output:

  waste_collection_date:
    friendly_name: 'Collection Date'
    value_template: '{{ as_timestamp(states.calendar.waste_collection.attributes.start_time) | timestamp_custom("%A %d") }}'
    icon_template: mdi:calendar-star

Does anyone have any ideas how to calculate the correct suffix based on a date?

http://strftime.org for reference.

Struggling to download the pup extension onto my Ubuntu install. Could someone clarify what the command is for this?

i think you can only do something like this (sorry cant do it completely because i am bad with template):

{{if (as_timestamp(states.calendar.waste_collection.attributes.start_time) | timestamp_custom("%d") == 1)}}
 as_timestamp(states.calendar.waste_collection.attributes.start_time) | timestamp_custom("%A %d")st
{{elif (as_timestamp(states.calendar.waste_collection.attributes.start_time) | timestamp_custom("%d") == 2)}}
 as_timestamp(states.calendar.waste_collection.attributes.start_time) | timestamp_custom("%A %d")nd
{{elif (as_timestamp(states.calendar.waste_collection.attributes.start_time) | timestamp_custom("%d") == 3)}}
 as_timestamp(states.calendar.waste_collection.attributes.start_time) | timestamp_custom("%A %d")th
{{ end if}}

Mmm thanks for the suggestion but its giving me an error. I’ll have a play with the template console and see what I can do.

I found this on Stackoverflow but can’t get my head around how it would translate into a HA template:

like i gave it returns a string, so you need to convert that to an int.
its probably not the simplest or nicest way, but i tested this:

{% if (as_timestamp(states.calendar.waste_collection.attributes.start_time)|timestamp_custom("%d")|int)==1 -%}
  st
{% elif (as_timestamp(states.calendar.waste_collection.attributes.start_time)|timestamp_custom("%d")|int)==2 %}
  nd
{% elif (as_timestamp(states.calendar.waste_collection.attributes.start_time)|timestamp_custom("%d")|int)==3 %}
  rd
{% else %}
  th
{%- endif %}

in the editor and it works.

Wouldn’t that run into problems with 11st, 12nd and 13rd?

Ok I think I’ve found a Jinja2 specific answer but not 100% it can be implemented as I don’t know that much about the templating system.

i dont think there is an elevenst but an eleventh?
or am i wrong about that :wink:

@apt i know that you can use vars (or in this case a dict) but i think you need to search the forum to find examples.
the code i gave works, but it can be simplefied.

The code you supplied just seems to output th, it’s defaulting to the final {% else %} from what I can see.

it gives st when the day is 1, nd when the day is 2 and rd when the day is 3
i replaced (as_timestamp(states.calendar.waste_collection.attributes.start_time)|timestamp_custom("%d")|int) with 1 and then it gives st

of course th is default, that applies to 90% from all dates.

Thanks for all your help. I managed however (via another thread) to work out how to perform what I wanted via a Jinja2 macro, See below for the full sensor:

  waste_collection_date:
    friendly_name: 'Collection Date'
    value_template: >
       {%- macro suffix(d) %}
       {%- set sfx = {1:'st',2:'nd',3:'rd'} %}
       {{- 'th' if 11 <= d <= 13 else sfx.get(d%10, 'th') }}
       {%- endmacro %}
       {% set day = as_timestamp(states.calendar.waste_collection.attributes.start_time) | timestamp_custom('%A') %}
       {% set date = as_timestamp(states.calendar.waste_collection.attributes.start_time) | timestamp_custom('%d') |int %}
       {{ day }} {{ date }}{{ suffix(date) }}
    icon_template: mdi:calendar-star
1 Like

if it makes you happy :wink:
it does the same but way more complex :wink:

1 Like

But yours doesn’t work as far as I can tell, because your algorithm doesn’t take into account 11, 12 and 13.

thats because it shouldnt.
its

1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th, 10th, 11th, 12th, 13th, 14th, etc.
so you only need exceptions for 1, 2 and 3

its the script from apt what is very strange.

that says:
make it “th” if the date is 11, 12 or 13
else make it like we put in the dict (so use 1, 2 and 3) and if its not in the dict use “th”

so the complete first statement is obsolete.
this macro would do exactly the same:

       {%- macro suffix(d) %}
       {%- set sfx = {1:'st',2:'nd',3:'rd'} %}
       {{- sfx.get(d%10, 'th') }}
       {%- endmacro %}

we are looking at the day in date, not at the last figure from the day in date