ok. so had to resort to below template again, since I couldn’t get the full all-in-one to work correctly. Think is was the template calculating the offset, because it never got to being 0.
below I can calculate
0= today_alarm
else always next_alarm, with a check comparing the current time to relevant alarm time.
value_template: >
{%- macro getalarm(offsetday=0) %}
{%- set day = (now().weekday() + offsetday) % 7 %}
{%- set offset = offsetday * 86400 %}
{%- set fmat = 'd' if day in range(5) else 'e' %}
{%- set hour = 'input_number.alarmclock_w{}_hour'.format(fmat) %}
{%- set minute = 'input_number.alarmclock_w{}_minute'.format(fmat) %}
{%- set alarm = states(hour) | float | multiply(3600) + states(minute) | float | multiply(60) %}
{%- set time = now().hour * 3600 + now().minute * 60 %}
{{ (offset - time) + alarm if offsetday else alarm - time }}
{%- endmacro %}
{% set week_on = is_state('input_boolean.alarmclock_wd_enabled','on') %}
{% set end_on = is_state('input_boolean.alarmclock_we_enabled','on') %}
{% set in_days = states('sensor.number_of_days_next_alarm_alt')|int %}
{% set today_alarm = getalarm() | float %}
{% set next_alarm = getalarm(in_days) | float %}
{% if week_on or end_on %}
{% if in_days == 0 %}
{{ today_alarm | timestamp_custom('%-H:%-M', False) }}
{% else %}
{% if (next_alarm // 86400 )|int == 0 %}
{{ next_alarm | timestamp_custom('%-H:%-M', False) }}
{% else %}
{{ (next_alarm // 86400 )|int }}:{{ next_alarm | timestamp_custom('%-H:%-M', False) }}
{% endif %}
{% endif %}
{% else %} Not set, relax
{% endif %}
or even shorter:
time_until_next_alarm_alt:
friendly_name: 'Time until next alarm alt'
entity_id:
- sensor.time
- input_boolean.alarmclock_wd_enabled
- input_boolean.alarmclock_we_enabled
- input_number.alarmclock_wd_hour
- input_number.alarmclock_wd_minute
- input_number.alarmclock_we_hour
- input_number.alarmclock_we_minute
value_template: >
{%- macro getalarm(offsetday=0) %}
{%- set day = (now().weekday() + offsetday) % 7 %}
{%- set offset = offsetday * 86400 %}
{%- set fmat = 'd' if day in range(5) else 'e' %}
{%- set hour = 'input_number.alarmclock_w{}_hour'.format(fmat) %}
{%- set minute = 'input_number.alarmclock_w{}_minute'.format(fmat) %}
{%- set alarm = states(hour) | float | multiply(3600) + states(minute) | float | multiply(60) %}
{%- set time = now().hour * 3600 + now().minute * 60 %}
{{ (offset - time) + alarm if offsetday else alarm - time }}
{%- endmacro %}
{% set week_on = is_state('input_boolean.alarmclock_wd_enabled','on') %}
{% set end_on = is_state('input_boolean.alarmclock_we_enabled','on') %}
{% set in_days = states('sensor.number_of_days_next_alarm_alt')|int %}
{% set alarm = getalarm(in_days) | float %}
{% if week_on or end_on %}
{% if (alarm // 86400 )|int == 0 %}
{{ alarm | timestamp_custom('%-H:%-M', False) }}
{% else %}
{{ (alarm // 86400 )|int }}:{{ alarm | timestamp_custom('%-H:%-M', False) }}
{% endif %}
{% else %} Not set, relax
{% endif %}
using the intermediary template again to calculate the number of days:
number_of_days_next_alarm_alt:
friendly_name: Days to next alarm alt
entity_id: sensor.time
value_template: >
{% set end_on = is_state('input_boolean.alarmclock_we_enabled','on') %}
{% set week_on = is_state('input_boolean.alarmclock_wd_enabled','on') %}
{% set week_time = now().time().strftime('%H:%M') < states('sensor.alarmclock_wd_time') %}
{% set end_time = now().time().strftime('%H:%M') < states('sensor.alarmclock_we_time') %}
{% set now = now().weekday() %}
{% if not end_on and not week_on %} Not set
{% elif now in [0,1,2,3] %}
{% if week_on %} {{'0' if week_time else '1' }}
{% elif end_on %} {{5 - now}}
{% endif %}
{% elif now == 4 %}
{% if week_on %}
{% if week_time %} 0
{% else %} {{ '1' if end_on else '3'}}
{% endif %}
{% else %} {{ '1' if end_on}}
{% endif %}
{% elif now == 5 %}
{% if end_on %} {{'0' if end_time else '1'}}
{% else %} {{'2' if week_on }}
{% endif %}
{% elif now == 6 %}
{% if end_on and not week_on %}
{{ '0' if end_time else '6' }}
{% elif end_on and week_on %}
{{ '0' if end_time else '1' }}
{% else %} {{ '1' if week_on }}
{% endif %}
{% endif %}
and this template to calculate the ‘daytype’:
next_alarm_day:
friendly_name: Next alarm day
entity_id:
- sensor.time
- sensor.number_of_days_next_alarm
- input_boolean.alarmclock_wd_enabled
- input_boolean.alarmclock_we_enabled
value_template: >
{% set offset = states('sensor.number_of_days_next_alarm') %}
{% set day_number = now().strftime('%-d') %}
{% if is_state('input_boolean.alarmclock_wd_enabled','off') and
is_state('input_boolean.alarmclock_we_enabled','off') %} {% set daytype = 'Not set' %}
{% elif offset == '0' %} {% set daytype = 'Today,'%}
{% elif offset == '1' %} {% set daytype = 'Tomorrow,' %}
{% elif offset == '2' %} {% set daytype = 'The day after tomorrow,' %}
{% elif offset > '2' %} {% set daytype = 'Next' %}
{% endif %}
{% if offset == 'Not set' %} Not set
{% elif offset == '0'%}
{{daytype}} {{ as_timestamp(now().replace(day=now().day)) | timestamp_custom('%A') }}
{% else %}
{% if now().day + offset|int > states('days_current_month')|int %}
{{daytype}} {{ as_timestamp(now().replace(month= +1).replace(day=(now().day +
offset|int - day_number|int))) | timestamp_custom('%A')}}
{% else %}
{{daytype}} {{ as_timestamp(now().replace(day=(now().day + offset|int )))| timestamp_custom('%A') }}
{% endif %}
{% endif %}
which has set another challenge for me, because simply replacing the day, caused it to surpass the allowed days of the current month. S0 again the need for an extra sensor was born… sensor.days_current_month:
days_current_month:
friendly_name: Days current month
entity_id: sensor.date
value_template: >
{% set month = now().strftime('%-m') %}
{% if month in ['1','3','5','7','8','10','12'] %} 31
{% elif month in ['4','6','9','11'] %} 30
{% else %} {{'29' if (now().strftime('%-y'))|int//4 == 0 else '28'}}
{% endif %}
hope this rather complex set does it now, for such a simple task as showing the next alarm :-))