Binary sensor checking time ok, how to add offset?

You’re right, It’s not my case, and normally I try to avoid triggering something around midnight, because I don’t know what to expect. :wink:

yeah, I know what you mean

But I don’t like loose ends :face_with_symbols_over_mouth:

Are we just talking about this template?

{{states('input_select.orario_chiusura') == states('sensor.time')}}

No, the OP wants two boolean sensors to be used in automations (hence the complications).
the first one is fine, the second : -

chiusura_automatica_box_quindici_minuti:
  value_template: "{{ as_timestamp(states('sensor.date') ~ ' ' ~ states('sensor.time')) == as_timestamp(states('sensor.date') ~ ' ' ~ states('input_select.orario_chiusura')) + 900 }}"
  friendly_name: 'Chiusura Automatica Box Dopo 15 Minuti'

… has the issue that if it rolls over midnight will never trigger (but works otherwise). it’s imperfect - needs improvement :smiley:

That will work over midnight. You’re converting both sides to timestamps which is a universal integer based off the epoch.

1 Like

Yes, that’s what I thought; but look again, if the second trigger rolls over midnight, so will its target time ie its always chasing tomorrow at 00:05 not today at 00:05 (if it was set at 23:50 and the second trigger is thus “Tommorow at 00:05” and it’s always “Tommorow at 00:05” because the first trigger is always “Today at 23:50”)

I see what you’re talking about now. Just gotta offset it minus a day if the initial time is inside a 15 minute window around midnight.

{% set t = states('sensor.time') %}
{% set offset = 900 - 86400 if t > 23:45 else 900 %}
{{ as_timestamp(states('sensor.date') ~ ' ' ~ t) == as_timestamp(states('sensor.date') ~ ' ' ~ states('input_select.orario_chiusura')) + offset }}
1 Like

Yep, that should work and even better we could define the offset and use the variable in all instances (which will accommodate people with different offsets), that’s even simpler than where I was going with it. :+1:

Just as an update, I put the condition&action part of the second automation into the action part of the first automation.
Everything worked at first try.
Thanks again.