Display 12h time sensor as 24h

Hi team,

I’ve tried searching through the forums but I’m not sure what I’m doing wrong.

I have a sensor which outputs time in the 12 hour clock i.e. 08:00 pm is displayed as:

08:00

I would like to display this as:

20:00

I have tried using the following template but it’s still displaying the data in the 12 hour clock.

  • platform: template
    sensors:
    shop:
    friendly_name: ‘shop’
    value_template: ‘{{ states.sensor.json_time.attributes[“shop”] | timestamp_custom(“%H:%M %p”) }}’

timestamp_custom expects to receive a timestamp. Your template is supplying it with a string like your example 08:00 which is not a timestamp. As a consequence, timestamp_custom just produces the string it received.

BTW, if the shop attribute reports 08:00, how does one know if that’s AM or PM?

Ah ha. That makes sense. I almost feel silly now reading your reply!

Well I have 5 shop sensors. The first sensor will always be AM. The remaining 4 sensors will PM.

Is there a way I can convert that string to 24 clock? Can I add

You can use the Template Editor to test templates. Go to Developer Tools > Templates and paste this into the Template Editor:

{% set t = state_attr('sensor.json_time', 'shop') %}
{{ (t[:2] | int + 12) ~ t[-3:] }}

It should convert the shop attribute from 12h time to 24h.

For example, if it’s 08:15, the template begins by slicing the first two characters (08), converting it to an integer (8) then adding 12 to produce 20. It slices the last three characters (:15) and concatenates it with the first value to produce 20:15.

NOTE
This template is imperfect. It converts 12:00 to 24:00 instead of 00:00. If that’s a problem, you’ll need to enhance the template to properly handle it.

1 Like

Thank you so much for this. When I place the code in the developer tool it correctly shows the 24 hour clock! :slight_smile: I’m extremely happy.

However when I paste the following into my configuration file, it gives an invalid configuration:

  • platform: template
    sensors:
    shop:
    friendly_name: ‘shop’
    value_template: {% set t = state_attr(‘sensor.json_time’, ‘shop’) %} {{ (t[:2] | int + 12) ~ t[-3:] }}

When posting code in the community forum, you must format it correctly. Indenting is important to the code’s proper operation and if you post it with no formatting, all the indenting is wrecked.

There are two ways you can format the code:

Select all the code and then click the </> icon in the editor’s menu-bar.

OR

Type three consecutive backquotes ``` on a separate line before your code.
Type three more backquotes on a separate line after your code.

Here’s how to fix your template:

  - platform: template
    sensors:
      shop:
        friendly_name: 'shop'
        value_template: >
          {% set t = state_attr('sensor.json_time', 'shop') %}
          {{ (t[:2] | int + 12) ~ t[-3:] }}
1 Like

Thank you so much yet again. I’ve been using the wrong quotation for the code. I will do this next time.

You rock!!!

1 Like