Automation only once a day - superior method?

Hello all,

There are several uncomplicated ways to check whether an automation has already been executed on the same day. I have played around with a few of them and wonder if one is preferable to the others.

Maybe this is just a purely academic question, so forgive my curiosity.

click

{%- set test = state_attr('automation.test', 'last_triggered') %}

now().day {{ now().day }}

test.day {{ test.day }}

now().day == test.day {{ now().day == test.day }}

now().day != test.day {{ now().day != test.day }}

now().day > test.day {{ now().day > test.day }}

----

today_at() {{ today_at() }}

test {{ test }}

today_at() == test {{ today_at() == test }}

today_at() != test {{ today_at() != test }}

today_at() > test {{ today_at() > test }}

----

now().date() {{ now().date() }}

test.date() {{ test.date() }}

now().date() != test.date() {{ now().date() != test.date() }}

now().date() == test.date() {{ now().date() == test.date() }}

now().date() > test.date() {{ now().date() > test.date()}}

----

now().day {{ now().day }}

test.day {{ test.day }}

now().day == test.day {{ now().day == test.day }}

now().day != test.day {{ now().day != test.day }}

now().day > test.day {{ now().day > test.day }}


Check if today’s date is greater than the automation’s last_triggered date.

  • If it is then it means the automation hasn’t triggered today.
  • If it isn’t then it means the automation has triggered today.

Expressed as a Template Condition:

condition:
  - '{{ now().date() > this.attributes.last_triggered.date() }}'

Thanks for coming in. I know the output and my favorite is a simple comparison of date (as you did in your example). However, I’m wondering if the different approaches have different effects that I do not recognise due to lack of experience.

Your topic’s title asks for the “superior method”. What is your criteria for “superior”? If it’s speed, I doubt you’ll detect any significant difference because they all do the same thing, namely perform one arithmetic comparison of two values. Any differences are likely to be so small that they have no practical impact on the automation’s overall execution time.

If it’s not speed, then you’ll need to define your criteria for ‘superior’.


NOTE

If you put now() statements before and after each time comparison, you can can get an estimate of how much time each comparison takes to perform. It’s likely to be in the microseconds range.

My point was about advantages/disadvantages. But I believe that I had an error in my logic: I no longer considered the terms listed in my initial post as independent functions. And with such a simple comparison, the question is indeed probably of a purely philosophical nature. :wink: