Track consumption with button press

Hello everyone,

I would like to track the consumption of bottled water manually. The idea is to put a button next to the water storage and you press that button when you take a fresh bottle from the storage.

In the end I would like to have pretty statistics like on the energy dashboard: bottles per day, summed up over the weeks, monthly overview, averages, etc.

I am fine with the hardware: there is a Zigbee button firing events. But how does it go in from there? I could define a counter helper and an automation increasing it on each press.

Maybe I am missing something obvious here.

How would you set it up? And how do I get the visualization?

Thanks for your help.

for visualization, perhaps take a look at this:

for collecting the data that feeds that, look at this:

you’ll need to determine what stats you want to track and display then create the appropriate sensors wrapping the button.

You can use my new component:

It like a counter but with stats support.

In the video (in spanish but with subtitles) I show a example about how count exercise session that you can easily adapt for your case.

1 Like

Thanks for your answer. I also saw that but I thought that the statistics card is only usable on sensors.
I do not have a sensor from my button presses, only a helper. Or am I missing something?

Create a trigger based template sensor to store the number of bottles used. You can set the trigger to be the event that your Zigbee button fires.

Be sure to define a state_class if you want long term statistics created.

Great. That was the missing link. The timestamp of each change to the template sensor is also saved somewhere?

That also looks interesting. Then I would not need a helper counter and a triggered template sensor but just your special sensor instead.
An automation to run when the button is pressed and it updates the sensor. Sounds doable, I will give it a try.
Thank you.

I still need a helper counter in the first place to keep track of the bottles and a triggered template sensor to convert it into a sensor, right?

No, all you need is a trigger-based template sensor. Nothing else. The state of that sensor would be the count of bottles of water. Something like this:

template:
  - trigger:
      - platform: event
        event_type: my_zigbee_event
        event_data:
          some_key: something_like_button_press
    sensor:
      - name: Water Bottle Count
        state_class: total_increasing
        state: >
          {{ this.state | int(0) + 1 }}

If you want monthly/yearly/etc. counts, you can create utility meter helpers (either in YAML or through the UI) and have them reset at whatever interval you want.

Every time the sensor is triggered, its last_changed and last_updated properties will update (just like any other entity in HA).

You can access those properties in a template like this:

{{ states['sensor.water_bottle_count']['last_changed'] }}
or
{{ states.sensor.water_bottle_count.last_changed }}

Great it works as desired! Thanks alot!

Is there a possibility to reset the sensor?

The easy way, without code changes and only from the UI, would be to go to developer tools → states, then filter for your entity and click it, and you can set it to 0 (or to any value you desire).

Another option is to add a custom event as another trigger, and change your template to handle that other trigger differently. Then you can fire that custom event along with event data that has the value you want to set the entity to. If you want to reset this sensor from a script or automation, this would be the way to do it. But also keep in mind if you have utility meter entities based off this sensor, those can already be set to any value with a service call.