Templating Primer?

I apologize upfront for the newbie questions but I’ve scoured the documentation and I’m still a bit lost.

I’ve added the time_date sensor to my config but now I want to convert the time date format to US. I see that templating is the way to go but I’m absolutely lost on where and how to create this template. I’m not afraid of coding things w/ the appropriate examples, but where do I put the code? How do I replace the native display options with that code?

Is there any good step by step primers on how to do this or something similar that I can study and use as a guide to get started? I’ve found some code snippets but not knowing where to put them or how to interact with them makes the code useless to me.

Any help would be appreciated.

Thanks, Rob

1 Like

Basically you need to create a new entity using the Template Sensor integration. In its value_template parameter you enter your template that converts the state of sensor.time_date to whatever format you want. E.g., you might do:

sensor:
- platform: template
  sensors:
    my_time_date:
      value_template: >
        {{ strptime(states('sensor.time_date'), '%H:%M, %Y-%m-%d')
           .strftime('%B %d, %Y, %I:%M %p') }}

Use the Template Editor on the TEMPLATE tab of the Developer Tools page to work out your template. There are links at the top of that page to the templating documentation.

Just to prove the adage that there’s more than one way to get the job done in Home Assistant, here’s a variation:

sensor:
  - platform: template
    sensors:
      us_format:
        entity_id: sensor.time_date
        value_template: "{{ now().timestamp() | timestamp_custom('%m/%d/%Y %I:%M %p') }}"

Example in Template Editor:

If you don’t want the month and day to be zero-padded, add a negative sign like this:

        value_template: "{{ now().timestamp() | timestamp_custom('%-m/%-d/%Y %I:%M %p') }}"
1 Like

Thanks. I always forget about the minus sign to prevent zero padding! :slight_smile:

Thanks a lot guys… That is a HUGE help in how this works.