Really dumb question now() == today_at("whatever:time") is unpredictable?

Hello, I was modifying one of my automation which is sending the notification about to turn on vacuum based on states of two entities and time of a day.

First part is checking if today is more than 3 days (written by start vacuum automation into the helper) the vacuum was running OR if the reminder is on AND if it is specific time of a day.

template is here:
{{ (now().strftime('%Y-%m-%d') > states('input_datetime.stetka_uklid') or states('input_boolean.stetka_reminder') == "on") and now() == today_at("16:15") }}

That part with OR works fine but the other part seems always false → now() == today_at("16:15"). It is really confusing me. I know now() itself is super detailed with all those milliseconds and whatever but there has to be a match at some point with today_at("whatever:time:chosen")

I spent some time trying to find “the truth” in developer tools but obviously I would need more luck to match and get a True :slight_smile:. Simple >= instead of == works fine and solved my problem, maybe even better if for some reason HA is down in that specific time. But I would be happier if I knew why there is no match when == is used. Anybody has a clue?

Templates are only evaluated periodically, typically every minute plus when referenced entities change (and now() is not an entity).

That makes it unlikely that the now() datetime object is ever the same as the today_at() datetime object when checked.

Using sensor.time (ref) instead:

{{ is_state('sensor.time', '16:15') }}

Barring system issues, I think you’re guaranteed that this will fire as soon as 16:15 rolls around and the entity updates.

1 Like

I’d retailor your automation to use a time trigger.

trigger:
- id: datetime
  platform: time
  at: input_datetime.stetka_uklid
- id: at
  platform: time
  at: "06:15:00"
condition:
- condition: template
  value_template: "{{ trigger.id == 'at' and is_state('input_boolean.stetka_reminder', 'on') or trigger.id == 'datetime' }}"
action:
...

Much more predictable.

if you want to condense everything more…

trigger:
- platform: time
  at:
  - input_datetime.stetka_uklid
  - "06:15:00"
condition:
- condition: template
  value_template: "{% set v = trigger.now().strftime('%H%M') == '06:15' %}{{ v and is_state('input_boolean.stetka_reminder', 'on') or not v }}"
action:
...

Interesting, never thought time integration would be good for anything. I would probably go for @Troon suggestion as my input_datetime is date only and I am lazy to redo it :slight_smile:.

But @petro thank you for your part, there is an information for me with set which I thought it cannot be used in template :slight_smile:.