Case study

Rain gauge with ESPHome in Home Assistant

A 3D-printed tipping-bucket rain gauge, connected to the smart home via an ESP8266 and ESPHome – with a calibration that turns counted tips into genuinely reliable millimetres of rainfall.

Role
Build, electronics, configuration, calibration
Hardware
Wemos D1 mini (ESP8266), reed switch, 3D-printed tipping mechanism; BME280 in the same housing (work in progress)
Software
ESPHome, Home Assistant
Connectivity
Wi-Fi, native Home Assistant API (no cloud service)
Author
Julian Haag, state-certified electrical engineering technician

The task

Weather data from the internet applies to the next town, not to your own garden. Anyone who wants to automate irrigation, shading or a cistern needs the rainfall figure on site – as a dependable measurement, not an estimate. The goal was a standalone rain gauge that measures locally, works without any cloud service and delivers clean long-term statistics in Home Assistant.

Implementation

The mechanism uses the classic tipping-bucket principle: a funnel collects the water and feeds it into a two-chamber seesaw. Once a chamber is full it tips over, empties and triggers a reed switch. Each tip corresponds to a defined volume of water – the number of pulses gives the rainfall.

The housing parts come from an open-source 3D model (see sources below) and were printed in-house. A Wemos D1 mini reads the reed switch via an interrupt-capable GPIO with an internal pull-up. The firmware is built with ESPHome; the connection to Home Assistant runs over the native API on the local network – no cloud service and no manufacturer account.

All parts of the model are printed: funnel, base, bucket, mesh, electronics lid, mounting and cable plug. Three pins are used on the D1 mini. The printed D numbers are not the GPIO numbers: D1 is GPIO5, not GPIO1. Anyone entering the GPIO number in ESPHome has to convert.

Pin mapping on the Wemos D1 mini
LabelGPIOFunction
D5GPIO14reed switch of the bucket, internal pull-up, inverted, 10 ms debounce
D2GPIO4I²C SDA for the BME280 (work in progress)
D1GPIO5I²C SCL for the BME280 (work in progress)
Rain gauge: from the funnel to statistics in Home Assistant Top, the printed mechanics: the funnel with 124.88 square centimetres of inner area fills a two-chamber tipping bucket that triggers a reed switch when it tips. Bottom, the electronics: a Wemos D1 mini reads the contact on D5, which is GPIO14, with internal pull-up and 10 milliseconds of debouncing, ESPHome sends it over the native API on the local network to Home Assistant, where it becomes a cumulative counter with a state class. Below, the formula: millimetres per tip equals tipping volume in millilitres times 0.080076; 500 millilitres equal 40.04 millimetres, and the tips are counted in Home Assistant. mechanics, 3D-printed funnel inner 124.88 cm² tipping bucket 2 chambers, tips reed switch pulse when tipping Wemos D1 mini D5 = GPIO14, pull-up, 10 ms ESPHome native API, local network Home Assistant cumulative counter with state_class electronics, local mm per tip = tipping volume [ml] × 0.080076 500 ml = 40.04 mm · count tips in Home Assistant
Structure of my own rain gauge, without a cloud. The area is measured at the inner funnel edge; the tipping volume is measured rather than assumed, because print tolerances shift it.

Calibration – where accuracy is won or lost

This is the point where most rebuilds stay inaccurate: a rain gauge counts tips, but what you need is millimetres. The conversion factor depends on two quantities – the tipping volume of the bucket and the collecting area of the funnel.

What counts is the inner funnel edge, not the outer dimension: water landing on the 1.2 mm wall crown splits roughly in half. I did not estimate the area; I cut it from the funnel’s STL file and measured it.

Rain gauge funnel: which area counts Top view of the rectangular funnel with rounded corners. Outside 142.0 by 92.0 millimetres, that is 130.3 square centimetres; this area does not count. Inside, at the inner edge, 139.6 by 89.6 millimetres, highlighted as the catchment area of 124.88 square centimetres. The wall is 1.2 millimetres thick and exaggerated in the drawing; water on its crown splits roughly in half. catchment area inner edge 139.6 × 89.6 mm 124.88 cm² 142.0 mm outer 92.0 mm outer outer edge 142.0 × 92.0 mm = 130.3 cm² – does not count wall 1.2 mm (exaggerated): water on the crown splits in half
Cut from the funnel’s STL file and measured, with the rounded corners included. That is why both areas are slightly below the plain rectangle.

One millimetre of rainfall is one litre per square metre. One millilitre on 124.88 cm² therefore gives 0.080076 mm, and:

mm per tip = tipping volume [ml] × 0.080076

At 7.0 ml tipping volume that is 0.561 mm per tip, at 8.0 ml 0.641 mm. The difference is bigger than it looks: anyone calculating with 0.64, i.e. 8 ml, whose bucket actually tips at 7 ml reports about 14 % too much rain.

So do not rely on a nominal volume – manufacturing tolerances in printing shift the tipping volume noticeably. The check: pour 500 ml of water into the funnel as a thin stream, do not tip it in or it spills over, and count the tips that arrive in Home Assistant – not the mechanically visible ones. With this area, 500 ml equal 40.04 mm of rain.

mm per tip = poured ml × 0.080076 ÷ counted tips

About 62 to 63 tips mean 8 ml per tip, about 71 to 72 tips 7 ml. My own 500 ml test is still to come; until then the device uses the provisional value for 7 ml. I will add the result here.

The distinction matters: on some tipping mechanisms the reed switch only triggers on every second movement, depending on magnet position. Counting mechanically hides a factor of two – and the gauge permanently reads half the real value. Calibrating against the pulses actually counted rules this error out.

Integration into Home Assistant

A reading on its own does not make a useful analysis. Three things separate a number on a dashboard from real statistics:

The core of the ESPHome configuration, trimmed to bucket, total counter and sum. Wi-Fi and API keys live in secrets.yaml, not in plain text:

globals:
  # mm per tip = tipping volume [ml] × 0.080076
  # provisional 7 ml → 0.56 until the 500 ml test is done
  - id: rain_mm_per_tip
    type: float
    restore_value: no
    initial_value: '0.56'
  # total counter: survives a restart, not a power cut
  - id: rain_total_count
    type: int
    restore_value: true
    initial_value: '0'

binary_sensor:
  - platform: gpio
    name: "Regenmesser-Eimer"
    pin:
      number: 14          # D5 on the Wemos D1 mini
      mode: INPUT_PULLUP
      inverted: true
    filters:
      - delayed_on: 10ms  # debounce the reed switch
      - delayed_off: 10ms
    on_press:
      then:
        - globals.set:
            id: rain_total_count
            value: !lambda 'return id(rain_total_count) + 1;'

sensor:
  - platform: template
    name: "Niederschlag gesamt"
    unit_of_measurement: "mm"
    device_class: precipitation
    state_class: total_increasing
    accuracy_decimals: 2
    update_interval: 60s
    lambda: |-
      return id(rain_total_count) * id(rain_mm_per_tip);

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

api:
  encryption:
    key: !secret api_encryption_key

The total counter is kept with restore_value in the ESP8266’s RTC memory – that is where ESPHome stores it on this chip unless restore_from_flash is enabled. It survives a restart but not a power cut. That is deliberate: with total_increasing, Home Assistant treats a jump to zero as a counter reset, and the flash memory is spared.

Daily, monthly and yearly totals are created in Home Assistant. The entity ID follows from the device and sensor name and has to be adapted to your own installation; delta_values is not needed because the source already is a cumulative counter:

utility_meter:
  # adapt the entity ID to your installation
  niederschlag_taeglich:
    source: sensor.regenmesser_niederschlag_gesamt
    cycle: daily
  niederschlag_monatlich:
    source: sensor.regenmesser_niederschlag_gesamt
    cycle: monthly
  niederschlag_jaehrlich:
    source: sensor.regenmesser_niederschlag_gesamt
    cycle: yearly

Extension: temperature, pressure, humidity – work in progress

The same housing holds a BME280 on the I²C bus, address 0x76. Two points for rebuilding:

As of today the module is faulty. ESPHome reports “SCL is held low” and “Found no devices” and marks the sensor as FAILED. Measuring gave about 20 Ω between SCL and ground directly on the module, but no short at the D1 mini. So the fault is on the module, probably a solder bridge between the adjacent GND and SCL pins. The rain gauge keeps running unaffected.

What cost time when flashing and debugging

The counting is quick to build. The time went elsewhere:

What this project demonstrates

The rain gauge is a small device, but it stands for the approach I apply to larger systems too: local instead of cloud, open standards instead of vendor lock-in – and a measurement only counts once it has been checked against reality. That same diligence later decides whether energy monitoring delivers dependable figures or merely plausible-looking ones.

Sources & templates used

This project builds on open-source work by others. The sources used:

The licence terms of each source continue to apply unchanged. Please check them directly with the provider before rebuilding.

Links marked with an asterisk are affiliate links. As an Amazon Associate I earn from qualifying purchases; the price is unchanged for you. What I recommend does not depend on it.

Would you like to integrate your own sensors, a weather station or irrigation into Home Assistant? Get in touch – the initial consultation is free.