How to create software counter and its display in Lovelace

I am quite new with HA. I would like your help to get a counter implemented in my Lovelace dashboard that shows the number interrupts of an infrared beam in my garden during the night and that can be reset manually through the dashboard.

Current situation: There are two infrared beams installed both at the front and the back of our house. If the beam at the front is interrupted during the night, an HA automation turns on flashing red LED strips for 5 minutes. If the beam at the back is interrupted during the night, an HA automation turns on lights inside the house for 5 minutes.

The HA automations run fine, they are triggered by the infrared switch (trigger: switch.ac_1a3cd52_12 turned on) at the front and a similar trigger for the back. However I do not know how to implement the counters I described and their display in Lovelace. I would much appreciate if someone could provide me with a hint/solution. I am not yet very familiar with templates. Thanks in advance.

You could use this, when you set the period correctly it ‘reset’ itself.
History Stats - Home Assistant (home-assistant.io)

Much appreciating you pointing this out to me. I will check-it out!

Here’s a simple example of a counter. It does not do the reset, but that could be added.


template:
  - trigger:
      - platform: state
        entity_id: binary_sensor.cd_sump_running
        from: "off"
        to: "on"
    sensor:
      - name: "cd_sump_cycle_count"
        state: "{{ states('sensor.cd_sump_cycle_count')|int(0) + 1 }}"
        unit_of_measurement: cycles

Thank you very much. Finally got time to implement. It works like a charm!!
Now looking at a way to manually reset the counter.

Good, please mark the post for solution so others can benefit of that too :slight_smile:

I think I marked it now as “Solution”.

I think we could add an second trigger and use that in a jinja expression to return 0 or the current calc depending on what trigger triggered. For that second trigger could use an input boolean, an event (fired from a button press) a time of day. Let me know if you figure it out

Maybe use a “Button” to call the Service to clear the Counter on “Action”
" Action call service"

- service: counter.reset
 entity_id: counter.my_custom_counter

Thou ofcause then the “created” Sensor above should be domain “counter”

EDIT: Meaning that above “Solution” is actually Not the right Solution to your Question " How to create software counter "

Below is “Counter-Increment”

And reset of same is done with above call-service: counter.reset

So first you create your counter, and restart ha.
Then you create an automation

trigger:
  - platform: state
    entity_id:
      - whatever.my_entity
    from: "off"
    to: "on"
action:
  - service: counter.increment
    target:
      entity_id: counter.my_sum_count

Then , if you want a button to reset the counter, you create a button with a action/call-service function

Thank you all: @boheme61, @PeteRage, @vingerha. You provided me the lead and all the solution components that allowed me to create the solution:

I created 2 Helpers of type Counter and one of type Button.

I added an action to my 2 existing automations (that switched-on LEDs and lights). These automations are triggered by a physical interrupt of the IR beam at the front and at the back of our house. The actions in the automation now also call the service “Counter: Increment” which increments the Counter I created earlier through the Helper. On an entity card in Lovelace I monitor the counter.

In a new Automation I used the Button as State change trigger for performing the service: Counter: Reset for both counters. In the entity card in Lovelace I added the Button for manual reset via the UI of both counters in one go.

1 Like