☔ DIY Zigbee rain gauge

No point of using ESP. Xiaomi door densor battery is lasting year or years. It gives possibility to put the meter right where the rain is, not where the power outlet is.

i had tried to use the original magnet to trigger the aqara reed sensor, but this didn´t worked for me. principally it worked, but it was inaccurate. it triggered the reed only by flipping to one side, but not securly to the other… any issue of not perfect positioning of gauge magnet and aqara reed… had used some of the linked 3dprinted adapters to hold the aqara in place…cant remember ecactly, but it didn´t worked for me, and i didn´t want to spent more time to investigate… so i had soldered original reed of the rain gauge to the aqara, as it was the original solution in this thread(?) and what i had tryed to avoide at the beginning. But at the end this is working well… (but i had broken the aqara reed :cry: too). the benefit of using the reed of the gauge is, the exact placement, and the short cables, who give extra range, when accessing the arara for pairing or battery change, without to dismantle the reed as well… im happy with that solution for a couple of month now :grin:

2 Likes

Good to know, thanks for the update. You just saved me from having the 3D adapter printed.

Just to learn from it: Does the 3D printed adapter press the Aqara PCB against the plastic board towards the magnet?

the adapter is not to press it against, its to hold in position… but yes due to a little bit more Material the pcb is presssed much more than without adapter…

I’m struggling with the calculation area on this. Today we’ve gotten 3 “flips” but my rainfall today is still 0.

In my configuration.yaml I have this

### Rain Guage        
  - sensor:
    - name: Rainfall today
      unit_of_measurement: in
      state_class: total_increasing
      unique_id: rainfall_today
      state: >-
        {% set count = states('sensor.rainsensor_flips') | int(0) %}
        {% set in = count * 0.01193 %}
        {% if count >= 0 %}
          {{ in|round(1, 'floor') }}
        {% endif %}
      # If you have issues with the history sensor doubling after restarting HA, add the line below (@BigG)
      availability: "{{ (states('sensor.rainsensor_flips') not in ('unknown', 'unavailable')) }}"

In my sensors.yaml I have this

- platform: history_stats
  name: Rainsensor flips 
  entity_id: binary_sensor.rain_gauge_opening #The aqara sensor
  state: 'off'
  type: count
  start: '{{ now().replace(hour=0, minute=0, second=0) }}'
  end: '{{ now() }}'

Any ideas what I need to change/fix?

Thanks!

Paste this in the template thing in developer tools and post the outcome.

{% set count = states('sensor.rainsensor_flips') | int(0) %}
{% set in = count * 0.01193 %}
{{ count }}
{{ in }}
{{ in|round(1, 'floor') }}

Also post your logs

I get this from the template tool:
5
0.059649999999999995
0.0

Am I not seeing anything because I haven’t collected enough to round up??

===================================

Which log would you like to see? I’ve searched for “rain” in my log and I get this from this morning in the HA Core log.


2023-12-13 09:15:17.152 WARNING (Recorder) [homeassistant.components.sensor.recorder] The unit of sensor.rain_week (in) cannot be converted to the unit of previously compiled statistics (None). Generation of long term statistics will be suppressed unless the unit changes back to None or a compatible unit. Go to https://my.home-assistant.io/redirect/developer_statistics to fix this

`2023-12-13 09:15:17.152 WARNING (Recorder) [homeassistant.components.sensor.recorder] The unit of sensor.rain_month (in) cannot be converted to the unit of previously compiled statistics (None). Generation of long term statistics will be suppressed unless the unit changes back to None or a compatible unit. Go to https://my.home-assistant.io/redirect/developer_statistics to fix this`

2023-12-13 09:15:17.153 WARNING (Recorder) [homeassistant.components.sensor.recorder] The unit of sensor.rain_guage (in) cannot be converted to the unit of previously compiled statistics (None). Generation of long term statistics will be suppressed unless the unit changes back to None or a compatible unit. Go to https://my.home-assistant.io/redirect/developer_statistics to fix this

I went into Developer statistics and fixed it and am still seeing the same thing.

Yes, try changing the line of code to

{{ in|round(2, 'floor') }}

This will round 2 decimals

You can also remove the floor part if you like

I’ve changed the rounding and the calculation in the template shows:
5
0.059649999999999995
0.06

On my dashboard I’m still getting 0.00 though.

I feel like I’ve got to be missing something really obvious here.

You should also change it in the configuration.yaml for the daily rainfall sensor :slight_smile:

LOL I swear I did!

I went back and checked it and yep, I changed the line:

{{ in|round(2) }}

So then I copied it from the Template window in Developer Tools and pasted and formatted it into the config file again.

Which is when I realized you had a change in there from my code. :woman_facepalming:

Thanks so much for your help!

Just a follow-up note in case someone not-so-great-with-yaml comes and references this. My final code in configuration.yaml was:

### Rain Gauge        
  - sensor:
    - name: Rainfall Today
      device_class: precipitation
      unit_of_measurement: in
      state_class: total_increasing
      unique_id: rainfall_today
      state: >-
        {% set count = states('sensor.rainsensor_flips') | int(0) %}
        {% set in = count * 0.01193 %}
        {{ in|round(2, 'floor') }}
      # If you have issues with the history sensor doubling after restarting HA, add the line below (@BigG)
      #availability: "{{ (states('sensor.rainsensor_flips') not in ('unknown', 'unavailable')) }}"

the additional

{{ count }}
{{ in }}

was used for testing in the Template tool under Developer Tools to see what values were returning for count and in. If you leave them in your configuration.yaml, you’ll get an error because that returns multiple numbers, causing it to be a string instead of an int.

1 Like

my first day of use and the sensor experiences its first storm. it is rain but also hail. Does the internal handling of the sensor work well in that case?

As soon as the hail melts, they will be counted as rain drops

1 Like

Hi,

I’ve made this rain meter and added it to the mqtt broker. I can see it switching but have no idea where to add the code. Should the rainfall today sensor show up in the entities list ? I’ve tried to add the code to configuration.yaml to no avail. Could you please explain ? I am using home assistant in a docker container but by now I have also tried home assistant os. No luck.

If you added the sensor to mqtt you should have a binary sensor where you can see if the state is open or closed. For me it is binary_sensor.rainsensor_on_off. You can see that in the first piece of code.

What you should do is change that piece of code to the name of your sensor which is probably different.

At that moment you can probably copy paste all the other code into the configuration.yaml, but I would suggest you add each codeblock separately and try to understand what it’s doing.

Let me know if you need further help!

rain_today:
friendly_name: Regn i dag
unit_of_measurement: mm
value_template: >-
{% set count = states(‘sensor.regnsensor_vippe’) | int %}
{% if states.sensor.date_time_iso.last_changed - states.sensor.regn_i_dag.last_changed > timedelta(minutes=60) %}
{% set mm_per_pulse = 0.303 %}
{% else %}
{% set mm_per_pulse = 0.30303 %}
{% endif %}
{% set mm = count * mm_per_pulse %}
{% if states(‘sensor.regnsensor_vippe’) != ‘unknown’ %}
{{ mm }}
{% else %}
{{ states(‘sensor.regn_i_dag’) }}
{% endif %}
regn_pr_hour:
friendly_name: “Regn pr time”
unit_of_measurement: ‘mm/h’
value_template: >-
{% if state_attr(‘binary_sensor.regn’,‘gradient’) != None and state_attr(‘binary_sensor.regn’,‘gradient’) > 0 and state_attr(‘binary_sensor.regn’,‘gradient’) < 0.0278 %}
{{ (state_attr(‘binary_sensor.regn’,‘gradient’)*3600) | round(1) }}
{% else %}
0
{% endif %}

Hello… is this code in a template or in a sensor?

rain_today:
friendly_name: Regn i dag
unit_of_measurement: mm
value_template: >-
{% set count = states(‘sensor.regnsensor_vippe’) | int %}
{% if states.sensor.date_time_iso.last_changed - states.sensor.regn_i_dag.last_changed > timedelta(minutes=60) %}
{% set mm_per_pulse = 0.303 %}
{% else %}
{% set mm_per_pulse = 0.30303 %}
{% endif %}
{% set mm = count * mm_per_pulse %}
{% if states(‘sensor.regnsensor_vippe’) != ‘unknown’ %}
{{ mm }}
{% else %}
{{ states(‘sensor.regn_i_dag’) }}
{% endif %}
regn_pr_hour:
friendly_name: “Regn pr time”
unit_of_measurement: ‘mm/h’
value_template: >-
{% if state_attr(‘binary_sensor.regn’,‘gradient’) != None and state_attr(‘binary_sensor.regn’,‘gradient’) > 0 and state_attr(‘binary_sensor.regn’,‘gradient’) < 0.0278 %}
{{ (state_attr(‘binary_sensor.regn’,‘gradient’)*3600) | round(1) }}
{% else %}
0
{% endif %}

Hello… is this code in a template or in a sensor?

It is old code. Here is a newer one

template:
  - sensor:
    - unique_id: rain_last_24h
      name: "Regn siste 24t"
      unit_of_measurement: mm
      state: >-
        {% set count = states('sensor.regnsensor_vippe_24t') | int(0) %}
        {% set mm_per_pulse = 0.30303 %}
        {% set mm = count * mm_per_pulse %}
        {{ mm }}
    - unique_id: rain_last_hour
      name: "Regn siste time"
      unit_of_measurement: mm
      state: >-
        {% set count = states('sensor.regnsensor_vippe_1t') | int(0) %}
        {% set mm_per_pulse = 0.30303 %}
        {% set mm = count * mm_per_pulse %}
        {{ mm }}
    - unique_id: rain_since_midnight
      name: "Regn siden midnatt"
      unit_of_measurement: mm
      state: >-
        {% set count = states('sensor.regnsensor_vippe_i_dag') | int(0) %}
        {% set mm_per_pulse = 0.30303 %}
        {% set mm = count * mm_per_pulse %}
        {{ mm }}
    - name: "Regn intensitet"
      unit_of_measurement: 'mm/h'
      state_class: measurement
      unique_id: rainfall_per_hour
      state: >-
        {% set rain_hour = ((state_attr('binary_sensor.rainfall_trend', 'gradient') | float(0)) * 3600) | round(1, 'floor') %}
        {% if rain_hour >= 0 and states('sensor.regn_siste_time')|float(0) > 0.0 %}
          {{ rain_hour }}
        {% else %}
          {{ 0 }}
        {% endif %}

sensor:
  - platform: history_stats
    name: Regnsensor vippe 24t
    entity_id: binary_sensor.regnsensor
    state: 'off'
    type: count
    duration:
      hours: 24
    end: '{{ now() }}'

  - platform: history_stats
    name: Regnsensor vippe 1t
    entity_id: binary_sensor.regnsensor
    state: 'off'
    type: count
    duration:
      hours: 1
    end: '{{ now() }}'

  - platform: history_stats
    name: Regnsensor vippe i dag
    entity_id: binary_sensor.regnsensor
    state: 'off'
    type: count
    start: '{{ now().replace(hour=0, minute=0, second=0, microsecond=0) }}'
    end: '{{ now().replace(hour=23, minute=59, second=59) }}'

binary_sensor:
    - platform: trend
      sensors:
        rainfall_trend:
          entity_id: sensor.regn_siden_midnatt
          max_samples: 2
2 Likes

Hi I see that there is a little water in it, is this the intention? or should the water be measured and drained immediately, so to speak?

There is no measured rain yet today…when I look up the rain intensitivy sensor

Are the other sensors still the same? Like very much in top of this topic (post #37 and #42)?