How to measure Integration of rain pulse counter into daily value

Nice post @gdschut , i used the code from @bernat.albet and so far so good, but how do you all work with deep_sleep? after around 30 secs and pushing all other sensor data, my wemos is going to deep_sleep. It seems because of that not always rain is passed, because it is not counted while it was online.

my output is giving this:

[12:01:14][D][pulse_counter:159]: 'SolarWeatherStation Rain': Retrieved counter: 30.00 pulses/min
[12:01:14][D][sensor:092]: 'SolarWeatherStation Rain': Sending state 10.77763 mm with 2 decimals of accuracy

Is this correct? because i only flipped the Rain sensor just once! now it is giving 30 pulses? 10 mm ?

Sorry, I cannot help you with deepsleep issues. I use NodeMCU and it is never going to sleep.

no problem. How about the measuring?
Perhaps i donā€™t understand the calculations, but my output is something around the 120-190 pulses/min with the rain sensor and around 50-80 mm sending to Home assistant. But , really? 50-80 mm? if i read the local weather authority they talk most of the times about 0,5 - 3 mm ?

or does the 'update_interval: 2s ā€™ which i have now need to be at 60 sec?

The calculation depend on the type of sensor, you should calibrate it yourself if you have another type of sensor. The update interval should be the times HA is updated about the value.
To be honest I changed the whole setup to be more independant from network troubles. I do pulse counting and storing on the ESP side, and update an HA sensor from the ESP In HA I calculate Daily Rain. My esp yaml:

substitutions:
  name: weathersensor
  friendly_name: Weathersensor

esphome:
  name: ${name}
  platform: ESP8266
  board: nodemcuv2

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

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

i2c:
   - id: bus_a
     sda: D2
     scl: D1
     scan: True

# D3, D4, D8 are OUTPUT ONLY
dallas:
  - pin: D6

time:
  - platform: homeassistant
    id: homeassistant_time
    on_time:
      - seconds: 0
        minutes: 0
        hours: 0
        then:
           - homeassistant.service:
               service: input_number.set_value
               data:
                 entity_id: input_number.rain_pulse_count
                 value: !lambda return id(pulse_count_int);
           - lambda: |-
               id(pulse_count_int) = 0;

interval:
  - interval: 1min
    then:
      - homeassistant.service:
          service: input_number.set_value
          data:
            entity_id: input_number.rain_pulse_count
            value: !lambda return id(pulse_count_int);
      - logger.log:
          format: "The global var pulse_count_int has value %d "         
          args: [ 'id(pulse_count_int)']

globals:
   - id: pulse_count_int
     type: int
     restore_value: yes
     initial_value: '0'

binary_sensor:
  - platform: gpio
    pin: D5
    name: "${friendly_name} rain sensor"
    filters:
      - delayed_off: 100ms
    on_press:
      then:
        - lambda: |-
            id(pulse_count_int) += 1;
        - homeassistant.service:
            service: input_number.set_value
            data:
              entity_id: input_number.rain_pulse_count
              value: !lambda return id(pulse_count_int);

sensor:
  - platform: bmp280
    temperature:
      name: "${friendly_name} BMP280 Temperature"
      oversampling: 16x
    pressure:
      name: "${friendly_name} BMP280 Pressure"
    address: 0x76
    update_interval: 60s
    i2c_id: bus_a


  - platform: adc
    pin: A0
    name: "${friendly_name} Illuminance"
    unit_of_measurement: lx
    filters:
      - lambda: |-
          return (x / 10000.0) * 2000000.0;


  - platform: dallas
    address: 0x390115906EAEFF28
    name: "${friendly_name} Temperature" 


  - platform: wifi_signal
    name: "${friendly_name} WiFi Signal Strength"
    update_interval: 60s

  - platform: uptime
    name: "${friendly_name} Uptime Sensor"


text_sensor:
  - platform: version
    name: ${friendly_name} ESPHome Version

switch:
  - platform: restart
    name: "${friendly_name} Restart"
    icon: "mdi:restart"

  - platform: gpio
    name: "${friendly_name} Relay"
    pin: D4


My HA sensor for Daily Rain:

- platform: template
  sensors:
    daily_rain:
      friendly_name: "Daily Rain"
      value_template: '{{(states.input_number.rain_pulse_count.state | float * 0.367) | round(1) }}'
      unit_of_measurement: "mm"
      icon_template: mdi:weather-pouring

This gives me good values, comparable to the weatherforecast, and it has no issues with instable network.

Thanks for answering. I have the same sensor as you had from AliExpress. How do you compare an output as 60-80 mm to the real weather data like 1-3mm?

I did by calculating the top area of the sensor.
Than I measured 200ml of water and dripped it slowly into the sensor, counting the wips of the sensor.
area: 49mm 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.
:slight_smile:

I understand but would assume when I just make 1 flip so 1 pulse it should calculate 0,367mm isnā€™t ?

Correct! At least for my sensor it is.

why are you using delayed_off as 100ms?
on my sensor (identical with yours) flips are much faster than 100ms so if I keep this number, it discards a lot of transitions (pulses).

For now I removed completely the delayed_off and it seems to count all pulses (I also have a pull-up between D5 and 3.3v)

It works different, read about it here: Binary Sensor Component ā€” ESPHome
I never experienced missed pulses, but very rare I have false positives, but I still do not know what caused that.

What do you mean it works differently?
Line stays on ON. When a pulse occurs, it goes to OFF for the duration of the pulse, then back to ON.
So delayed_off simply requires the OFF to stay for 100ms in this case. Meaning that the pulse must be at least 100ms long, otherwise is completely discardedā€¦
And I am seeing much shorter ā€œoffā€ pulses - probably 3 or 10 times fasterā€¦

From documentation: ā€œOr in other words: Only send an OFF value if the binary sensor has stayed OFF for at least the specified time periodā€

Hi Gerben,
Iā€™m a newbie in Home Assistant. Im trying to migrate from Domoticz)
My pair of espeasy and Domoticz worked flawless for years. I found your setup for the rain gauge. Unfortunately, i have not any date. Iā€™m sure that iā€™m doing something wrong. Therefore pls, sorry for silly questions)

I have created the country via Helpers, however it looks like the HASS doesnā€™t see it:
What I find in the log:

Unable to find referenced entities input_number.rain_pulse_count

Do you know where should I dig?

I use normally off, and the pulse is on.

Hi, no problem, and welcome to Home Assistant!
I think I would first check under developer tools if the entity really exist. Perhaps a typo in the name?
Than check in configuration, integrations if your esp has connection with HA.
With ESPhome you can monitor your esp with ā€œlogā€ from command line or HA user interface if you use ESPHome addon.
Hope this helps! good luck!

ok, that explains itā€¦ so GND connected to the relay, with a possible pull-down on the pin instead of my pull-up.
Wiring is completely reversed compared with what I have.

In this case delayed_off simply makes sure there are no 2 pulses faster than 100ms (which seem OK at 1 pulse =0.36mm/m2. Faster than 1 pulse every 100ms means actually more than 3 liters of rain per second for each m2 and if I had that I would have bigger drowning problems than the functionality of this sensor).

Thank you for clarifying this for me.

1 Like

@gdschut Hi, may I know why you changed from pulse_sensor to binary_sensor?

I simply could not get it to work properly, so I changed the setup so all the calculations are done bij HA instead of on the ESP. There is now a global var on the ESP counting all the pulses for the day, and uploading the number into HA by using a service call every minute or so, so HA knows all the actual pulses of today and calculates the amount of rain. Now this makes it also not depending on a stable network.
The count of pulses is reset by midnight, to start on the new day with zero again.

1 Like

How did you create the HA sensor for Daily Rain, sorry I am a little noob :slight_smile:

Edit: now with the right code
Found out I had two sensors (daily_rain and rain) and I thought I was using the daily_rain but in fact it was the other. For strange reasons I only get this working in configuration.yaml instead of in sensors.yaml. Does anybody know how to move this to sensors.yaml???

In configuration.yaml:

template:
  - sensor:
      - name: rain
        state: '{{(states.input_number.rain_pulse_count.state | float * 0.367) | round(1) }}'
        unit_of_measurement: "mm"
        state_class: measurement

Now with the new statistics it is also nice to add this sensor with a statistics lovelace card :slight_smile:
You need the state_class: measurement to get it into the statistics.
Another strange thing: No icon is accepted for this type of sensorā€¦ (donā€™t know why)

As you can see the sensor I use is named ā€œrainā€ only, as it increases over the day, and is in fact a daily rain sensor. I use the utility meter for monthly calculations.

Haha, thanks, very stupid but I didnā€™t immediately think of that place :slight_smile: and that while it is already completely full. But thanks a lot!! Gonna try it right away :slight_smile: