How to use Date (input_datetime) in an Automation Condition?

I’m trying to create a reminder automation that only executes once per day. The easiest way I see to do that would be to have an input_datetime for date only and if it is not equal to today, then execute the reminder and set the input_datetime to today’s date.

However, I can’t seem to understand how the “time” condition works (and if it even works for just a date).

With input_datetime.pc_reminder set to yesterday’s date (using the UI “Test” option for the condition):

  • If I set the “time” condition to “after” pc_reminder with the “before” set to midnight (in the visual UI, it has to be set to something), the condition fails.
  • If I set the “time” condition to “after” pc_reminder and using YAML, remove the “before” completely, the condition also fails.
  • If I set both before and after conditions to pc_reminder, it passes.

However, with input_datetime.pc_reminder set to today’s date, I get the exact same results (first two fail and the last one passes).

I’m trying to have it so that, if the pc_reminder date is before today, the condition will pass and I can then set the pc_reminder date to today’s date (so I won’t be reminded again today). But then the condition should fail if pc_reminder is the same as today’s date.

I’ve seen an alternative method for “once a day” condition that uses a boolean, but then you have to reset the boolean via another automation. I’m hoping to get this working so I don’t need the extra boolean.

What am I doing wrong?

Thanks in advance,
Paul

alias: Remind Me When I am at my Computer
description: ''
trigger:
  - platform: state
    entity_id:
      - switch.pauls_pc
    to: 'on'
    for:
      hours: 0
      minutes: 1
      seconds: 0
condition:
  - condition: time
    after: input_datetime.pc_reminder
action:
  - service: input_datetime.set_datetime
    target:
      entity_id: input_datetime.pc_reminder
    data:
      date: '{{ states(''sensor.date'') }}'
  - service: notify.mobile_app_pixel_4a
    data:
      message: Check your Computer To-Do List
      title: Computer To-Do List Reminder
mode: single

You don’t need a helper.

  - condition: template
    value_template: >
      {{ now() - state_attr('automation.remind_me_when_i_am_at_my_computer', 'last_triggered') > timedelta(days=1) }}

When using that value_template, I get an error when I test it (“template value should be a string for dictionary value @ data[‘value_template’]. Got None”). I tried wrapping it in quotes as

condition: template
value_template: >-
  "{{ now() - state_attr('automation.remind_me_when_i_am_at_my_computer', 'last_triggered') > timedelta(days=1) }}"

But I get the same error message.

Thoughts?

Similar Topic was discussed here:

Has the automation ever triggered?

Try this:

- condition: template
    value_template: >
      {% if state_attr('automation.remind_me_when_i_am_at_my_computer', 'last_triggered') %}
        {{ now() - state_attr('automation.remind_me_when_i_am_at_my_computer', 'last_triggered') > timedelta(days=1) }}
      {% else %}
        false
      {% endif %}

Don’t use quotes here if you use multiline YAML markers (the >). Only use the quotes around the template when it’s on the same line as value_template.

I’m assuming @tom_l solution is correct and will let you know if it doesn’t work (Tom, I originally never triggered the automation as I was using the Test button and having issues, as noted). I did run it manually and tested your logic (the more extensive version) in the Developer Tools → Template area and can confirm it provides the correct true/false.

However, that solution still gives the same error when using the “Test” button in the automation UI. So, as @Osorkon noted, apparently that is a bug with value_templates and the Test UI, which is still an issue in 2022.7.6, which is what I’m currently on.

https://github.com/home-assistant/frontend/issues/12282

Thanks for the help all!

Okay, now that it has been a day, the automation doesn’t run since it appears @tom_l solution actually requires a delay of 24 hours = 1 day, not just a new day.

{{ now() - state_attr('automation.remind_me_when_i_am_at_my_computer', 'last_triggered') > timedelta(days=1) }}

So I changed it to:

{{ now().strftime('%Y-%m-%d') != state_attr('automation.remind_me_when_i_am_at_my_computer', 'last_triggered').strftime('%Y-%m-%d') }}

Is there a better way than the string comparison to check if it is a new/different date?

Thanks,
Paul

{{ now().date() > state_attr('automation.remind_me_when_i_am_at_my_computer', 'last_triggered').date() }}