History_stats or something else?

hi all

i have a custom built rain gauge sensor that is using a contact sensor to measure the rain. every time the contact is turn on or off, i am measuring the rain. i have a problem with the start of a new day

  - platform: history_stats
    name: "Rainsensor flips on"
    entity_id: binary_sensor.l1_raingauge_contact
    state: "on"
    type: count
    start: "{{ now().replace(hour=0, minute=0, second=0)}}"
    end: "{{ now() }}"
  - platform: history_stats
    name: "Rainsensor flips off"
    entity_id: binary_sensor.l1_raingauge_contact
    state: "off"
    type: count
    start: "{{ now().replace(hour=0, minute=0, second=0)}}"
    end: "{{ now() }}"

currently i use 2 sensors to count the on and off contact. and as you can see, i try to “reset” the count at the start of a new day to measure rainfall for a new day. however, i start the day with the count of 1 on either one of the sensor, given that it is currently in that state at 00:00

how do i make it such that both sensors start at 0 at 00:00 ?

do i need to use a different type of sensor?

thank you in advance

Forget the history stats sensor and use a counter and automation.

This will increment the counter counter.rain on each transition to off or on and will reset the count at midnight.

trigger:
  - id: count
    platform: state:
    entity_id: binary_sensor.l1_raingauge_contact
    not_to:
      - unknown
      - unavailable
    not_from:
      - unknown
      - unavailable
  - id: reset
    platform: time
    at: "00:00:00"
action:
  - if:
      - condition: trigger
        id: reset
    then:
      - service: counter.reset
        target:
          entity_id: counter.rain
    else:
      - service: counter.increment
        target:
          entity_id: counter.rain

thank you for the quick reply. am i still able to track history then?

Like every other entity counters have state history. This is kept for 10 days by default. They don’t have a state class though so you won’t get long term statistics.

Probably the best way to fix that would be to use a template sensor. You can account for the actual amount of each tip of the bucket then too. e.g. if each tip = 0.2mm of rain:

configuration.yaml

template:
  - sensor:
      - name: Daily Rainfall
        state_class: measurement
        device_class: precipitation
        unit_of_measurement: mm
        state: "{{ states('counter.rain')|float * 0.2 }}"