Customize Date and Time format

according to [Time & Date - Home Assistant], the customize date and time command should be as below

template:
  - sensor:
      - name: "Date and time test"
        state: "{{ as_timestamp(states('sensor.date_time')) | timestamp_custom('%d-%b-%Y, %I:%M:%S %p') }}"
        icon: "mdi:calendar-clock"

but somehow when I paste it to configuration.yaml. The date and time sensor show unavailable.

image

is that something wrong with my command?

The actual date & time format that I need is “DD-MMM-YYYY, h:mm:ss AM/PM”

You don’t need the Time & Date sensor for this application.

template:
  - sensor:
      - name: "Date and time test"
        state: "{{ now().timestamp() | timestamp_custom('%d-%b-%Y, %I:%M:%S %p') }}"
        icon: "mdi:calendar-clock"

Also keep in mind that dates and times will be formatted according to your locale, if set correctly. I’m not sure if what you’re attempting here is that, or something different from what you’re used to natively.

template:
  - sensor:
      - name: "Date and time test"
        state: "{{ now().timestamp() | timestamp_custom('%d-%b-%Y, %I:%M:%S %p') }}"
        icon: "mdi:calendar-clock"

above code work but second is not moving, it only update on start of the minute. Possible to made the second to be update on realtime.

I need to display time with second & customize the date layout.

now() and the Time & Date sensors update once a minute. I suggest you review this section of the documentation to understand why: Rate Limiting Updates

You won’t get per second updates with a template that merely computes time. Refer to the documentation to see an example of what does produce per second updates.

You can try adding the example into your Template Sensor (see below) and see if it causes it to be updated every second. I haven’t tried it myself so I can’t be certain if it works (it may get throttled to once a minute because of the presence of now()). If it doesn’t work, then I know of no other way to do it.

template:
  - sensor:
      - name: "Date and time test"
        state: >
          {% set x = states.sensor | selectattr('state', 'in', ['unavailable', 'unknown', 'none']) | list | count %} 
          {{ now().timestamp() | timestamp_custom('%d-%b-%Y, %I:%M:%S %p') }}
        icon: "mdi:calendar-clock"
2 Likes

although sometime it might skip 1 or 2 sec but it work !! now the second ticking. I really appreciate your help to solved this, thanks alot :slight_smile:

1 Like

I notice another thing as well, home-assistant_v2.db file size is increasing kind of fast although I already excluded that “date & time test” sensor from the recorder, or maybe there is something else is filling up the database.

Yes, you will definitely want to exclude this Template Sensor from having its history recorded. One database entry per second will inflate your database to an unwieldy size very quickly!

FWIW, just another reason why a time-based update defaults to once a minute.

1 Like