Converting Time Template Help

Im trying to take the time from from my calander that looks like start_time: 2017-11-25 22:00:00 and make that a readable format for tts and other automations. I would like it read 12 hour format just hour and min i dont care about the date.

Ive tried a few things that I found around here but nothing seems to work. Can someone point me in the right direction?

This sensor grabs the start time in the format I mentioned above:
cal_start_time:
value_template: ‘{{ states.calendar.dustin_arbogast.attributes.start_time }}’
friendly_name: ‘Next Meeting Start Time’

Couple examples of what I have tried…
{{ states.calendar.dustin_arbogast.attributes.start_time.strptime("%H:%M") }}

‘{{ states.calendar.dustin_arbogast.attributes.start_time.strftime("%H:%M") }}’

{{ as_timestamp(calendar.dustin_arbogast.attributes.start_time ) | timestamp_custom("%H:%M")}
1 Like

I’m having the same issue but I’m using the snippet you’ve provided and I get an error message of:
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.

Snippet
- platform: template sensor: friendly_name: Time value_template: {{ as_timestamp(states('sensor.time')) | timestamp_custom('%H:%M') }}

- platform: template 
    sensors: #<---- needed s
      my_time: #<---------MISSING
        friendly_name: Time
        value_template: "{{ as_timestamp(states('sensor.time')) | timestamp_custom('%H:%M') }}"
                        ^                                                                     ^
                        |                                                                     |
               MISSING OUTSIDE QUOTES                                              MISSING OUTSIDE QUOTES

There were a number of errors.

  1. sensor should have been sensors.
  2. You were missing the entity_id of the sensor. You went straight into the sensor without defining it.
  3. Also, when you are templating, templates need to be in quotes. Or you need to use multi-line templating (the carrot symbol).

I couldn’t tell if 3 was actually a problem because the code wasn’t formatted properly.

Thanks. I’ll give it a go once I get my instance back up and running yet again for the nth time, smh

So after inputting:

  - platform: template
sensors:
  my_time:
    friendly_name: Time
    value_template: "{{ as_timestamp(states('sensor.time')) | timestamp_custom('%H:%M') }}"

I get an empty state. What is wrong now?

what happens if you put this in the template dev tool:

{{states('sensor.time')}}

I’m guessing you don’t have sensor.time implemented because sensor.time outputs in %H:%M format as is.

Here’s the component that makes sensor.time. You shouldn’t even need your template. I probably should have said that before but I wasn’t thinking straight.

# Example configuration.yaml entry
sensor:
  - platform: time_date
    display_options:
      - 'time'

Also, if for some reason you do have sensor.time implemented and you still want to make this template:

  - platform: template
    sensors:
      my_time:
        friendly_name: Time
        entity_id: sensor.time
        value_template: "{{ as_timestamp(now()) | timestamp_custom('%H:%M') }}"

If you don’t want to implement sensor.time, this will also work:

  - platform: template
    sensors:
      my_time:
        friendly_name: Time
        entity_id: sun.sun
        value_template: "{{ as_timestamp(now()) | timestamp_custom('%H:%M') }}"

Still not working for me. F it. I give up. Should’ve figured it’d be a pita to do such a simple thing. Oh well.

did you copy and paste the template I provided? Post your yaml.

This really isn’t hard. Fundimentally, there has to be something you are doing wrong. Looking at what you are trying to do, all you really need is this code to get the time.

# Example configuration.yaml entry
sensor:
  - platform: time_date
    display_options:
      - 'time'

It should be in the sensor section along with other platforms.

If you want 12 hour format:

%I:%M %p (ex. 06:30 AM)
%-I:%M %p (ex. 6:30 AM)

This worked! Thanks!

1 Like

For future reference, this is what successfully worked thanks to @petro & @walrus_parka

  • platform: template
    sensors:
    my_time:
    friendly_name: Time
    entity_id: sensor.time
    value_template: “{{ as_timestamp(now()) | timestamp_custom(’%-I:%M %p’) }}”