World clock in a different format

Hi you all!

I’m trying to display both my current time and my home town.

With a worldclock it’s possible but I’d prefer a 12 hrs format.
With now it’s simple, just {{ now().strftime('%I:%M %p') }}, but with the sensor.worldclock_sensor I cannot get it to work, I’ve tried things like {{ strptime(states('sensor.worldclock_sensor'), '%I:%M %p') }}

sensor.worldclock_sensor doesn’t return a proper datetime object. If I could create one object with America/Caracas as the timezone I could format it.

Any ideas? Thanks!

strptime(states(‘sensor.worldclock_sensor’), ‘%I:%M %p’)

With strptime, you have to tell it the format of the string you’re passing in, not the format of the output you want. Looking at the code, it looks like it’s just %H:%M.

  # Convert state to ptime object
  {% set nt = strptime(states('sensor.worldclock_sensor'), "%H:%M") %}
  # Convert ptime object back to string in a new format.
  {{ nt.strftime("%I:%M %p") }}
1 Like

Beautiful

Can you share the code of your clock?

What I don’t get is how come they (in the 2 images you’ve shown) are 2 or 3 minutes out from normal time zone offsets ?
I think Venezuela is on a half hour offset, but still … ?

I assume the difference was due to the fact that for the my current time I was using now() in the lovelace-ui, and for the Venezuelan time I was using a sensor. I’ve moved both times to sensors and there’s no out of sync anymore.

sensor.yaml

  - platform: worldclock
    time_zone: America/Caracas
      
  - platform: worldclock
    time_zone: Europe/Amsterdam
      
  - platform: template
    sensors:
        venezuela_12hrs:
            value_template: >
                {% set nt = strptime(states('sensor.worldclock_sensor'), "%H:%M") %}
                {{ nt.strftime("%I:%M %p") }}
                
        amsterdam_12hrs:
            value_template: >
                {% set nt = strptime(states('sensor.worldclock_sensor_2'), "%H:%M") %}
                {{ nt.strftime("%I:%M %p") }}

I’m basing my UI in @Mattias_Persson’s awesome repo.
It’s still a work-in-progress-learning-while-doing-it but that block is looking something like:

  - badges: []
    cards:
      - elements:
          - content: |
              {{ states('sensor.greetings') }} <br><br> <font>It's</font>
            style:
              '--ha-card-background': none
              '--ha-card-box-shadow': none
              '--primary-text-color': 'rgba(0, 0, 0, 0.9)'
              font-family: SF Display
              font-size: 1.6vw
              height: 1px
              left: 11%
              letter-spacing: '-0.05vw'
              line-height: 2.05vw
              max-width: 20vw
              opacity: 0.65
              position: absolute
              top: 7%
              width: 20vw
            type: 'custom:hui-markdown-card'
          - content: |
              <h1>{{ states('sensor.amsterdam_12hrs') }}</h1>
            style:
              '--ha-card-background': none
              '--ha-card-box-shadow': none
              '--primary-text-color': 'rgba(0, 0, 0, 0.9)'
              font-family: SF Display
              font-size: 2vw
              font-weight: 100
              height: 1px
              left: 10.7%
              letter-spacing: '-0.2vw'
              line-height: 2.05vw
              max-width: 20vw
              opacity: 0.65
              position: absolute
              top: 18%
              width: 20vw
            type: 'custom:hui-markdown-card'
          - content: >
              <font color="#95a5a6"><i> {{ states('sensor.venezuela_12hrs') }}
              in Venezuela</i></font><br><br> <font>{{ now().strftime('%a, %d
              %B') }}</font>
            style:
              '--ha-card-background': none
              '--ha-card-box-shadow': none
              '--primary-text-color': 'rgba(0, 0, 0, 0.9)'
              font-family: SF Display
              font-size: 1.5vw
              height: 1px
              left: 11%
              letter-spacing: 0.06vw
              line-height: 2.05vw
              opacity: 0.65
              position: absolute
              top: 23%
              width: 20vw
            type: 'custom:hui-markdown-card'

And the sensors:

      
  - platform: worldclock
    time_zone: America/Caracas
      
  - platform: worldclock
    time_zone: Europe/Amsterdam
      
  - platform: template
    sensors:
        venezuela_12hrs:
            value_template: >
                {% set nt = strptime(states('sensor.worldclock_sensor'), "%H:%M") %}
                {{ nt.strftime("%I:%M %p") }}
                
        amsterdam_12hrs:
            value_template: >
                {% set nt = strptime(states('sensor.worldclock_sensor_2'), "%H:%M") %}
                {{ nt.strftime("%I:%M %p") }}
                
        greetings:
            value_template: >
                {% if now().hour < 5 %} <b>Good night {{'\U0001F634'}}</b> {% elif
                now().hour < 12 %} <b>Good morning</b> {{'\u2615\uFE0F'}} {% elif
                now().hour < 18 %} <b>Good afternoon</b>
                {{'\U0001F44B\U0001F3FB'}} {% else %} <b>Good evening</b>
                {{'\U0001F44B\U0001F3FB'}} {% endif %}
1 Like

Hi,
I’m a new in HA.
But I like your clock card a lot, I’s ve tried several times, but i failes to get it done.
Can you tel me where toe put te code?
I wan t to show (in my case) only the Europe.Amsterdam time zone.
Do you have a kind of step by step installation manual for me??
All help is welcome.

Hello, did you find a solution?

image

hello, it doesn’t work even though I did the installations. Can you explain the steps to me?