and, just to give full feedback and thanks, this is what I am using now and things seem to be working perfectly.
Did have to create an automation to update all sensors, immediately and not await sensor.time. I had the commented entity_ids in first, but they wouldn’t make a difference for the updating of the sensors. Automation does it better. Now only have the entities listed which are used in the template, and even they might not be necessary , safe the sensor.time.
number_of_days_next_alarm:
friendly_name: Days to next alarm
entity_id:
- sensor.time
- sensor.alarmclock_wd_time
- sensor.alarmclock_we_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: >
{% if is_state('input_boolean.alarmclock_wd_enabled','on') %}
{% if now().weekday() in [0,1,2,3] %}
{% if now().time().strftime('%H:%M') < states('sensor.alarmclock_wd_time') %} 0
{% else %} 1
{% endif %}
{% elif now().weekday() == 4 %}
{% if is_state('input_boolean.alarmclock_we_enabled','on') %}
{% if now().time().strftime('%H:%M') < states('sensor.alarmclock_wd_time') %} 0
{% else %} 1
{% endif %}
{% else %} 3
{% endif %}
{% elif now().weekday() == 5 %}
{% if is_state('input_boolean.alarmclock_we_enabled','on') %}
{% if now().time().strftime('%H:%M') < states('sensor.alarmclock_we_time') %} 0
{% else %} 1
{% endif %}
{% else %} 2
{% endif %}
{% elif now().weekday() == 6 %}
{% if is_state('input_boolean.alarmclock_we_enabled','on') %}
{% if now().time().strftime('%H:%M') < states('sensor.alarmclock_we_time') %} 0
{% else %} 1
{% endif %}
{% else %} 1
{% endif %}
{% endif %}
{% elif is_state('input_boolean.alarmclock_we_enabled','on') %}
{% if now().weekday() in [0,1,2,3] %} {{5 - now().weekday()}}
{% elif now().weekday() in [4] %} 1
{% elif now().weekday() in [5] %}
{% if now().time().strftime('%H:%M') < states('sensor.alarmclock_we_time') %} 0
{% else %} 1
{% endif %}
{% elif now().weekday() == 6 %}
{% if now().time().strftime('%H:%M') < states('sensor.alarmclock_we_time') %} 0
{% else %} 6
{% endif %}
{% endif %}
{% else %} Not set
{% endif %}
next_alarm_day:
# icon_template: >
# {% if not is_state('sensor.next_alarm_day','Not set') %} mdi:calendar-clock
# {% else %} mdi:alarm-off
# {% endif %}
friendly_name: Next alarm day
entity_id:
- sensor.time
- sensor.number_of_days_next_alarm
- sensor.next_alarm_daytype
# - sensor.time_until_next_alarm
# - 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: >
{% set offset = states('sensor.number_of_days_next_alarm')|int %}
{% if is_state('sensor.next_alarm_daytype','Today,')%}
{{states('sensor.next_alarm_daytype')}} {{ as_timestamp(now().replace(day=now().day)) | timestamp_custom('%A') }}
{% elif states('sensor.next_alarm_daytype') in ['Tomorrow,', 'The day after tomorrow,'] %}
{{states('sensor.next_alarm_daytype')}} {{ as_timestamp(now().replace(day=now().day + offset)) | timestamp_custom('%A')}}
{% elif is_state('sensor.number_of_days_next_alarm','Not set') and is_state('sensor.next_alarm_daytype','Not set') %}
Not set
{% else %}
{{ as_timestamp(now().replace(day=now().day + offset)) | timestamp_custom('%A')}}
{% endif %}
next_alarm_daytype:
friendly_name: Next alarm daytype
entity_id:
- sensor.time
- sensor.time_until_next_alarm
- 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: >
{% set days = states('sensor.number_of_days_next_alarm') %}
{% if is_state('input_boolean.alarmclock_wd_enabled','off') and
is_state('input_boolean.alarmclock_we_enabled','off') %} Not set
{% elif days == '0' %} Today,
{% elif days == '1' %} Tomorrow,
{% elif days == '2' %} The day after tomorrow,
{% else %} More than 2: ({{days}}) days
{% endif %}
time_until_next_alarm:
friendly_name: 'Time until next alarm'
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 weekday = is_state('input_boolean.alarmclock_wd_enabled','on') %}
{% set weekend = is_state('input_boolean.alarmclock_we_enabled','on') %}
{% set current_day = now().weekday() %}
{% if weekday and not weekend %}
{% set offset = 7-current_day if current_day in range(4,7) else 1 %}
{% elif not weekday and weekend %}
{% if current_day in range(4) %}
{% set offset = 5 - current_day %}
{% else %}
{% set offset = 6 if current_day == 6 else 1 %}
{% endif %}
{% elif weekday and weekend %}
{% set offset = 1 %}
{% else %}
{% set offset = None %}
{% endif %}
{% if offset != None %}
{% set today_alarm = getalarm() | float %}
{% set next_alarm = getalarm(offset) | float %}
{% set alarm = next_alarm if today_alarm < 0 else today_alarm %}
{% if offset > 1 %}
{{ (next_alarm // 86400) |int }}:{{ next_alarm | timestamp_custom('%-H:%-M', False) }}
{% else %}
{{ alarm | timestamp_custom('%-H:%-M', False) }}
{% endif %}
{% else %}
Not set, relax
{% endif %}
next_alarm_text:
friendly_name: Next alarm text
entity_id:
- sensor.time_until_next_alarm
- sensor.next_alarm_day
# - sensor.number_of_days_next_alarm
# - sensor.next_alarm_daytype
value_template: >
{% set time = states('sensor.time_until_next_alarm') %}
{% set next_alarm = states('sensor.next_alarm_day')|lower %}
{% set pickup = 'Next alarm will be ' + next_alarm + ' in ' %}
{% if time == 'Not set, relax' %} Alarm is {{time|lower}}
{% elif (time.split(':')|length - 1) > 1 %}
{{pickup}}
{% set day = 'day' if time.split(':')[0]== '1' else 'days' %}
{% set hour = 'hour' if time.split(':')[1]== '1' else 'hours' %}
{% set minute = 'minute' if time.split(':')[2]== '1' else 'minutes' %}
{% if time.split(':')[1] == '0' %} {{time.split(':')[0] }} {{day}} {{time.split(':')[2] }} {{minute}}
{% elif time.split(':')[2] == '0' %} {{time.split(':')[0] }} {{day}} and {{time.split(':')[1] }} {{hour}} exactly
{% else %} {{time.split(':')[0] }} {{day}}, {{ time.split(':')[1] }} {{hour}} and {{time.split(':')[2] }} {{minute}}
{% endif%}
{% else %}
{{pickup}}
{% set hour = 'hour' if time.split(':')[0]== '1' else 'hours' %}
{% set minute = 'minute' if time.split(':')[1]== '1' else 'minutes' %}
{% if time.split(':')[0] == '0' %} {{time.split(':')[1] }} {{minute}}
{% elif time.split(':')[1] == '0' %} {{time.split(':')[0] }} {{hour}} exactly
{% else %} {{time.split(':')[0] }} {{hour}} and {{time.split(':')[1] }} {{minute}}
{% endif%}
{% endif %}
and automation:
- alias: 'Update next alarm'
id: 'Update next alarm'
initial_state: 'on'
trigger:
platform: state
entity_id:
- sensor.time_until_next_alarm
- 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
action:
service: homeassistant.update_entity
entity_id:
- sensor.next_alarm_day
- sensor.next_alarm_daytype
- sensor.number_of_days_next_alarm