Count based on binary_sensor into a template sensor?

Allright, I hope you guys can help me with this. I have set up a binary_sensor using a GPIO pin:

  - platform: rpi_gpio
    ports:
      5: Water meter

Now what this does is, everytime 1 liter of water is used it goes On and then Off again. Currently what I did is create a counter and have an automation increase the counter by 1 everytime the sensor is triggered from Off to On.

I bet there’s a much better way because I would like this as a numeric sensor. So that I can plot graphs of the water usage and such. I have looked into the template sensor but I feel like I am missing something very simple.

Can any of you guys help me out?

This is what I can get now:

But that’s not what I want as you can imagine!

history statistics with count. But you’ll have to specify a duration for the counting and you need to store history for the item in question.

Allright, fair enough! That looks good, what I have is the following:

  - platform: history_stats
    name: Water verbruik
    entity_id: binary_sensor.water_meter
    type: count
    duration: 00:01:00
    start: '{{ 0 }}'
    state: 'on'

But I’d like it to count in periods of 1 minute. I don’t think this syntax is totally correct. Still looking into it, any pointers?

Hmmm it says that it can track “how long a sensor had a specific state in a period of time”. I want to count how often the state ‘on’ was achieved. So sensor type ‘count’ would seem good. I’m just not sure as to how I can make it show in blocks of 1 minute.

Weird, even when I do this:

  - platform: history_stats
    name: Water verbruik
    entity_id: binary_sensor.water_meter
    type: count
    start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    end: '{{ now() }}'
    state: 'on'

I still get a flatline…whilst I definantly see results in the binary_sensor’s history. Many on/off states!

[edit]
I now see the sensor line rising…but the value just gets bigger and bigger. I’d like it to say how many liters per minute…

So not:

I’m starting to think this isn’t possible…

I want it like this: (sorry I can’t post 2 images in 1 post)

if you just want the count in the last minute, change the duration to only look at the last minute and specify the end. The documentation mentions that you need to specify exactly 2 of the following paramters: start, end, duration. Not all 3.


When you configured it this way, you’re saying ‘start at the begining of time’. Begining of time in computer speak is jan 1st 1970. So in this configuration you essentially said “Look at the the minute between 00:00:00 and 00:01:00 on January 1st 1970”.

start: '{{ 0 }}'
duration: 00:01:00

When you configured it this way, you’re saying ‘start at the begining of the day’, because 0 hour, 0 minute, and 0 second is midnight.

start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
end: '{{ now() }}'

So the proper configuration to solve your problem would be: (this will tabulate data based on your startup, i.e if you started up at second 14, this will tabulate every time the time hit second 14).

  - platform: history_stats
    name: Water verbruik
    entity_id: binary_sensor.water_meter
    type: count
    end: '{{ now() }}'
    duration: "00:01:00"
    state: 'on'

or (this will tabulate your data every minute on the minute)

  - platform: history_stats
    name: Water verbruik
    entity_id: binary_sensor.water_meter
    type: count
    end: '{{ now().replace(minute=0) }}'
    duration: "00:01:00"
    state: 'on'
2 Likes