Convert 24H Time to 12H?

I have a sensor with timestamp as the state (24 hour format, i.e. 22:30). I want to convert this to 12 hour format (i.e. 10:30 PM) for easily readable notifications.

Any suggestions? Sorry for such a basic question.

subtract 12 if it’s past 12:59

Sorry, I should’ve specified a bit more, I know I can do it the manual way with math and conditional statements for AM/PM. I was hoping there’s a more simpler function though.

Yeah it would be nice if that were a built in feature huh?

Since time triggers and comparissons are based on a 24h format, using a12h am/pm format would be just for displaying purposes. This is where the template-senors jumps in.

sensor:
  - platform: template
    value_template: {{ as_timestamp(states('real_sensor.entity_id')) | timestamp_custom('%I:%M %p') }}

Hope that helps.

1 Like

This will help in understanding the formatting: http://strftime.org.

1 Like

I’m having the same issue.

I get this error message in the config validator
Error loading /config/configuration.yaml: while scanning a plain scalar in "/config/sensors.yaml", line 15, column 24 found unexpected ':' in "/config/sensors.yaml", line 15, column 82 Please check http://pyyaml.org/wiki/YAMLColonInFlowContext for details.

When I input this code:
- platform: template sensors: time: friendly_name: Time value_template: {{ as_timestamp(states('sensor.time')) | timestamp_custom('%H:%M') }}

What am I doing wrong?