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.
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.
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.
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 ?).
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
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.