☔ DIY Zigbee rain gauge

Good to hear it’s working well!

For the temperature sensor: from what I read it’s not accurate.

hello the sensor works perfectly but I have a problem restarting zigbee2mqtt or home assistant calculates an opening for me, I read some suggestions in the post but they did not help, how can I solve?

Have the same issue. Looking to find a solution as well.

I cannot reproduce this behaviour with ZHA, but I think it’s probably Z2M reporting an off state when restarting or reconnecting with home assistant.

Ive also installed the reverted Sensor from JohannCR , but at the Moment it will count one flip per restart.

For Zigbee2MQTT Sensor the following Configuration can be used to be restart-safe:

sensor:
- platform: template
  sensors:
    regensensor_contact_template:
        friendly_name: "Regensensor Contact Template"
        value_template: "{{ state_attr('binary_sensor.regensensor_contact', 'contact') }}"
- platform: history_stats
  name: Regensensor FLIPS Heute
  entity_id: sensor.regensensor_contact_template
  state: True
  type: count
  start: '{{ now().replace(hour=0, minute=0, second=0) }}'
  end: '{{ now() }}'
- platform: history_stats
  name: Regensensor FLIPS 48h
  entity_id: sensor.regensensor_contact_template
  state: True
  type: count
  start: '{{ as_timestamp(now())-172800 }}'
  end: '{{ now() }}'
- platform: template
  sensors:
    regenmenge_heute:
      friendly_name: Regenmenge Heute
      unit_of_measurement: mm
      value_template: >-
        {% set count = states('sensor.regensensor_flips_heute') | int %}
        {% set mm_per_pulse = 0.2794 %}
        {% set mm = count * mm_per_pulse %}
        {{ mm|round(1, 'floor') }}
      availability_template: >-
        {{ states('sensor.regensensor_flips_heute') | int(-100) != -100 }}
    regenmenge_48h:
      friendly_name: Regenmenge 48h
      unit_of_measurement: mm
      value_template: >-
        {% set count = states('sensor.regensensor_flips_48h') | int %}
        {% set mm_per_pulse = 0.2794 %}
        {% set mm = count * mm_per_pulse %}
        {{ mm|round(1, 'floor') }}
      availability_template: >-
        {{ states('sensor.regensensor_flips_48h') | int(-100) != -100 }}

A few words on why this configuration is restart-safe. When Home Assistant is restarted, Zigbee2MQTT sensors may have a wrong state for a few seconds. In our case, the sensor assumes the “Closed” state, although the rain sensor is “Open” in the idle state. Shortly after the restart, however, the correct “Open” state is reported and thus a change from “Closed” to “Open”. This is counted by the History Stats Sensor and that’s why each restart causes the sensor to increase.

In my configuration, the state of the door sensor is not evaluated, but its attribute “contact” is evaluated using a template sensor. This attribute is unknown for the period of time in which the state is not correct and then changes to the correct value. So there is no longer a time with incorrect value, but only with an “unknown” value. This means that “true” (closed) values can now be counted correctly, since the sensor only changes once from “false” (open) to “unknown” to “false” (open) during a restart.

4 Likes

I tried with this configuration but on restart it always counts an opening

@roberto87, not sure if other people tried this, but I just did a clean “copy/paste” of the code of @AndrejDelany and after a couple of “restarts” stays the correct value.
Did a couple of restarts since the first restart also the original sensor did not change.
But with most of the restarts the original sensor increases with one, whilst the template keeps the correct value. What I did in a previous attempt was check the “retain” option in zigbee2mqtt in the Aqara contact I use in the rain-sensor
Thanks @AndrejDelany, only had to adjust a little for “local language” but happy with how it works.

Fall has begun :upside_down_face:

1 Like

Damn, fixed it!

Nice Solution to evaluate the contact Attribute
Solved the Problem in a brilliant manner :ok_hand:

1 Like

It’s been almost a year ago that I made this post and installed it in my garden, and I’m happy to announce it’s still working flawlessly. Didn’t even had to change the battery yet.

Same, over 1 metre of rain and still going strong :grin::ok_hand:

1 Like

That’s A LOT! Lol

1 Like

Not been going as long as you (about 6 months) but yes, a brilliant idea that works perfectly.

Quick question: How do you collect a years worth of data? Surely your recorder isn’t set for a year?

I use the utility meter

utility_meter:
  rain_week:
    source: sensor.rainfall_today 
    cycle: weekly
  rain_month:
    source: sensor.rainfall_today 
    cycle: monthly
  rain_year:
    source: sensor.rainfall_today 
    cycle: yearly

Where sensor.rainfall_today is the sensor that measures the daily rainfall in mm or inches. It takes some time to populate the values.

4 Likes

utility_meter

Of course :man_facepalming:

Thanks!

1 Like

Hello!
Thanks for sharing this very nice rain gauge project!!

Instead of solder on the original PCB, I designed a small support for Aqara Door sensor (V2) if you want to put it directly into the rain gauge ^^

Not perfect I guess, but it’s one of my first designs ^^

5 Likes

FYI binary sensors should soon be restart safe according to this thread :

2 Likes

Thats really nice, that will solve the false tips for z2m users, right?

Thanks for making a GitHub issue and bumping it now and then.

the template will fail to render in HA Core 2022.1:

(error from the log)

Template warning: ‘int’ got invalid input ‘unknown’ when rendering template ‘{% set count = states(‘sensor.regenmesser_tips’) | int %} {% set mm_per_pulse = 0.30303 %} {% set mm = count * mm_per_pulse %} {{ mm|round(1, ‘floor’) }}’ but no default was specified. Currently ‘int’ will return ‘0’, however this template will fail to render in Home Assistant core 2022.1

what would be the best solution to cover it? this is the related part:

sensor:
  - platform: template
    sensors:
      rainfall_today:
        friendly_name: Rainfall today
        unit_of_measurement: mm
        value_template: >-
          {% set count = states('sensor.raingauge_tips') | int %}
          {% set mm_per_pulse = 0.30303 %}
          {% set mm = count * mm_per_pulse %}
          {{ mm|round(1, 'floor') }}