"human friendly" template for next alarm (companion app)

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.

4 Likes

Nice. Maybe change the category to “Share your projects” to get more attention.

1 Like

Just wanted to say thank you (I know a long time ago). I did have to modify it a bit to deal some errors in the most recent versions of HA. Here it is if it will help anywone.

  - platform: template
    sensors:
      alarm_chandlers:
        friendly_name: "Alarm Chandlers"
        unique_id: “alarm_chandlers_1”
        value_template: >-
                        {% set day = { 'Monday':'Monday', 'Tuesday':'Tuesday', 'Wednesday':'Wensday', 'Thursday':'Thursday', 'Friday':'Friday', 'Saturday':'Saturday', 'Sunday':'Sunday' } %}
                        {% set fullformat = '%Y-%m-%d %H:%M' %}
                        {% set timeformat = '%H:%M' %}

                        {% if states('sensor.chandlers_googlehome_alarms') != 'unavailable' %}
                        {% set timestamp = (state_attr('sensor.chandlers_googlehome_alarms', 'Time in Milliseconds') /1000) | int %}
                        {% endif %}

                        {% if states('sensor.chandlers_googlehome_alarms') == 'unavailable' %}
                        No alarm

                        {% elif timestamp | timestamp_custom('%Y-%m-%d', true) == (as_timestamp(states.sensor.date.state)+ (86400)) | timestamp_custom("%Y-%m-%d",true)%}
                        Today {{ timestamp | timestamp_custom(timeformat, true)  }}

                        {% elif (1+ (timestamp - now().timestamp() | int) / 86400) | int == 1 %}
                        Tomorrow {{ 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 %}
2 Likes

in your case you don’t need the translation of day names.

This part can be changed

{{ day[timestamp | timestamp_custom("%A",true)] }}

to this:

{{ timestamp | timestamp_custom("%A",true) }}

And the first line starting with {% set day can be completely removed.

Perfect and will make those two changes! Thank you again! Made the Sig Other much happier to see the alarms in the kids rooms being much easier to read!