I saw several pollen sensor implementations, official components or here in the forum. But none showed data for Germany so I came up with my own solution.
In it one can search the subregion_id and the Allergens he is interested in.
I put this in a Rest Sensor and pull out the values for today, tomorrow and the day after tomorrow with a template sensor. But it does not quite work yet.
Can somebody help me out?^^
The rest sensor currently show information in the form of: {'tomorrow': '2', 'today': '2', 'dayafter_to': '1-2'}
- platform: rest
scan_interval: 43200
resource: https://opendata.dwd.de/climate_environment/health/alerts/s31fg.json
name: pollen_101_graeser # Rhein, Pfalz, Nahe und Mosel
value_template: '{% for region in value_json.content -%}{%- if region.partregion_id == 101 %}{{region.Pollen.Graeser}}{% endif -%}{%- endfor %}'
- platform: rest
scan_interval: 43200
resource: https://opendata.dwd.de/climate_environment/health/alerts/s31fg.json
name: pollen_101_birke # Rhein, Pfalz, Nahe und Mosel
value_template: '{% for region in value_json.content -%}{%- if region.partregion_id == 101 %}{{region.Pollen.Birke}}{% endif -%}{%- endfor %}'
- platform: rest
scan_interval: 43200
resource: https://opendata.dwd.de/climate_environment/health/alerts/s31fg.json
name: pollen_101_roggen # Rhein, Pfalz, Nahe und Mosel
value_template: '{% for region in value_json.content -%}{%- if region.partregion_id == 101 %}{{region.Pollen.Roggen}}{% endif -%}{%- endfor %}'
- platform: template
sensors:
pollen_101_graeser_today:
friendly_name: 'Gräser Heute'
value_template: '{{ states.sensor.pollen_101_graeser.value_json.today}}'
pollen_101_graeser_tomorrow:
friendly_name: 'Gräser Morgen'
value_template: '{{ states.sensor.pollen_101_graeser.value_json.tomorrow}}'
pollen_101_graeser_dayaftertomorrow:
friendly_name: 'Gräser Übermorgen'
value_template: '{{ states.sensor.pollen_101_graeser.value_json.dayafter_to}}'
pollen_101_birke_today:
friendly_name: 'Birke Heute'
value_template: '{{ states.sensor.pollen_101_birke.value_json.today}}'
pollen_101_birke_tomorrow:
friendly_name: 'Birke Morgen'
value_template: '{{ states.sensor.pollen_101_birke.value_json.tomorrow}}'
pollen_101_birke_dayaftertomorrow:
friendly_name: 'Birke Ăśbermorgen'
value_template: '{{ states.sensor.pollen_101_birke.value_json.dayafter_to}}'
pollen_101_roggen_today:
friendly_name: 'Roggen Heute'
value_template: '{{ states.sensor.pollen_101_roggen.value_json.today}}'
pollen_101_roggen_tomorrow:
friendly_name: 'Roggen Morgen'
value_template: '{{ states.sensor.pollen_101_roggen.value_json.tomorrow}}'
pollen_101_roggen_dayaftertomorrow:
friendly_name: 'Roggen Ăśbermorgen'
value_template: '{{ states.sensor.pollen_101_roggen.value_json.dayafter_to}}'
Yeah, the state of the rest sensors may look like a map/dictionary, but it’s really a string. I ran into this before. The way I solved the problem was by using the regex_findall_index filter. E.g.:
Nice! Even though this can be made to work with the REST platform, I think this deserves to be a platform on its own. It should not be hard to implement.
I completely agree. But as I never contributed code to an open source project, yet alone in Python, I wanted to try out the “ugly way” first.
I think I will find some free time to tackle this in a few weeks.
One thing which could be improved is the updating. On the site the next update is stated. It is easily parsed and could be used to update the sensor only once a day when there really is news data.
I hacked together a Python library that simply parses the JSON file. It caches the data until the “next update” time has passed. This could be used as the basis for a HA sensor platform.
@eifinger (someone else) may you please post your complete tenplate sensor here.
I am also searching for a solution for germany but unfortunately don’t know python (yet) to code this.
Unfortunately, i only get “Unknown” as values.
Just changed the id (from 101 to 110 mathcing my area) but everything else is still same as your code.
Any hints?
Some weeks ago I already created a custom component for DWD pollen information. You might want to check it out at https://github.com/marcschumacher/dwd_pollen. Let me know if you like it and feel free to open issues!
[jackeroo_marc] I tried your platform. It works great. Had the fear that it would not work without the requirements under Hass.io in Docker. Fortunately, that’s not the case. Great!
As far as I can see in your sensor.py there will just be the values from 0-6 as return? Currently I’m using a template sensor in addition to the REST sensor to give me a readable output instead of the dwd index:
- platform: template
scan_interval: 1800
sensors:
pollen_92_graeser_today:
friendly_name: 'Gräser'
value_template: >
{% if (states('sensor.pollen_92_graeser')|regex_findall_index("'today': '[-0-9]+'")|regex_findall_index("[-0-9]+") == "0-1") %} Keine bis geringe Belastung
{% elif (states('sensor.pollen_92_graeser')|regex_findall_index("'today': '[-0-9]+'")|regex_findall_index("[-0-9]+") == "1") %} Geringe Belastung
{% elif (states('sensor.pollen_92_graeser')|regex_findall_index("'today': '[-0-9]+'")|regex_findall_index("[-0-9]+") == "1-2") %} Geringe bis mittlere Belastung
{% elif (states('sensor.pollen_92_graeser')|regex_findall_index("'today': '[-0-9]+'")|regex_findall_index("[-0-9]+") == "2") %} Mittlere Belastung
{% elif (states('sensor.pollen_92_graeser')|regex_findall_index("'today': '[-0-9]+'")|regex_findall_index("[-0-9]+") == "2-3") %} Mittlere bis hohe Belastung
{% elif (states('sensor.pollen_92_graeser')|regex_findall_index("'today': '[-0-9]+'")|regex_findall_index("[-0-9]+") == "3") %} Hohe Belastung
{% else %} Keine Belastung {% endif %}
But maybe I can also get this now in manipulation the sensor.py. I will see Else I will do it the same way as with the REST sensor.
Edit: RTFM to myself I saw in the documentation that there will be this readable format in the description attribute. This will help.
This is awesome. Exactly what i was looking for. However, where did you select the region? I’m trying to find where you put that but i cant find it… Unless im blind