Format Sensor data using strptime?

Hello - Hoping for some help. I’m trying to reformat a sensor attribute to better display on a card.

Sensor shows:

language: en
category: Met
event: wind gusts
responseType: Prepare
urgency: Immediate
severity: Minor
certainty: Likely
effective: 2024-06-10T09:04:00+02:00
onset: 2024-06-10T14:00:00+02:00
expires: 2024-06-10T21:00:00+02:00

Trying to reformat the onset from above to:

At time: 14:00

{% set begin_time = strptime(state_attr(‘binary_sensor.kusel’, ‘onset’), ‘%Y-%m-%dT%H:%m:%s+%z’) %}

ValueError: Template error: strptime got invalid input ‘2024-06-10T14:00:00+02:00’ when rendering template ‘{% set begin_time = strptime(state_attr(‘binary_sensor.kusel’, ‘onset’), ‘%Y-%m-%dT%H:%m:%s+%z’) %} {{begin_time}}’ but no default was specified

Clearly I’m missing something.

This is the correct pattern to use with strptime in your template:

'%Y-%m-%dT%H:%M:%S%z'

You can also do this:

{% set begin_time = state_attr('binary_sensor.kusel', 'onset') | as_datetime %}
1 Like

Okay, but how do I get the ‘00’ in the minutes to format correctly?

{% set begin_time = state_attr('binary_sensor.kusel', 'onset') | as_datetime %}
{% set end_time = state_attr('binary_sensor.kusel', 'expires') | as_datetime %} 
Begins: {{ begin_time.hour }}:{{ begin_time.minute }}
Ends: {{ end_time.hour }}:{{ end_time.minute }}

Result type: string

Begins: 14:0
Ends: 21:0

If all you want to do is display the time portion of the datetime string, you can simply “slice” it.

{{ state_attr('binary_sensor.kusel', 'onset')[11:16] }}

Very nice!

If slicing doesn’t work in your application, here’s how to zero-pad a two-digit number:

{% set now_time = now() %}
Now: {{ "%02d:%02d" % (now_time.hour, now_time.minute) }}

If you really want to first convert the datetime string to a datetime object and then display the time as a string, you can use strftime.

{% set begin_time = state_attr('binary_sensor.kusel', 'onset') | as_datetime %}
{% set end_time = state_attr('binary_sensor.kusel', 'expires') | as_datetime %} 
Begins: {{ begin_time.strftime('%H:%M') }}
Ends: {{ end_time.strftime('%H:%M') }}

For your stated application, slicing is simpler.

1 Like

This is what my card looks like now! AWESOME!

type: conditional
conditions:
  - condition: state
    entity: binary_sensor.kusel
    state_not: 'off'
card:
  type: markdown
  content: >
    _<font>{{state_attr('binary_sensor.kusel', 'headline')}} </font>_

    Severity: **<font>{{state_attr('binary_sensor.kusel', 'severity')}}
    </font>**

    {% set begin_time = state_attr('binary_sensor.kusel', 'onset') | as_datetime
    %} {% set end_time = state_attr('binary_sensor.kusel', 'expires') |
    as_datetime %}  {% set effective = state_attr('binary_sensor.kusel',
    'effective') | as_datetime %}

    In effect from: {{ effective.strftime('%m-%d-%Y') }} until {{
    end_time.strftime('%m-%d-%Y') }}

    Begins: {{ begin_time.strftime('%H:%M') }}

    Ends: {{ end_time.strftime('%H:%M') }}


    {{state_attr('binary_sensor.kusel', 'description')}}  {% set card_title =
    (state_attr('binary_sensor.kusel', 'event') ) %}
  title: Weather Alert

Weather Alert

Official WARNING of WIND GUSTS
Severity: Minor

In effect from: 06-10-2024 until 06-10-2024
Begins: 14:00
Ends: 21:00

There is a risk of wind gusts (level 1 of 4).
Height range: > 400 m; Max. gusts: 50-60 km/h; Wind direction: south-west then west; Increased gusts: near showers and in exposed locations ~ 70 km/h

1 Like