ok, cool, no worries. I will be patient
it does however check with my earlier finding that when adding &d it always showed 1 for day, even when the alarm was set to the same day.
It now adds 1 correctly, so always shows 2… if the alarm is set to more than 24 hours from now.
please let me ask a related question.
I’ve changed the output of the sensor to:
{% if days %}
{{ alarm | timestamp_custom('%-d:%-H:%-M', False) }}
{% else %}
{{ alarm | timestamp_custom('%-H:%-M', False) }}
{% endif %}
so I can use that for my tts.google_say script:
say_sleep_left:
alias: Say Sleep Left
sequence:
- service: tts.google_say
data_template:
entity_id:
- >
{{states('sensor.intercom')}}
- >
{{states('sensor.sleep_radio')}}
message: >
Good {{states('sensor.day_phase')|lower}},
{% set time = states('sensor.time_until_next_alarm') %}
{% if time == 'Not set, relax' %} alarm is {{time|lower}}.
{% elif time|length > 5%}
next alarm will be in
{% if time.split(':')[1] == '0' %} {{time.split(':')[0] }} days {{time.split(':')[2] }} minutes.
{% elif time.split(':')[2] == '0' %} {{time.split(':')[0] }} days and {{time.split(':')[1] }} hours exactly.
{% else %} {{time.split(':')[0] }} days, {{ time.split(':')[1] }} and {{time.split(':')[2] }} minutes.
{% endif%}
{% else %}
next alarm will be in
{% if time.split(':')[0] == '0' %} {{time.split(':')[1] }} minutes.
{% elif time.split(':')[1] == '0' %} {{time.split(':')[0] }} hours exactly.
{% else %} {{time.split(':')[0] }} hours and {{time.split(':')[1] }} minutes.
{% endif%}
{% endif %}
{% set phase = states('sensor.day_phase') %}
{% if phase in ['Morning','Day'] %} Have a nice {{phase|lower}}.
{% elif phase == 'Evening' %} Enjoy your {{phase|lower}}
{% else %} Sleep well.
{% endif %}
this does work but might not be as intelligent as it could be (since it is hard coded), using a |length
filter in the template to decide which message is spoken.
Would you change that?
Also, it now shows:
so I changed the customize template for the unit_of_measurement in to
sensor.time_until_next_alarm:
templates:
icon: >
if (entities['input_boolean.alarmclock_wd_enabled'].state === 'on' ||
entities['input_boolean.alarmclock_we_enabled'].state === 'on') return 'mdi:clock-end';
return 'mdi:alarm-off';
unit_of_measurement: >
if (state ==='Not set, relax') return null;
if (state.length > 5) return 'D:H:M';
return 'H:M';
building on this, and this might be a bit of a challenge, I was wondering it it could be perfected and take into account the booleans for enabling alarm on weekdays and weekend days:
input_boolean.alarmclock_wd_enabled
input_boolean.alarmclock_we_enabled
scenario:
- if both ‘on’, the current template would be perfect (only check the strange day count of 2)
- if both 'off, simply state ‘Not set, relax’
- if only weekend enabled, have it skip the weekdays and on sunday add the 6 days to the next saterday
- if only weekday enabled, have it skip the weekend days and on friday add the 3 days to jump to monday
carefully expanding like this:
value_template: >
{% if is_state('input_boolean.alarmclock_wd_enabled','on') or
is_state('input_boolean.alarmclock_we_enabled','on') %}
{%- 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 today_alarm = getalarm() | float %}
{% set tomorrow_alarm = getalarm(1) | float %}
{% set alarm = tomorrow_alarm if today_alarm < 0 else today_alarm %}
{% set days = alarm // 86400 %}
{% if days %}
{{ alarm | timestamp_custom('%-d:%-H:%-M', False) }}
{% else %}
{{ alarm | timestamp_custom('%-H:%-M', False) }}
{% endif %}
{% elif is_state('input_boolean.alarmclock_wd_enabled','off') and
is_state('input_boolean.alarmclock_we_enabled','off') %} Not set, relax
{% endif %}
but the big challenge lies ahead…(still using the ‘or’ in the first template, which has to be changed obviously )