Format input_datetime to AM / PM instead of 24 hours when send a message to my phone

I have an input datetime for something that I want to send notification to my phone showing me the set time of the input, but I want it to output as AM/PM in the message to my phone. The template formatting I’m trying gives me an error

Error message:

"ValueError: Template error: timestamp_custom got invalid input '19:58:00' when rendering template '{{ states('input_datetime.bedroom_ac_nightly') | timestamp_custom('%I:%M %p')}}' but no default was specified" 

Template I’m trying:

{{ states('input_datetime.bedroom_ac_nightly') | timestamp_custom('%I:%M %p')}}

timestamp_custom() requires a timestamp as input, not a “HH:MM:SS” string.

It’s likely easier to use datetime methods instead of timestamps…

{{ today_at(states('input_datetime.bedroom_ac_nightly')).strftime('%-I:%M %p') }}

If you prefer timestamps you need to complete the datetime string with an arbitrary date and convert it to a timestamp:

{{ as_timestamp('2023-01-01 '~states('input_datetime.bedroom_ac_nightly'))
| timestamp_custom('%-I:%M %p') }}
3 Likes

You’re a lifesaver, I could not find any examples of this anywhere. Thanks!

Me too - this puzzled me for ages. Thanks for your post!