Only allow automation to trigger once a day?

To ensure an automation only executes once for any given day, simply use the following Template Condition.

condition:
  - "{{ state_attr(this.entity_id, 'last_triggered').date() < now().date() }}"
5 Likes

Good to know, thanks! I’ll implement this from now on :slight_smile:

Great, thanks for that. Do you have a solution if I want to retrieve the same automation once a day for two time slots?
To example:

  1. time slot 05:00 - 06:30 for me
  2. time slot 06:30 - 07:30 for my wife

I’m using the following template as a condition to see if the automation already has run that day. It basically converts the last triggered attribute to a y/m/d string and compares it to today’s date converted to a y/m/d string. This removes the remaining details from the date attribute. If you are not in UTC you also have to convert the last_triggered attribute to a local time. If the below condition is equel it means it already run that day.

{{ (state_attr('automation.morning_music_livingroom', 'last_triggered')|as_local).strftime('%Y-%m-%d')
  == now().date().strftime('%Y-%m-%d') }}

If you’re interested, you can reduce the template to this:

{{ (state_attr('automation.morning_music_livingroom', 'last_triggered')).astimezone() .date()
  == now().date() }}

Or just this:

{{ this.attributes.last_triggered.astimezone().date() == now().date() }}

EDIT

Correction. Adjust datetime for local timezone.

2 Likes

Hmm, that did not work for me.
Automation states for last_triggered date attribute have a long quotation e.g.“2024-02-13 10:49:51.748362+11:00”. I need to compare against only the date (yyyy-mm-dd) and not the rest, as I need to know if it already run today in my timezone and not UTC.

A datetime object’s date() method returns a date object (contains year, month, and day). What I overlooked to do was what your template did and that’s to initially convert UTC to local time.

I corrected the examples I had posted above.

I tested the first template using an automation that triggered yesterday and it correctly reported false today.

1 Like

I know it’s been a while, I was able to build a blueprint for this specific purpose:

Or you can try this excellent custom component: