I don’t like the timestamp format used by the app(s) so I first did a simple Y-m-d H:m format, but even that looked unnatural.
2020-09-30… instead of just “tomorrow”.
So I decided to build a template that makes a human friendly format.
- Unavailable is replaced with “No alarm”
- “Today hh:mm”
- “Tomorrow hh:mm”
- “Monday hh:mm” (where monday is in local language)
- “2020-10-05 hh:mm” if the date is more than a week away
of course the actual format can be changed.
The first five lines is the part that needs to be customized, and the outputs i.e. “Inget alarm” => “No alarm” and “Imorgon” => “Tomorrow”.
If you do not need a translation of the day then remove the first line and replace {{ day[timestamp | timestamp_custom("%A",true)] }} {{ timestamp | timestamp_custom(timeformat, true) }}
with {{ timestamp | timestamp_custom("%A " ~ timeformat, true) }}
sensor:
- platform: template
sensors:
alarm_andreas:
friendly_name: "Alarm Andreas"
value_template: >-
{% set day = { 'Monday':'Måndag', 'Tuesday':'Tisdag', 'Wednesday':'Onsdag', 'Thursday':'Torsdag', 'Friday':'Fredag', 'Saturday':'Lördag', 'Sunday':'Söndag' } %}
{% set fullformat = '%Y-%m-%d %H:%M' %}
{% set timeformat = '%H:%M' %}
{% set timestamp = (state_attr('sensor.andreas_next_alarm', 'Time in Milliseconds') /1000) | int %}
{% if states('sensor.andreas_next_alarm') == 'unavailable' %}
Inget alarm
{% elif timestamp | timestamp_custom('%Y-%m-%d', true) == (as_timestamp(states.sensor.date.state)+ (86400)) | timestamp_custom("%Y-%m-%d",true)%}
Imorgon {{ timestamp | timestamp_custom(timeformat, true) }}
{% elif (1+ (timestamp - now().timestamp() | int) / 86400) | int == 1 %}
Idag {{ timestamp | timestamp_custom(timeformat, true) }}
{% elif (1+ (timestamp - now().timestamp() | int) / 86400) | int <= 7 %}
{{ day[timestamp | timestamp_custom("%A",true)] }} {{ timestamp | timestamp_custom(timeformat, true) }}
{% else %}
{{ timestamp | timestamp_custom(fullformat, true) }}
{% endif %}
Edited to add “today” was missing.