I built custom sensors for our home to monitor air quality. The sensors I used (SCD41) have the option to configure ambient pressure to get accurate CO2 measurements. I didn’t plan for the adjustment before and therefore there are no pressure sensors installed in each unit.

So I decided to use the measurement of ambient pressure from the city where I live. These measurements are public on the DWD Website (Deutscher Wetterdienst – Engl. German Weather Service) and updated every hour.

Weather information from DWD

The blog post is divided into three steps:

  1. Configure the web scrapper
  2. Enable ambient pressure as input for the sensors
  3. Create automation to send ambient pressure to sensors

Configure the web scraper

I use the scrape sensor platform to retrieve the information and save the value in Home Assistant. Add the code block to the configuration.yaml and adjust it to your needs. Unfortunately, the DWD website uses the same id on several elements. So I chose to count the td occurrence. The information updates every 5 mins.

sensor:
  - platform: scrape
    resource: https://www.dwd.de/DE/wetter/wetterundklima_vorort/schleswig-    
       holstein_hamburg/hamburg/_node.html
    name: ambient_pressure_hamburg
    select: "td"
    index: 16
    value_template: '{{ value.split(" ")[0]}}'
    unit_of_measurement: 'hPa'
    scan_interval: 300

Now the sensor value can be displayed in a lovelace dashboard.

Lovelace dashboard with ambient pressure

Enable ambient pressure as input for the sensors

Next, each sensor needs an input number to change the configuration remotely via Home Assistant. Copy the configuration to the YAML file of your Esphome device. The ambient pressure for the SCD41 sensor can be updated via set_ambient_pressure_compensation in bar.

number:
  - platform: template
    name: "Ambient pressure"
    id: air_sensor_ambient_pressure
    initial_value: 1005
    min_value: 900
    max_value: 1100
    step: 1
    optimistic: true
    on_value:
        then:
            - lambda: "id(air_sensor_scd41)->set_ambient_pressure_compensation(x / 1000.0);"

sensor:
  - platform: scd4x
    id: air_sensor_scd41
    co2:
      name: "air_sensor_CO2"
    temperature:
      name: "air_sensor_temperature"
    humidity:
      name: "air_sensor_humidity"
    automatic_self_calibration: False
    ambient_pressure_compensation: 1005

The sensors can be updated via the Home Assistant front end.

Create automation to send ambient pressure to sensors

As a final step, I created an automation in Home Assistant to update ambient pressure on each sensor every hour since DWD publishes new measurements every hour. Make sure to check the entity id of the scrape sensor. The id might be different than the id in your Esphome configuration.

alias: update ambient pressues for air sensors every hour
trigger:
  - platform: time_pattern
    hours: "/1"
action:
  - service: number.set_value
    data_template:
      entity_id:
        - number.air_sensor_ambient_pressure
      value: "{{ states('sensor.ambient_pressure_hamburg') }}"
mode: single

Now all sensors receive hourly the current ambient pressure measurement in the city to adjust their CO2 measurements.