Time Sensor AM/PM

Was searching the forum for this and can’t find anything.

Now that the time sensor allows us to set 12/24 hour format, is there any way to
have the sensor show AM or PM based on time of day,
in the lower circle like battery sensors show % or temp sensors show F.

You want the sensor ti simply have a value of AM or PM? I think you could do this with a template sensor.

Perhaps:

{{ now().timestamp() | timestamp_custom("%p") }}

Here is the sensor I created before you could set it up in the front end.
It shows the PM or AM in the sensor circle but that’s to big for the sensor.
What I am trying to do is get AM or PM to show in the lower middle of the sensor
circle like % does in an battery sensor.

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

Changed it to one of the examples above but that just puts PM in the inside the sensor.

  - platform: template
    sensors:
      timenew:
        friendly_name: 'XTime'
        value_template: >
          {{ now().timestamp() | timestamp_custom("%p") }}
#          {% set time = states('sensor.time') %}
#          {% set hour = time[:2]|int %}
#          {{ '{:2d}:{} {}'.format(hour if hour == 12 else hour % 12, time[3:5], "\n" 'PM' if hour>11 else 'AM') }}

I have 1 custom sensor that detects power that I have this line in.
It puts a w in the center of the circle like I am looking for.
unit_of_measurement: "w"
Is their an option for AM/PM that anyone know’s about?

If by “sensor circle” you mean a badge, then the % symbol that appears in the “lower middle” is the sensor’s unit_of_measurement. It’s not part of the sensor’s state value.

Screenshot from 2021-11-15 18-05-51

The unit_of_measurement option only accepts a string, not a template. That means you can put “AM” or “PM” in there but you can’t put a template that computes if the current time is AM or PM. In other words, it doesn’t do what you want it to do.

Thanks for the clarification.
Yes the Badge is what I mean.

Tried to changes below but getting an error when checking config.
If I put the unit_of_measure line into the template editor it resolves to ‘PM’

  - platform: template
    sensors:
      timenew:
        friendly_name: 'XTime'
        unit_of_measure: '{{ now().timestamp() | timestamp_custom("%p") }}'
        value_template: >
          {% set time = states('sensor.time') %}
          {% set hour = time[:2]|int %}
          {{ '{:2d}:{} {}'.format(hour if hour == 12 else hour % 12, time[3:5], "\n" 'PM' if hour>11 else 'AM') }}

Here is the error I get.
invalid config for [sensor.template]: [unit_of_measure] is an invalid option for [sensor.template]. Check: sensor.template->sensors->timenew->unit_of_measure. (See ?, line ?).

The error tells you this is not a valid option:

Looking at the documents you should be able to see why:

https://www.home-assistant.io/integrations/template/#unit_of_measurement

I have this custom sensor that works fine with no error.
Can you tell me the difference?

  - platform: template
    sensors:
      washer_watts:
        friendly_name: "Washing Machine Power"
#        icon: mdi:washing-machine
        unit_of_measurement: "w"
        value_template: '{{ states.switch.wemo_insight.attributes.current_power_w | round(0) }}'

Found the issue.
I had unit_of_measure not unit _of_measurement
Will test to see if it works now.

1 Like

No error now when checking config but it puts the whole expression where the w should be
in the badge area.

If I put this line into the template editor it resolves to PM
unit_of_measure: {{ now().timestamp() | timestamp_custom("%p") }}

Please read Taras’s previous post.

I noticed that in the previous post.
Is their any way to resolve it to a string and then use it in the unit_of_measurement?

No, you can not template the unit.

Don’t know if conditional badges is an option.
But that could be an option if that even works.
So you create two sensors, one with AM and one with PM and you display the correct one in Lovelace

How is this an issue? How do you not know whether it is am or pm?

1 Like

You appear to have missed this part of what I wrote:

Yes I read it.
Just trying to figure out a way to resolve the template to a string and then use it as the
unit_of_mesurement:

If you are trying to do that then you didn’t understand my explanation. You cannot use a template for unit_of_measurement.

Whatever you enter to the right of unit_of_measurement: will be used literally. It will not be processed by the Jinja2 template interpreter.

I get what you are saying.
What is confusing me is if I put this in the Jinja template editor
unit_of_measure: {{ now().timestamp() | timestamp_custom("%p") }}
It shows this in the window to the right which seams to me that it processed it
unit_of_measure: AM
I am not a programmer just fumbling thru a way to make it work.