I’m just curious but will this work with rolling days?
Say that you want the alarm to start at 00:10:00 (just after midnight) with a duration of 25 minutes.
This wait_template
implies that it will not work.
0 < as_timestamp(states(sensor) if sensor != ''none'' else states(''sensor.date'')
~ '' '' ~ manual_time) - as_timestamp(states(''sensor.date_time_iso'')) <= float(seconds)
Test:
{%- set manual_time = '00:10:00' %}
{%- set seconds = 25*60 %}
{%- set date = "2021-01-22" %}
{%- set nnow = strptime("2021-01-22T23:55", "%Y-%m-%dT%H:%M")%}
{{ as_timestamp(date ~ ' ' ~ manual_time) - as_timestamp(nnow) }}
{{ 0 < as_timestamp(date ~ ' ' ~ manual_time) - as_timestamp(nnow) <= float(seconds) }}
Returns False
.
This is a suggestion on how to fix it:
{% set date = "2021-01-22" %}
{% set seconds = 25*60 -%}
Seconds to consider: {{ seconds }}
{% for manual_time in ['23:56:00', '00:10:00'] %}
{%- for iso_date in ["2021-01-22T23:55", "2021-01-22T22:55", ] %}
{%- set nnow = strptime(iso_date, "%Y-%m-%dT%H:%M")%}
{% set time_diff = (as_timestamp(date ~ ' ' ~ manual_time) - as_timestamp(nnow) ) -%}
Alarm time: '{{manual_time}}', now: {{nnow}}:
Original: {{ 0 < as_timestamp(date ~ ' ' ~ manual_time) - as_timestamp(nnow) <= float(seconds) }}, 0 > {{as_timestamp(date ~ ' ' ~ manual_time) - as_timestamp(nnow) }} <= {{ seconds }}
New: {{ (0 < time_diff <= seconds) or ((24*60*60)-seconds < time_diff*-1 < (24*60*60))
}}, {{ 0 }} < {{time_diff}} <= {{seconds}} or {{(24*60*60)-seconds }} < {{ time_diff*-1}} < {{(24*60*60) }}
{%- endfor %}
{%- endfor %}
Which outputs this:
Seconds to consider: 1500
Alarm time: '23:56:00', now: 2021-01-22 23:55:00:
Original: True, 0 > 60.0 <= 1500
New: True, 0 < 60.0 <= 1500 or 84900 < -60.0 < 86400
Alarm time: '23:56:00', now: 2021-01-22 22:55:00:
Original: False, 0 > 3660.0 <= 1500
New: False, 0 < 3660.0 <= 1500 or 84900 < -3660.0 < 86400
Alarm time: '00:10:00', now: 2021-01-22 23:55:00:
Original: False, 0 > -85500.0 <= 1500
New: True, 0 < -85500.0 <= 1500 or 84900 < 85500.0 < 86400
Alarm time: '00:10:00', now: 2021-01-22 22:55:00:
Original: False, 0 > -81900.0 <= 1500
New: False, 0 < -81900.0 <= 1500 or 84900 < 81900.0 < 86400
Suggestion for change:
- wait_template: '{{0 < as_timestamp(states(sensor) if sensor != ''none'' else states(''sensor.date'')
~ '' '' ~ manual_time) - as_timestamp(states(''sensor.date_time_iso'')) <= float(seconds)
and states(check_entity) in [''unknown'', ''on'', ''home'']}}'
#TO
- wait_template: >-
{% set time_diff = as_timestamp(states(sensor) if sensor != 'none' else
states('sensor.date') ~ ' ' ~ manual_time) - as_timestamp(states('sensor.date_time_iso')-%}
{{(0 < time_diff <= float(seconds) or (86400 - float(seconds) < time_diff|abs < 86400) and
states(check_entity) in ['unknown', 'on', 'home']}}