History stat with state from off to on

Need help solving a problem.

Background:
I have built a DIY tipping bucket rain gauge. I have an Aqara door/window sensor that registers the tippings. When it tips over, the sensor goes from open, passing closed and tips over to open on the other side. Going from closed to open is one tip.
I’m using History Stats sensor to count every time the state is ON (open). I have done many testings and it works perfect.
However, yesterday it saw that it had register a few tippings although it was standing on a shelf indoors. I checked the logs and saw that the sensors state had been “unknown” for some seconds and then came back to ON and that, off course, also counts as one tip.

Now to my question:
Is there a way templating History Stats so that it only counts when it goes from OFF to ON and not just register ON? That would exclude the unknown state.
I’m thinking like the State trigger in an automation where you can set both the from- and to-state.

Many thanks!

You are going to have to create a binary template sensor that filters out the unknown state and use that in your History stats sensor as there is no provision fro a from: state in the History stats integration.

Thank you for replaying.
I’ll have to look more in to this. I understand what you mean, but so far I haven’t figured out how to filter out only the valid ON. There could be several reasons for the sensor to go from any state to ON, for example a restart of HA.

Try using this binary sensor in your history stats counter:

template:
  - trigger:
      platform: state
      entity_id: binary_sensor.rain_bucket
      from: "off" # unavailable to on won't trigger this
      to: "on"
    binary_sensor:
      - name: "Rain Bucket Filtered"
        state: "{{ true }}" # turn on
        delay_off: 1 # turn off after one second. 
1 Like

I will try your solution.
However, I found a much easier way to solve my problem. The Aqara sensor of my rain bucket sits in the middle and magnet flips from open (ON) left, passing the door sensor which sets it quickly to close (OFF) and then over to open (ON) right.
I was counting every time it was set to open (left or right). What i realized yesterday was that if I instead count OFF, since the default position always is ON, it doesn’t matter if it becomes unknown and then back to ON as it’s the OFF’s that counts.
I have made two sensors to compare. One for ON and one for OFF and so far today I have two unwanted counts for ON but none for OFF :slight_smile:
It seems that that is good enough for me in this matter, but I will test your solution anyway. Might be useful next time :slight_smile:

Thanks for your help!

Hi Johlarl.
I am trying to do the same as you…
However, i am not that strong in custom sensors in HA.
Can you help we with the code and how to show the data on the dashboard?

Thanks!

Hi,
What is it that you want to achieve? Do you mean the filtering that @tom_l suggested? I tried that but never got it to work properly on my setup but I have no doubt that it works fine for others.
My solution is much simpler. Instead of counting ON, I’m now counting OFF. Since my gears default state is ON, it never add any ghost counts on, for example, a reboot of HA.

Not sure I have answered your question?

Reliable way to filter our change from or to unavailable state.
I have one Esphome device, which entities go into unavailable intermittently.