I assume you are adding up the on and off state flips to get a total and then multiplying that by the tip volume to get the amount of rain?
If so here’s an alternative to using history stats:
(configuration.yaml)
template:
- trigger:
- id: count
platform: state
entity_id: binary_sensor.aqara_dw_2_open_closed
to:
- 'on'
- 'off'
from:
- 'on'
- 'off'
- id: reset
platform: time
at: '0:00'
sensor:
- name: 'Rain per day'
unit_of_meaurement: 'mm' # or cm
state_class: measurement # only include this if you want long term statistics
device_class: precipitation
state: >
{% set mm_per_flip = 0.2 %}
{% if trigger.id == 'reset' %}
0
{% else %}
{{ this.state | float(0) + mm_per_flip }}
{% endif %}
Change the mm / flip value to your bucket tip volume.