How to measure Integration of rain pulse counter into daily value

Hi!
I build myself a rain gauge with a sensor from China like this https://www.aliexpress.com/item/1000001838878.html and a NodeMCU running ESPHome.
This is part of the ESPHome config:

sensor:
  - platform: pulse_counter
    pin:
      number: GPIO12
      inverted: True
      mode: INPUT_PULLUP    
    unit_of_measurement: 'mm'
    name: 'Rainfall'
    filters:
      - multiply: 0.367

Everything runs fine, and I get the values in HA, but I would like to have them also as a daily value:
As a “Daily amount of rainfall”, to present in a graph. This means: every night at 0:00 start with 0 (zero) and accumulate all values throughout the day up until the end of the day.
I thought the Integration Sensor (https://www.home-assistant.io/components/integration/) could do this, but this one will not (never) reset it’s value, it is only accumulating. So this won’t do the job.

What is the best way to calculate / integrate into daily values?

1 Like

There’s a python script you can use to reset the value of your sensor:

1 Like

Thanks!
This works, but I think there should be a better and cleaner way to get daily values…
Thanks anyway!

Well, it seemed to work but it doesnt.
It does reset the state of the sensor, but only until a new value of the source sensor. Than the Integration sensor adds the new value to the old value of before the reset.
So the reset is not permanent enough. Or am I doing something wrong?

Hi @gdschut did you ever figure out how to get the daily total rainfall?

I think this is the integration you are wanting.

2 Likes

Hi!
I have this working using:

utility_meter:
# Rain
  rain_daily:
    source: sensor.neerslag_2
    cycle: daily

:slight_smile:

2 Likes

Please mark Nicks post as the solution.

2 Likes

How did you plug this sensor on your NodeMCU ?

Did you use a 15kohms resistance between VCC & Data pins ?

No, just directly on the nodemcu like this:

1 Like

Sorry can’t see well the pinout … green is plugged on TX and red on PIN D5 ?

One on GND and one on D6 (GPIO 12). It is a switch so you can exchange the wires if you like. Check the pinout here https://esp8266-shop.com/esp8266-guide/esp8266-nodemcu-pinout/.
You can use other GPIO as well but it should be consistant with your ESPHome config of course.

Thanks …

I am using an ESP32 but it’s easy to adapt… I read somewhere a source with a pullup resistance… and it was not working with. I will try this more easy setup.

1 Like

@gdschut How did you manage to find this particular multiplier (0.367). Is it return data in mm per hour?

It is a calculated value of the rainfall in mm for every pulse that is counted. It is calculated by measuring the area the rain is collected, and by testing with 200ml water how many counts there were. It seems a valid value for my sensor, but Im not sure yours would have the same dimensions.

area: 4,9 x 110mm,
200ml -> 101 pulses.
1mm rainfall = 5,39 ml.
1ml = 0,1855 mm.
1 pulse = 1,98 ml.
1 pulse = 0,367 mm rain.

Thx @gdschut for your reply. I have exactly the same rain gauge bought from the same Aliexpress seller. I wired it pretty much the same as you, one wire to GPIO14 (D5) other to GND. The only difference is about pin number
When I pour out 200ml of water into the bucket it counts as 187-192 pulses. Much more than yours. I think it is overcounting pulses. If I trigger shoulders manually it count as more than one pulse.

[21:55:58][D][pulse_counter:159]: 'Rainfall': Retrieved counter: 187.00 pulses/min
[21:55:58][D][sensor:092]: 'Rainfall': Sending state 68.62901 mm with 2 decimals of accuracy

Do you have any idea how to debounce readings?

You can use delayed_on and/or delayed_off to debounce the input.

Thanks @tom_l
I got it working and seems the correct number of On values are counted
How to count the ONs and publish as number of pulses (counts) per minute?

After an update of the esphome platform and an update of the rain sensor code I noticed several false positive pulses counted.
I did some experiments and the below code might not be optimized, at least it seams to deliver good values :slight_smile:

I also tried to debounse the pulse counter, but could not get it working.

Mind the use of D6 as binary sensor, D7 as switch and D8 as pulse counter, where D7 and D8 are connected with a wire. D6 has the connection with the actual rainsensor (switching to ground).

binary_sensor:
  - platform: gpio
    pin:
      number: D6
      inverted: True
      mode: INPUT_PULLUP 
    name: "${friendly_name} Rainfall Pulse"
    filters:
      - delayed_off: 10ms
    on_press:
      then:
        - switch.turn_on: tmp_switch
        - delay: 1s
        - switch.turn_off: tmp_switch

sensor:
  - platform: pulse_counter
    pin:
      number: D8
    unit_of_measurement: 'mm'
    name: "${friendly_name} Rainfall"
    filters:
# opp:4,9x110mm, 200ml -> 101 pulses. 1mm rainfall = 5,39 ml. 1ml = 0,1855 mm. 1 pulse = 1,98 ml. 1 pulse = 0,367 mm rainfall.  
      - multiply: 0.367

switch:
  - platform: gpio
    pin: D7
    name: "${friendly_name} tmp_switch"
    id: tmp_switch

Hi everyone!

My first home assistant post here! :slight_smile: I had the same issue, but I did some tests to check how the filter works. I think the problem is that you are counting pulses using the rising edge, and in the pulse_counter documentation it says clearly it has to be on falling edge:

internal_filter ( Optional , Time): If a pulse shorter than this time is detected, it’s discarded and no pulse is counted. Defaults to 13us . On the ESP32, this value can not be higher than 13us , for the ESP8266 you can use larger intervals too. If you enable this, set up the count_mode to increase on the falling edge, not leading edge. For S0 pulse meters that are used to meter power consumption 50-100 ms is a reasonable value.

So, I wrote a simple code to count values:

sensor:
  - platform: pulse_counter
    name: "rain_gauge"
    pin:
      number: D5
      mode: INPUT_PULLUP    
    update_interval: 1s
    count_mode:
      rising_edge: DISABLE
      falling_edge: INCREMENT
    internal_filter: 13us
    filters:
      - multiply: 0.0166666666666666666667

In this way, it’s shown the pulse count per second, every second. Then, using the function generator of my Picoscope, I generated a signal simulating some bouncings:

It’s counting 4, which is correct, since I have the filter to 13us and my shortest pulse is 17us long. Then I changed the filter time to 10ms, and now it’s counting 1 even if I send 2 pulses together. (Sorry, I uploaded more oscilloscope captures, but it’s telling me I can’t upload more than 1 image because I’m a new user… :man_facepalming:)

The limit where it counts 2 pulses is when I split the pulses about 12ms.

So, my final code is as follows:

sensor:
  - platform: pulse_counter
    name: "rain_gauge"
    pin:
      number: D5
      mode: INPUT_PULLUP    
    update_interval: 60s
    count_mode:
      rising_edge: DISABLE
      falling_edge: INCREMENT
    internal_filter: 10ms
    unit_of_measurement: "mm"
    icon: "mdi:water"
    filters:
      - multiply: 0.359254458

I guess it would be nice if there was some option to prevent sending data if it counts zero, I think it’s not much useful to fill the database with zeros.

Hope to help!

2 Likes