Latest from YouTube Channel

UK Pollen Count Forecast in Home Assistant

April 22nd, 2023
YouTube video

It’s getting towards that time of year again where my eyes swell up and I can’t stop sneezing, so wouldn’t it be great if my home automation system knew what the current pollen count was for my area and maybe turn on air purifiers or maybe send me an alert if the forecast is high? Well, if you live in the UK then getting the pollen count via an API is quite tricky. There are a couple of services out there that sometimes work, sometimes don’t, but I’ve discovered a nice reliable way to get that data into Home Assistant.

Where do most people in the UK get their news and weather information from? Yes, the BBC. The BBC weather site also very conveniently provides you with a pollen count level. Now, it’s not a numeric value and it doesn’t separate grass pollen from tree pollen for example, but it does give you a rough idea as to how bad it is outside on a scale from Low to Very High.

Now unfortunately there is no way to grab this data via an official API – even the Met Office doesn’t seem to include this information in their Datapoint API, so the only way I’ve found to get this information is by scraping the BBC Weather web site. Luckily there is a special scraper feature built right in to Home Assistant to help you with this! Before we dive into configuring Home Assistant though, you’re going to need to find your local weather forecast on the BBC Web site.

Visit www.bbc.co.uk/weather and search for your nearest location. I’m going to pick Oxford (https://www.bbc.co.uk/weather/2640729) just as an example during this tutorial.

Now you’ll notice down under the hourly forecast are three diamonds – the first diamond is labelled Pollen. The letters inside that diamond are what we’re planning to grab from this web page. Click in your address bar and copy the URL somewhere safe to use in a minute.

There’s going to be a bit of manual configuration editing required to get this up and running, but don’t worry it’s not difficult. As always, I suggest you install Home Assistant’s File Editor add-on. Navigate to Settings > Add-ons > click on the Add-on Store button in the bottom right, search for “File Editor” and install it. If you toggle the “show in sidebar” option then start the add-on, you should see it appear in the left-hand menu. Click on that to load the file editor. By default it should open your configuration.yaml file but if not click on the ‘browse’ button near the top-left and scroll to find and select configuration.yaml.

Make a little bit of space at the end of that file and paste in the following configuration, making sure that you replace the ‘resource’ URL with the actual URL of the BBC weather forecast local to you.

# ===============================================================
# Web Page Scrapers
scrape:
  - resource: https://www.bbc.co.uk/weather/2639823
    scan_interval: 3600
    sensor:
      - name: "Pollen Count Severity"
        select: "span.wr-c-environmental-data__item.wr-c-environmental-data__item--pollen > span.wr-c-environmental-data__icon-wrapper > span.wr-c-environmental-data__icon-text.gel-brevier-bold"
      - name: "Pollen Count Numeric"
        select: "span.wr-c-environmental-data__item.wr-c-environmental-data__item--pollen > span.wr-c-environmental-data__icon-wrapper > span.wr-c-environmental-data__icon-text.gel-brevier-bold"
        value_template: >-
          {% if value == 'L' %}
            1
          {% elif value == 'M' %}
            2
          {% elif value == 'H' %}
            3
          {% elif value == 'VH' %}
            4
          {% else %}
            0
          {% endif %}
        state_class: measurement

Save the file (floppy disk icon near the top-right) and restart Home Assistant (Developer Tools, check configuration then click on restart if there are no errors). Once it’s back up and running again, you should go and confirm that your new pollen entities are up and running. Navigate to Settings > Devices & Services > Entities (tab at the top). Filter by ‘pollen’ and you should see two entities: Pollen Count Severity; and Pollen Count Numeric.

Pollen Count Severity contains the actual letter(s) scraped directly from the BBC web site. This might be quite nice displayed on your dashboard as it is nice and human friendly. Pollen Count Numeric on the other hand is a numeric value on a scale of 0 to 4, where 0 represents no reading and 4 represents Very High (VH). A numeric reading is much more usable in automations.

Well, what can you do with this information then? I have an air purifier connected to a smart plug so if the pollen count is medium or above then I want that air purifier to switch on in my bedroom before I go to bed. That way it gets a chance to clean the air up a bit. Now obviously I have over complicated the automation a bit because the functionality is split between an automation and a script. So the automation just triggers at set times. If it’s triggered at 9pm then it calls the script and passes it the name of the air purifier that we might want to turn on. At 5am it is triggered again and turns off the air purifier.

Now for the script it starts with a ‘or’ condition. If any one of these conditions are true then the air purifier will be turned on. So I first check the air quality in my local area using the WAQI API data (that’s a story for another day!) If air quality is generally poor (has a WAQI score above 50) then I want it turned on. Next I check to see if it’s between May and July, because if it is then that air purifier must be turned on… I’m guaranteed to need it during those months. And finally you’ll see a check on the pollen count we created earlier – if it’s above 1, so levels 2 to 4, then turn it on!

alias: Air Purifier (Main Bedroom)
description: ''
trigger:
  - platform: time
    at: '21:00'
    id: 'on'
  - platform: time
    at: 05:00
    id: 'off'
condition: []
action:
  - choose:
    - conditions:
        - condition: trigger
          id: 'on'
      sequence:
        - service: script.turn_on_air_purifier_if_air_quality_is_low
          data:
            air_purifier_switch: switch.air_purifier_main_bedroom
    default:
      - service: homeassistant.turn_off
        data: {}
        target:
          entity_id: switch.air_purifier_main_bedroom
mode: single
alias: Turn on air purifier if air quality is low
fields:
  air_purifier_switch:
    description: Switch controlling the air purifier
sequence:  
  - choose:      
      - conditions:
          - condition: or            
            conditions:              
              - condition: numeric_state                
                entity_id: sensor.waqi_mylocalarea_united_kingdom  
                above: "50"
              - condition: template                
                value_template: "{{ now().month >= 5 and now().month <= 7 }}"
              - condition: numeric_state
                entity_id: sensor.pollen_count_numeric                
                above: 1        
        sequence:          
          - service: homeassistant.turn_on            
            target:              
              entity_id: "{{ air_purifier_switch }}"    
    default: []
mode: single
icon: mdi:air-purifier
  • As an Amazon Associate I earn from qualifying purchases.