Time Sensor display 12HR AM/PM

I have searched everywhere for this.
Like most things with HA always harder than it seams.
Anyone know how to get the standard time sensor
to display 12HR AM/PM

sensor:
  - platform: time_date
    display_options:
      - 'time'

Thanks…

1 Like

you can create a template sensor and do whatever you want wit the standard time - take look at this to get the idea.
but if it’s 24HR - what’s the point?

I was trying to get 12HR my error.
Updated post to be correct.
Will check out post you suggested.

Put this in my configuration.yaml and it added a new sensor with 12HR time.

Thanks for the help.

sensor:
  - platform: template
    sensors:
      timenew:
        friendly_name: 'XTime'
        value_template: >
          {% set time = states('sensor.time') %}
          {% set hour = time[:2]|int %}
          {{ '{:02d}:{} {}'.format(hour if hour == 12 else hour % 12, time[3:5], 'PM' if hour>11 else 'AM') }}
2 Likes

Does anyone know how to get the PM to display under the time in the sensor?
3:45
PM

Also if you remove the leading zero from the last template line it wont pad the time

{:02d}

Change to

{:2d}

you’re welcome
please consider marking a post with a solution by checking a checkbox next to it.

it’s might be possible using card-mod but it’s better to have a separate sensor with AM/PM.

huh? the TS asked for the padded time hence the format.

Tried a “\n” in jinja and it pushes the PM down to the next line but left formats it
and then in the actual sensor it does not push it down.
Do you know if the sensors can have 2 lines displayed?

Some sensors have things in the bottom like % any way to get the AM PM to
display there?

I doubt you it’s reasonable to spend time trying with only one sensor as standard cards (afaik) are not designed for that, you’ll have no control over it or the result will be ugly.

However, you can try Entity card - but then you need to modify your template sensor so it returns time as state and AM/PM as attribute and configure your Entity card accordingly (it’s a reasonably new card so I haven’t played with it yet).

Generally your choice is a) a vertical layout with 2 Entities cards (each has just one sensor) (won’t look nice imho) or b) custom card: there are many, one of the most flexible (but also difficult to configure for a novice) is button-card.
Let me know if you need further help with it.

That’s a little more than I was planning on doing just to display the sensor correctly.
Can do without the AM/PM for now.
Thanks for the help.

I decided to play with this new Entity card so you’re welcome to use it if it suits your needs.
It looks like this (icon can be changed/hidden)
Screen Shot 2020-04-15 at 16.35.58 (2)
Here’s the modified sensor

- platform: template
  sensors:
    timenew:
      icon_template: mdi:clock
      friendly_name_template: >
        {% set time = states('sensor.time') %}
        {% set hour = time[:2]|int %}
        {{ '{:02d}:{}'.format(hour if hour == 12 else hour % 12, time[3:5]) }}
      value_template: whocares
      attribute_templates:
        am_pm: >
          {% set time = states('sensor.time') %}
          {% set hour = time[:2]|int %}
          {{ 'PM' if hour>11 else 'AM' }}

and here’s the LL card

type: entity
entity: sensor.timenew
attribute: am_pm

Looks good.
I will play around with it.
Thanks…

Thanks for the 12hr adjustment!

But do you know how to set that sensor.timenew into a input_datetime.set_datetime?

I’m currently using this in an automation to set the current time based off a boolean trigger…


    - service: input_datetime.set_datetime
      data_template:
        entity_id: input_datetime.last_walk_date_time
        time: > 
          {{ now().timestamp() | timestamp_custom("%H:%M:%S", true) }} 

But this obviously sets it in 24hr time format.

I’m sure this is completely the wrong way to go about this, but its 2:30am and my brain is fried. Any help would be appreciated. Thanks

Hello;

I am using this time input card: Georgi Gardev’s “Time Picker Card”. The resulting sensor still reads “00:00am” at Midnight. I tried fiddling with the ....format(hour if hour == 12... stuff to add an (if == 00), to no avail. Couldn’t get it to work. Can anyone help?

Just figured this out. Here you go (this should get 0 to display as 12):

# AM/PM Time Sensor
sensor:
  - platform: template
    sensors:
      timenew:
        friendly_name: 'XTime'
        value_template: >
          {% set time = states('sensor.time') %}
          {% set hour = time[:2]|int %}
          {% set timenew = '{:2d}:{}'.format(hour if hour == 12 else hour % 12, time[3:5]) %}
          {% set hournew = timenew[:2]|int %}
          {% set hournew2 = (hournew + 12 if hournew == 0 else hournew) %}
          {{ '{:2d}:{} {}'.format(hournew2 if hournew2 == 12 else hournew % 12, timenew[3:5], 'PM' if hour>11 else 'AM') }}

Better late than never, lol. There’s gotta be a more elegant way of achieving this, but I hope someone still finds this useful.

1 Like

rg3d
Thanks for the reply.
I will implement you suggestion.
I stopped caring about simple things like this that were important to me in HA along time
ago because of the toxic replies and comments that you should not want AM/PM because
someone else doesn’t get why that may matter to you .

2 Likes
sensor:
  - platform: template
    sensors:
      timenew:
        friendly_name: 'XTime'
        value_template: "{{ now().strftime('%-I:%M %p') }}"

@wakeskate

or new style

template:
- sensor:
  - name: Time
    unique_id: time_am_pm
    state: "{{ now().strftime('%-I:%M %p') }}"

%-I is the current hour that removes the leading zero. I.e. 1, through 12.
%M is the current minute.
%p is AM/PM

you can remove the - in the %-I to get 01 through 12, i.e. keep the leading zero.

3 Likes

This definitely looks more elegant than my solution, but where does stftime come from? I’m getting “UndefinedError: ‘datetime.datetime object’ has no attribute ‘stftime’” when I try this in the template dev tool.

typo, fixed

1 Like

To be clear: Your example: now().strftime('%-I:%M %p') is recommended over using platform: time_date ?

Since now() is always different, this doesn’t update the sensor too often?

Or perhaps my sense of performance impact from something like this is just wrong?

Now causes the template to only update on the minute. So there should be virtually no impact on your system.

1 Like