Datetime template help

I have an alarm clock setup that uses input_datetime.manual_alarm as an input. I have a HAdashboard setup that I would like to show the datetime in a user friendly format. i.e. “9:30 PM”

The datetime is setup without using a date in configuration.yaml

input_datetime:
  manual_alarm:
    name: Manual Alarm Time
    has_date: false
    has_time: true 

If I put

{{ states.input_datetime.manual_alarm.state}}

in the template developer tool I get

06:35:00

So I was thinking my template should be something like this…

{{as_timestamp(strptime(states.input_datetime.manual_alarm.state, ‘%H:%M:%S’)) | timestamp_custom(‘%-I:%M %p’)}}

but I get “Unknown error rendering template”.

I believe the clue to its failure is provided by this example:

{{ strptime("06:35:00", '%H:%M:%S') }}

Result:
1900-01-01 06:35:00

Look at the year. It presents a problem for as_timestamp to convert it to a timestamp (Unix time). It’s too old.

According to the Wikipedia page for Unix time:

On systems where Unix time is stored as a signed 32-bit number, the largest value that can be recorded is 2147483647 (231 − 1), which is 03:14:07 Tuesday, 19 January 2038 UTC. The following second, the clock will wrap around to negative 2147483648 (−231), which is 20:45:52 Friday, 13 December 1901 UTC. This is referred to as the Year 2038 problem .

Similarly there’s a lower date limit and your example predates it.

If you put this into the Template Editor:

{{ as_timestamp(strptime("1902 06:35:00", '%Y %H:%M:%S')) }}

You get:
-2145875100.0

If you change the year to 1901 it results in Unknown error rendering template.


Try this:

{{states.input_datetime.manual_alarm.attributes.timestamp | timestamp_custom('%-I:%M %p', false)}}

EDIT
Corrected typo in the template.

Any ideas of other ways to template this to be in a more friendly format?

Did you try what I suggested?

I am so sorry. The email notification missed off the last line. I didn’t see the possible solution.

It was almost correct. The following works. Thank you for your help. I am sorry I missed it.

{{states.input_datetime.manual_alarm.attributes.timestamp | timestamp_custom(’%-I:%M %p’, false)}}

Sorry about that. For my tests I used an input_datetime with a completely different name. At the last second I replaced its name with yours … and made a copy-paste error (and needlessly included .state.)!

Glad to hear it ultimately worked out for you (and I’ve corrected my original post).