How to count the times an automation was triggered, restart persistent

if Id like to do that on this trigger:

  - trigger:

      - platform: state
        entity_id: counter.reload_tradfri_integration
        to: 0

how then can we reset the state if the existing trigger template sensor? Only trying to reset the state, the list of attributes can remain untouched, that is, it could log the time of the reset, but need whipe the complete history

would this be allowed:

  - trigger:

      - platform: state
        entity_id: automation.reload_tradfri_integration #counter.
        attribute: last_triggered
        id: count

      - platform: state
        entity_id: counter.reload_tradfri_integration
        to: 0
        id: reset


    sensor:

      - unique_id: count_reload_tradfri_integration_single_attribute
        name: Count reload Tradfri integration single attribute
        icon: mdi:eye-plus-outline
        state: >
          {% if trigger.id == 'count' %}
          {{this.state|default(0)|int(0)+ 1}}
          {% else %} 0
          {% endif %}

That would work, but you still need a counter entity to reload. Why not just make a button that resets it instead. Click the button ā†’ reset.

I am in for anything easyā€¦ so if a button would work, that would be great.

fwiw, I just tried that trigger in the counter (I have that available, so I figured to align all of these counter entities ) but it rendered both of my trigger template sensors unavailable ā€¦

btw config checker errored on the to: 0, and made me use to: '0' which surprised me, as a counter would indicate a number and not a stringā€¦?

That should work.

FWIW, I typically use an event. For example, something like this:

  - trigger:
      - platform: state
        entity_id: automation.reload_tradfri_integration #counter.
        attribute: last_triggered
      - platform: event
        event_type: reset_tradfri
    sensor:
      - unique_id: count_reload_tradfri_integration_single_attribute
        name: Count reload Tradfri integration single attribute
        icon: mdi:eye-plus-outline
        state: "{{ this.state|default(0)|int(0) + 1 if trigger.platform == 'state' else 0 }}"
        attributes:
          ... etc ...

Donā€™t forget to modify the attributeā€™s template as well.

if I manually reset a counter, would that emit an ā€˜eventā€™? that would be easiest.:wink:

for the sake of it, I made a input_button.reset_tradfri_reload_counter
lets see if this emote an event

  - trigger:

      - platform: state
        entity_id: automation.reload_tradfri_integration #counter.
        attribute: last_triggered
        id: count

      - platform: state
        entity_id: input_button.reset_tradfri_reload_counter
        id: reset

    sensor:

      - unique_id: count_reload_tradfri_integration_single_attribute
        name: Count reload Tradfri integration single attribute
        icon: mdi:eye-plus-outline
        state: >
          {% if trigger.id == 'count' %}
          {{this.state|default(0)|int(0)+ 1}}
          {% else %} 0
          {% endif %}

itā€™s that easy with a button

1 Like

When you reset a counter (such as by clicking its Reset button in the UI) it generates a call_service event.

The eventā€™s details look like this

event_type: call_service
data:
  domain: counter
  service: reset
  service_data:
    entity_id: counter.example

So you can use an Event Trigger to listen for a call_service event containing all of those details.

1 Like

got it!

event_type: call_service
data:
  domain: input_button
  service: press
  service_data:
    entity_id: input_button.reset_tradfri_reload_counter
origin: LOCAL
time_fired: "2023-03-17T15:06:03.874729+00:00"
context:
  id: 01GVR1PGQ2S8DSGAZZJMND8JZC
  parent_id: null
  user_id: 42fyueyubhfhehvwnfnnwvw78

so I can choose now, use an event, or the state change in Petroā€™s suggestion.

to align the counter, Ive added

  - alias: Reset Tradfri reload Counter on button press
    id: reset_tradfri_reload_counter_on_button_press
    mode: restart
    trigger:
      platform: state
      entity_id: input_button.reset_tradfri_reload_counter
    action:
      service: counter.reset
      target:
        entity_id: counter.reload_tradfri_integration

thanks!

edit

this is not accepted as trigger though

      - platform: event
        event_type: call_service
        data:
          domain: counter
          service: reset
          service_data:
            entity_id: counter.reload_tradfri_integration

data is invalid for template.
grrr it needs to be

      - platform: event
        event_type: call_service
        event_data:
          domain: counter
          service: reset
          service_data:
            entity_id: counter.reload_tradfri_integration

those details can be really nasty

You may have misunderstood my suggestion. I didnā€™t suggest to use an Event Trigger to detect an Input Buttonā€™s state-changes (or a counter reset by an Input Button). A State Trigger would be simpler for that purpose (see petroā€™s example above).

I suggested to use an Event Trigger to detect when a counter is reset via its button in the UI.

no, I got that,
sorry for the miscommunication here, it was indeed getting a bit mixed-up.

I have the 2 options working now:

reset via the counter, and the emitted event trigger
reset via an input_button, and itā€™s state change
even a third, but using an event on the input_button press.

Or as a distant 4th, state change on the counter to 0ā€¦ but since that failed in my first experiment, that is a very distant 4th

now up for the next challenge and rewrite those attributesā€¦

        attributes:
          herlaad_overzicht: >
            {% set triggered = trigger.to_state.attributes.last_triggered %}
            {% set op = as_local(triggered).strftime('%D %X') %}
            {% set current = this.attributes.get('herlaad_overzicht', []) %}
            {% set new = [{'op': op }] %}
            {{ (new + current)[:5] }}
          last_triggered: >
            {{trigger.to_state.attributes.last_triggered}}

have to split it up because there is no to_state in case of the button press, or counter resetā€¦
but, rather being lazy than tired, this suffices after all, as you said all along

        attributes:
          herlaad_overzicht: >
            {% set op = now().strftime('%D %X') %}
            {% set current = this.attributes.get('herlaad_overzicht', []) %}
            {% set new = [{'op': op }] %}
            {{ (new + current)[:5] }}
          last_triggered: >
            {{now()}}

small update, these have been working flawlessly the last couple of days:

template:

  - trigger:

      - platform: state
        entity_id: automation.reload_tradfri_integration #counter.
        attribute: last_triggered
        id: count

#       - platform: state
#         entity_id: input_button.reset_tradfri_reload_counter
#         id: reset

#       - platform: state
#         entity_id: counter.reload_tradfri_integration
#         to: '0'
#         id: reset

      - platform: event
        event_type: call_service
        event_data:
          domain: counter
          service: reset
          service_data:
            entity_id: counter.reload_tradfri_integration

    sensor:

      - unique_id: count_reload_tradfri_integration
        name: Count reload Tradfri integration
        icon: mdi:eye-plus-outline #counter
        state: >
          {% if trigger.id == 'count' %} {{this.state|default(0)|int(0)+ 1}}
          {% else %} 0
          {% endif %}

#           {% set previous = this.state | int(0) if this.state is defined else 0 %}
#           {{ previous + 1 }}

#           {{states('counter.reload_tradfri_integration')}}
        attributes:
          last: >
            {{now().strftime('%D %X')}}
          <<: &history
            history_1: >
              {{this.attributes.last|default('Not yet set')}}
            history_2: >
              {{this.attributes.history_1|default('Not yet set')}}
            history_3: >
              {{this.attributes.history_2|default('Not yet set')}}
            history_4: >
              {{this.attributes.history_3|default('Not yet set')}}
            history_5: >
              {{this.attributes.history_4|default('Not yet set')}}
            history_6: >
              {{this.attributes.history_5|default('Not yet set')}}
            history_7: >
              {{this.attributes.history_6|default('Not yet set')}}
            history_8: >
              {{this.attributes.history_7|default('Not yet set')}}
            history_9: >
              {{this.attributes.history_8|default('Not yet set')}}
            last_triggered: >
              {{now()}}
#               {{trigger.to_state.attributes.last_triggered}}

      - unique_id: count_reload_tradfri_integration_single_attribute
        name: Count reload Tradfri integration single attribute
        icon: mdi:eye-plus-outline
        state: >
          {% if trigger.id == 'count' %} {{this.state|default(0)|int(0)+ 1}}
          {% else %} 0
          {% endif %}
        attributes:
          herlaad_overzicht: >
            {% set op = now().strftime('%D %X') %}
            {% set current = this.attributes.get('herlaad_overzicht', []) %}
            {% set new = [{'op': op }] %}
            {{ (new + current)[:9] }}
          last_triggered: >
            {{now()}}

multiple history attributes with single reload time:

Following Taras advice using a single history attribute with all times in a list

the reload automation also holds a counter, and when that gets reset, it also resets these 2 trigger template sensors, as you can see in the listed triggers:

I would like the trigger sensor to keep track of the number of times the automation is triggered, specifically when the automation activates the action block.

yes, that is what the above does exactly