A beelogger on ESP32 basis, part 4: Test setup: The rain sensor

I bought a rain gauge from china, it’s a spare part for the “misol” weather stations, just search for “rain gauge sensor aliexpress” for the product, it was less than 15€. Technically it’s just a hall sensor.

For my test-setup, I simply cut off the connector and connected the wires directly to GND and GPIO4 and used the internal pull-up resistor:

Adding it to esphome is straight forward and easy. I’m using the pulse counter component, it uses the pulse counter peripheral of the ESP32 and works in standby-modes:

substitutions:
  update_interval: 5s

sensors:
 - platform: pulse_counter
    name: "Rainfall Pulse Counter"
    id: rainfall_pulse_counter
    use_pcnt: true
    count_mode: 
      falling_edge: INCREMENT
      rising_edge: DISABLE
    pin:
      number: GPIO4
      inverted: true
      mode:
        input: true
        pullup: true
    unit_of_measurement: "mm"
    icon: "mdi:water"
    update_interval: ${update_interval}
    filters:
      # I'm unsure what value is correct, I'll have to measure it myself
      - multiply: 0.2794
      # - multiply: 0.367
      - debounce: 10ms
    total:
      name: "Rainfall Total Pulses"

  - platform: template
    name: "Hourly Rainfall"
    id: hourly_rainfall
    unit_of_measurement: "mm"
    icon: "mdi:weather-rainy"
    update_interval: never

  - platform: template
    name: "Daily Rainfall"
    id: daily_rainfall
    unit_of_measurement: "mm"
    icon: "mdi:weather-pouring"
    update_interval: never

  - platform: template
    name: "Rainfall Accumulator"
    id: rainfall_accumulator
    unit_of_measurement: "mm"
    icon: "mdi:counter"
    update_interval: never
    internal: true  # Hide from Home Assistant

interval:
  # Every minute: add current pulse count to accumulator
  - interval: 60s
    then:
      - lambda: |-
          float current = id(rainfall_pulse_counter).state;
          id(rainfall_accumulator).publish_state(id(rainfall_accumulator).state + current);

  # Every hour: publish hourly total and reset accumulator
  - interval: 1h
    then:
      - lambda: |-
          float total = id(rainfall_accumulator).state;
          id(hourly_rainfall).publish_state(total);
          id(rainfall_accumulator).publish_state(0);

  # Every day at midnight: publish daily total and reset accumulator
time:
  - platform: homeassistant
    on_time:
      - seconds: 0
        minutes: 0
        hours: 0
        then:
          - lambda: |-
              float total = id(rainfall_accumulator).state;
              id(daily_rainfall).publish_state(total);
              id(rainfall_accumulator).publish_state(0);

The value “0.2794” is explained here in the datasheets I found in this wiki that seem to match my hardware:
datasheet_1
datasheet_2


The value “0.367” is explained here:
https://community.home-assistant.io/t/how-to-measure-integration-of-rain-pulse-counter-into-daily-value/136709

I don’t know yet which one is correct, I’ll do some measurements.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *