Counting the number of days that a device has been used


I would like to have a sensor that stores the number of days that a device is being used regardless the number of times that is switched in the same day. In other words, whenever the device is used it should increment the total days by 1. I was able to create an automation using the counter.increment service but the challenge is to avoid that the automation run just only once a day if for some reason the device was switched on several times in the same day. Any contribution is more than welcome.

I would create a helper datetime.
When the device is used the datetime is set to now() then increment counter.
As condition you use this datetime in a template condition.

{{ states('input_datetime.thisdatetime')[0:10] != states('') }}

but this requires the Time & Date - Home Assistant (

You could also use .... != now().timestamp() | timestamp_custom("%Y-%m-%d") }} if you don’t want to add the date sensor.


If you only want it to run ONCE a day max… Add a condition that the automation can only run that one time.

  - "{{ state_attr(this.entity_id, 'last_triggered').date() < now().date() }}"
1 Like

This looks very straight! I’ll try it over the weekend.

I think last triggered is cleared when you reboot HA.
So if you use the device, reboot HA then use the device again that will give you two counts, if I’m correct that the last triggered is cleared.
Datetimes is persistent over a restart on the other hand.

last_triggered isn’t cleared on a restart. It is persistent till you delete the automation.

Give it a try and pop this in your dev tools with an automation that had been triggered before your restart:

{{ state_attr('automation.androidtv_scans', 'last_triggered').date() }}

I misread it. I thought it was about the device last changed.
I guess I need to read more carefully :slight_smile:

It definitely works! The only thing is that you need to run the automation the first time manually otherwise the condition fail since there is no last triggered date.

That’s very true. Since that value is stored though, It just needs that initial kick.

If you’re worried about that though, you can wrap it in a check…

{% if state_attr(this.entity_id, 'last_triggered') != None %}
  {{ state_attr(this.entity_id, 'last_triggered').date() < now().date() }}
{% else %}
{% endif %}


{{ state_attr(this.entity_id, 'last_triggered') == None or state_attr(this.entity_id, 'last_triggered').date() < now().date() }}

If it doesn’t exist, let it run.

1 Like

If you’re interested there’s another way to do it. The question of running an automation just once a day was posted last February. It also had a second requirement where the time threshold wasn’t simply at 00:00 but at 05:00.

For this application, which only needs to ensure the automation executes once a day (time threshold is 00:00), the template is simpler:

{{ state_attr(this.entity_id, 'last_triggered') | default(today_at(), true) <= today_at() }}

If last_triggered doesn’t exist, the default filter provides today_at() which is the current date at 00:00.


Thank you @123 . For some reason I didn’t found that topic from February, anyway it is always good to have alternative solution. I have being reading so many HA topics in the last months and allow me to share that you are one of the greatest contributors of this fantastic community.

Thank you @calisro . Should the be ‘None’ or None (without single quotes)?

Without. It’s not a string. When you put quotes it’s a string.

1 Like