Count for number of times Automations triggered

Id like to know how many times my various automations get used. I think it would be useful to have a counter attribute by default for all automations (and scripts for that matter) that we can see how many times they are run, as well as the last-triggered which we currently have.

Further to the above, would anyone know a way of achieving this, short of having to set up a counter for each automation?

Thanks

So this counter attribute would record the number of times the automation was triggered from the moment it was created?

Can you provide two examples of a practical application of this information?

The value has to be stored somewhere that isn’t lost when Home Assistant restarts (such as a counter or input_text). It can even be an automation that publishes a value (as a retained message) to an MQTT topic displayed by an MQTT Sensor.

Yes, that is what I would like.

To be honest, no, I would struggle to provide examples of how to use this data. Other than the fact that it is interesting for the end user. I would find this information informative and it would allow me to look to imporve efficiency on my most used automations.

Yes, i could store a counter in an input_text, but with a large number of automations this would be very tiresome creating a counter for each corresponding automation and then having to edit each automation to add a step of increasing its counter by one. I was hoping for something a bit more autonomous than that if anyone has ideas?

Well, without having a practical application for the data makes it far less likely to attract a developer’s attention.

The Logbook already shows you when an automation has executed so you can already see how often it occurs. Frankly, the frequency of execution is tied directly to the triggers you have assigned to the automation; it’s a predictable metric.

For anyone that would like this functionality without the hassle of setting up counters for each automation and triggering an increment in each automation, I have created the following automation.

It will automatically create a MQTT sensor for each automation the first time an automation is run. It will then increment the count each subsequent time the automation is run but there is no need to create any inputs or edit any existing automations.

alias: Automation - Counter
description: ''
trigger:
  - platform: event
    event_type: automation_triggered
condition: []
action:
  - service: mqtt.publish
    data:
      topic: >-
        homeassistant/sensor/counter_{{ trigger.event.data.entity_id |
replace('.', '_') }}/config
      payload_template: >-
        {"name": "counter_{{ trigger.event.data.entity_id | replace('.','_')
        }}",
         "unique_id": "counter_{{ trigger.event.data.entity_id | replace('.','_') }}",
        "state_topic": "homeassistant/sensor/counter_{{
        trigger.event.data.entity_id | replace('.','_') }}/state"}
      retain: true
  - service: mqtt.publish
    data:
      topic: >-
        homeassistant/sensor/counter_{{ trigger.event.data.entity_id |
        replace('.', '_') }}/state
      payload_template: >-
        {% set trigger_address = "sensor.counter_" +
        trigger.event.data.entity_id | replace('.','_') %} {{
        states(trigger_address) | int + 1 }}
      retain: true
mode: single

1 Like

Well done! That effectively achieves the goal with virtually no ongoing configuration or maintenance.

BTW, if you use variables it can help to simplify the automation:

alias: Automation Counter
trigger:
  - platform: event
    event_type: automation_triggered
action:
  - variables:
      entity: "counter_{{ trigger.event.data.entity_id.replace('.', '_') }}"
  - service: mqtt.publish
    data:
      topic: homeassistant/sensor/{{entity}}/config
      payload_template: '{"name":"{{entity}}","unique_id":"{{ entity }}","state_topic":"sensor/{{entity}}"}'
      retain: true
  - service: mqtt.publish
    data:
      topic: sensor/{{entity}}
      payload_template: "{{ states('sensor.' ~ entity) | int + 1 }}"
      retain: true

NOTE

In this version of the automation, the state_topic is different from yours. I prefer not to store state_topic's data within an MQTT Discovery topic.

1 Like

Thank you for that tip

I have a practical use for your code (thank you btw). I have an automation that lowers my blinds if the room gets too hot. The blinds have 2 intermediate positions - so I want to run the automation a max of 2 times in a day. I’m experimenting with your code now … That all works :+1: Any idea how I’d reset all / selected counters at the end of the day?

Practical use for counting automation events: warning of switch, pumps, power etc failures and providing information to extend component lifetime.

My subfloor fan power supply just died after 12 months use (just at the warranty end) and now I want to know how often it’s power was switched on and off and would happily reduce the number of times it’s switched if that might extend its life.

1 Like

Quite new to this but already surprised that an automation run counter does not exist as basic functionality. I can think of multiple use cases for this, specifically cases of measuring and catching anomalies and use patterns of devices, humans or pets.

In my case I want to know (and graph) how often the cats have visited the litter boxes, using motion sensors and a number of terms. My automation also starts the air purifier at full speed for a couple of minutes after every visit.

I’ll try the suggested workaround for now, thanks! :blush:

1 Like

You need to change the “int” statement to “int(0)” to initialize the state when first run.

Is it possible to apply the automation alias as the friendly name for the sensors created so “counter_automation_trash_pickup” friendly name becomes “Trash Pickup”?