Galdan
November 21, 2018, 5:41pm
1
Hi all,
I wanted the garbage collection dates on my HA dashboard. Unfortunately, the city of Landshut doesn’t offer an API to access them directly, instead you can download a garbage collection calendar in PDF format.
I wrote a custom sensor to access the data anyway:
You find sourcecode, screenshots and configuration parameters on my private blog:
(I hope linking to my blog is allowed, otherweise please delete the link)
Cheers
Tom
3 Likes
Hi Tom! Thanks for your work! It was easy to convert your code for the city of “Hude”. And I added “Bio Müll” as sensor
1 Like
Aaaaaand I translated the weekdays to germlish Tagen:
- platform: template
sensors:
gelbersack_date:
value_template: "{{ as_timestamp(states.sensor.waste_gelber_sack.state) | timestamp_custom('%d.%m.%Y') }}"
gelbersack_tag:
value_template: "{{ as_timestamp(states.sensor.waste_gelber_sack.state) | timestamp_custom('%w') }}"
gelbersack_days:
unit_of_measurement: 'days'
value_template: "{{ ( ( (as_timestamp(states.sensor.waste_gelber_sack.state) + 64800) - as_timestamp(now()) ) / 86400 ) | int }}"
gelbersack_text:
value_template: >-
{% set wtagg = ["", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"] %}
{% set ddg = states.sensor.gelbersack_tag.state | int%}
{% set wtagg = wtagg[ddg] %}
{{ wtagg }}, {{ states.sensor.gelbersack_date.state }} (in {{ states.sensor.gelbersack_days.state }} Tagen)
Because of my “days left to date” didn’t change in frontend until an hass.io restart, I changed another part of the code:
example:
- platform: template
sensors:
gelbersack_date:
value_template: "{{ as_timestamp(states.sensor.waste_gelber_sack.state) | timestamp_custom('%d.%m.%Y') }}"
gelbersack_tag:
value_template: "{{ as_timestamp(states.sensor.waste_gelber_sack.state) | timestamp_custom('%w') }}"
gelbersack_days:
unit_of_measurement: 'days'
value_template: "{{ ( ( (as_timestamp(states.sensor.waste_gelber_sack.state) + 64800) - as_timestamp(states.sensor.date.state+' '+'00:00:00') ) / 86400 ) | int }}"
gelbersack_text:
value_template: >-
{% set wtagg = ["", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"] %}
{% set ddg = states.sensor.gelbersack_tag.state | int%}
{% set wtagg = wtagg[ddg] %}
{{ wtagg }}, {{ states.sensor.gelbersack_date.state }} (in {{ states.sensor.gelbersack_days.state }} Tagen)
I replaced as_timestamp(now())
with as_timestamp(states.sensor.date.state+' '+'00:00:00
. With this change the entity gets changed the “correct” way.
My wife is happy now
Robin
December 28, 2018, 7:28pm
5
Thanks to both of you, also works very well for my location
Galdan
December 29, 2018, 2:53pm
6
Hi together,
iam very happy that the snippet helped you.
Iam going to adapt your change to my code, thanks
Cheers
Tom
Robin
December 29, 2018, 4:46pm
7
i create the display text in the component and save it as an attribute:
class AbfallSensor(Entity):
def __init__(self, data, sensor_type):
self.data = data
self.type = sensor_type
self._name = SENSOR_PREFIX + SENSOR_TYPES[self.type][0]
self._unit = SENSOR_TYPES[self.type][1]
self._icon = SENSOR_TYPES[self.type][2]
self._state = None
self._attributes = {}
@property
def name(self):
return self._name
@property
def icon(self):
return self._icon
@property
def state(self):
return self._state
@property
def unit_of_measurement(self):
return self._unit
@property
def device_state_attributes(self):
"""Return attributes for the sensor."""
return self._attributes
def update(self):
self.data.update()
abfallData = self.data.data
try:
if self.type == 'gelbersack':
self._state = abfallData.get("gelberSack")
elif self.type == 'restabfall':
self._state = abfallData.get("restAbfall")
elif self.type == 'papiertonne':
self._state = abfallData.get("papierTonne")
elif self.type == 'biotonne':
self._state = abfallData.get("bioTonne")
elif self.type == 'problemstoffmobil':
self._state = abfallData.get("problemStoffMobil")
if self._state is not None:
weekdays = ["Mo", "Di", "Mi", "Do", "Fr", "Sa", "So"]
self._attributes['days'] = (self._state.date() - datetime.now().date()).days
self._attributes['display_text'] = self._state.strftime('{}, %d.%m.%Y (in {} Tagen)').format(weekdays[self._state.weekday()],self._attributes['days'])
except ValueError:
self._state = None
Then I only need one template sensor in the configuration.
- platform: template
sensors:
gelbersack_text:
value_template: "{{states.sensor.waste_gelber_sack.attributes.display_text}}"
biotonne_text:
value_template: "{{states.sensor.waste_bio_tonne.attributes.display_text}}"
papiertonne_text:
value_template: "{{states.sensor.waste_papier_tonne.attributes.display_text}}"
problemstoffmobil_text:
value_template: "{{states.sensor.waste_problemstoffmobil.attributes.display_text}}"
restmuell_text:
value_template: "{{states.sensor.waste_rest_mull.attributes.display_text}}"
leinich
December 30, 2018, 1:49pm
8
Thanks a lot for the additional help.
For Landkreis Böblingen (lrabb) I pubished the script under:
Just adjust line 66 and 67 to your
Galdan
December 30, 2018, 4:12pm
9
Wow, that are major improvements. Is it OK if i adapt my script with your changes and update the snippet on my blog? I would of course link back to this thread here.
Thanks and cheers
Tom
Robin
December 30, 2018, 4:25pm
10
Yes, of course it’s okay.
Galdan
December 30, 2018, 5:21pm
11
Great, thanks!
I changed my blog post and also added my notification configuration (i get one day before pick up an Telegram notification to bring the rubbish out).
Changed blog post should be online in few minutes…
Cheers
Tom
Hi together,
thanks for the nice write up! Was interested in a way to track garbage collection for my place and managed to setup the component. Still, I am currently struggling with getting the right IDs for my location.
Could someone give me a hnit on how to get them, as I was not able to extract them from either Homepage or App. Or did I miss some point in the instruction?
Thanks in advance!
Galdan
January 1, 2019, 3:29pm
13
Hi,
from which city are you? Can you give me the link where the “Abfallkalender” can get generated for your city. I try to generate the right ID’s for you.
Cheers
Tom
Hi Tom,
thanks for your offer. It would be the following site: https://www.wuerzburg.de/themen/umwelt-verkehr/vorsorge-entsorgung/abfallkalender/index.html
But if I got the right impression, they are using a different API to generate the calendar. So it seems I will have to do some work on my own
Regards,
Nils
Galdan
January 1, 2019, 7:00pm
15
Hi Nils,
you are right, it seems they use something proprietary. But as far as I see its fairly simple to extract the data from them. If you open the Developer Tools in Google Chrome on the page, and then select the correct values, you get an Ajax request to this URL (I took random data):
https://www.wuerzburg.de/themen/umwelt-verkehr/vorsorge-entsorgung/abfallkalender/index.html?_func=evList&_mod=events&ev[start]=2018-12-24&ev[end]=2019-02-03&ev[cat]=&ev[subcat]=&ev[addr]=19939&ev[search]=&_y=2019&_m=01&recon=p6ehg218vh6hks4rdi9illmi98&_dc=1546369043376
If you open the URL you get the data without further manipulation.
Cheers
Tom
leinich
January 2, 2019, 12:27pm
16
I uploaded a small update to reflect an issue with “in 1 Tagen” which is incorrect.
The code now adds heute/today, morgen/tomorrow or “in X Tagen”/“in X days”
taddeuz
(Philip Taddey)
January 2, 2019, 1:18pm
17
Is it possible to read the dates from a pdf or a ics export file?
Galdan
January 2, 2019, 1:32pm
18
you dont need to, you just need to replace the “pdf” or “ics” in the request URL with “csv”.
taddeuz
(Philip Taddey)
January 2, 2019, 1:40pm
19
Galdan
January 2, 2019, 1:56pm
20
I tried to manipulate the URL and it seems you cant get CSV files with this service.
I think its quite hard to parse PDF’s. I reckon its easier you parse the ICS file. But you need to write a custom parser for this.
Sorry I cant help you here.