Custom Time and Date format

My Date and Time data are as shown in the image.
Almanaque
However I want them to be displayed like this: Mon 15-02-2021 4:30PM.
To achieve this, I have consulted the example in the Home Assistant documentation (Time&Date), the Phyton standard and this forum.
This is how I modified my configuration file:

############ Se añade el sensor de Fecha y Hora ##################
  - platform: time_date
    display_options:
      - 'date_time_iso'

###### Personalización de Fecha y Hora ##########################
  - platform: template
    sensors:
      time_formatted:
        friendly_name: Fecha y Hora
        value_template: "{{ as_timestamp(states('sensor.date_time_iso')) | timestamp_custom('%a %d %m %Y, %I:%M %p') }}"
        icon_template: mdi:calendar-clock

What am I doing wrong?
Thanks in advance.
3 Likes

You don’t need sensor.date_time_iso.

This will report the date and time in the desired format and will update every minute:

  - platform: template
    sensors:
      time_formatted:
        friendly_name: Fecha y Hora
        value_template: "{{ now().timestamp() | timestamp_custom('%a %d-%m-%Y %-I:%M %p') }}"
        icon_template: mdi:calendar-clock

If you want 4:30 instead of 04:30 then you must use %-I to eliminate the leading zero.

5 Likes

Thank you very much for your response.
But, if I disable the “date_time_iso” sensor, what should I do to make it appear in the “entities card” with the title “Almanaque”?

I did not suggest to disable sensor.date_time_iso. You simply don’t need it for the Template Sensor called sensor.time_formatted.

If you have some other purpose for it then you should definitely not disable it.

Forgive my clumsiness. But, my knowledge of English is very limited.
Thank you again.

No need to apologize, I should have been more clear in my first post.

Glad to hear the suggested template answers your question.

Please consider marking my first post with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has an accepted solution. Effectively, it marks this topic as being resolved and helps users find answers to similar questions.

Following your instructions, I have changed my configuration file like this:

###### Personalización de Fecha y Hora ##########################
  - platform: template
    sensors:
      time_formatted:
        friendly_name: Fecha y Hora
        value_template: "{{ now().timestamp() | timestamp_custom('%a %d-%m-%Y %-I:%M %p') }}"
#        value_template: "{{ as_timestamp(states('sensor.date_time_iso')) | timestamp_custom('%a %d %m %Y, %I:%M %p') }}"
        icon_template: mdi:calendar-clock

Unfortunately, the format has not changed, it remains the same as the image in the initial post.
What have I done wrong?
Thanks

After you have modified the configuration file, you have to instruct Home Assistant to reload the file. If you don’t do that it is unaware you changed anything and will continue to use the previous version.

  • First, execute Configuration > Server Controls > Check Configuration. Make sure it doesn’t report any errors. If it does then it means your modifications introduced an error and it must be corrected.
  • Second, assuming there are no errors, execute Configuration > Server Controls > Reload Template Entities.

I have discovered my error and, now, the date and time are displayed as I want.
Besides modifying my “template”, in the “card entities” it is necessary to replace “sensor.date_time_iso” by the new sensor “sensor.time_formatted”.
An inexperienced mistake.
Thank you very much for your time.

For the result to be perfect, I just need to get the name of the days of the week to be displayed in Spanish.
Somewhere I will have to add es_ES. I have consulted the Phyton documentation and I have made a test without success.
Can someone help me?
Thanks in advance.

If you use timestamp_custom it will convert %a into the day in English only. It doesn’t use the local language (at least not in Home Assistant). Wherever you are adding es_ES won’t help (as you have already discovered).

Let me know if you still want the day in Spanish and I’ll show you a different way to do it (using a slightly more sophisticated template).

Thank you very much for your offer.
If you are willing to give me your time, I would like the days of the week to appear like this:
lunes, martes, miércoles, jueves, viernes, sábado, domingo

  - platform: template
    sensors:
      time_formatted:
        friendly_name: Fecha y Hora
        value_template: >
          {% set days = ['lunes', 'martes', 'miércoles', 'jueves', 'viernes', 'sábado', 'domingo'] %}
          {{ days[now().weekday()] }} {{ now().timestamp() | timestamp_custom('%d-%m-%Y %-I:%M %p') }}
        icon_template: mdi:calendar-clock

To prove it works, here’s the template’s result shown in the Template Editor:

The time is shown as: sábado 13-02-2021 6:06 PM

4 Likes

Again, thank you very much.
I will try it this afternoon and report back with the result.

Your template works perfectly. Now my “Almanaque” card looks like this
Almanaque_02
Thank you very much for your help to an 80 year old, inexperienced man, who uses Home Assistant to control his lights and weather station and that serves me to keep me active.

9 Likes

Can you share your Almanaque card code here?
I would love to have it on my dashboard

Thank you so much. How will you do it when the day (time) is an attribute of a sensor not the sensor itself?

Use the state_attr() function to get the value of the sensor’s date attribute. Use that value instead of now() in the template shown above.

Reference: Templating - States

1 Like

I have tried using the following:

# Example configuration.yaml entry
template:
  - sensor:
      - name: "time"
        state: "{{ as_timestamp(states('sensor.time')) | timestamp_custom('%I:%M:%S %p') }}"
      - name: "date"
        state: "{{ as_timestamp(states('sensor.date')) | timestamp_custom('%a %B %d') }}"

The date works as expected, but the time does not. My intent is to have 12Hour:Minute:Second AM/PM displayed. I’ve consulted the Python Date/Time formatting as per the HA Time & Date page, but I seem to be missing something.

Can anyone point me in the right direction?

Much obliged.

template:
  - sensor:
      - name: "time"
        state: "{{ now().timestamp() | timestamp_custom('%I:%M:%S %p') }}"
      - name: "date"
        state: "{{ now().timestamp() | timestamp_custom('%a %B %d') }}"
2 Likes