Change the format of time displayed in HA?

I grabbed some sensors (code) off this forum to display sunset and sunrise times in HA, and they’re displayed like so:

Sunrise: 05:59
Sunset: 18:24

Is there any way to change the format of the time, from 05:59 to 5:59 AM, and from 18:24 to 6:24 PM?

Here’s how I am doing it:

 nextsunrise:
   friendly_name: 'Next Sunrise'
   value_template: '{{ as_timestamp(states.sun.sun.attributes.next_rising) | timestamp_custom("%D %I:%M %P") }}'

 nextsunset:
   friendly_name: 'Next Sunset'
   value_template: '{{ as_timestamp(states.sun.sun.attributes.next_setting) | timestamp_custom("%D %I:%M %P") }}'
   
 dayofyear:
   friendly_name: 'Day Number'
   value_template: '{{ now().strftime("%j") }}'

 weekofyear:
   friendly_name: 'Week Number'
   value_template: '{{ now().strftime("%U") }}'

Which gives me this:

For time formatting, this will come in handy: http://strftime.org

4 Likes

Great, thanks for the help!

Yey, i did it in spanish but adding the names of the days!

sensor:
    - platform: template
      sensors:
        date_long:
          friendly_name: 'Fecha'
          value_template: >
            {% set months = ["Ene", "Feb", "Mar", "Abr", "May", "Jun", "Jul", "Ago", "Sep", "Oct", "Nov", "Dic"] %}
            {% set month = months[now().strftime('%m') | int -1] %}
            {% set dias = ["Lunes ", "Martes ", "Miercoles ", "Jueves ", "Viernes ", "Sabado ", "Domingo "] %}
          {% set dia = dias[now().strftime('%A') | int -1] %}
          {{ dia + now().strftime('%d') + ' ' + month + ' ' }}

1 Like