Changing time format

Good afternoon,

I am using GitHub - KoljaWindeler/ics: Integration that displays the next event of an ics link (support reoccuring events) intergration

I want my google mini to say the time and date of the appointment but if i do this:

{{ state_attr(‘sensor.ics_1’, ‘start’ ) }} i get: 2022-07-12 15:45:00+02:00

Hope someone can help me with this.

i want the following “15 uur 45” and “12-Juli” < dutch (English: “15 hour 45 minutes” “12-July”)

Plug this into Developer Tools / Templates and have a play:

{{ state_attr('sensor.ics_1', 'start' ) }}
{{ state_attr('sensor.ics_1', 'start' )|as_timestamp|timestamp_custom('%H uur %M', local=True, default=0) }}
{{ state_attr('sensor.ics_1', 'start' )|as_timestamp|timestamp_custom('%d-%B', local=True, default=0) }}

References: Templating - Home Assistant and datetime — Basic date and time types — Python 3.8.13 documentation.

Please format code snippets correctly in posts — if the forum software thinks it’s text, it substitutes “smart quotes” which mess up templates.

You can also use strftime() instead of using a timestamp conversion.

{{ (state_attr('sensor.ics_1', 'start')|as_datetime).strftime("%H uur %M") }}
{{ (state_attr('sensor.ics_1', 'start')|as_datetime).strftime("%-d-%B") }}

@Troon

Perfect works great thank you! Going to learn Python one of these days because its so cool!

With:
{{ state_attr('sensor.ics_1', 'start' )|as_timestamp|timestamp_custom('%d-%B', local=True, default=0) }}

I get 12-July i want July (and other months) translated to dutch e.q. Julie
I was searching the net but my attempts with examples given i could not find the solution.

{% set m = {
    1: 'Januari',
    2: 'Februari',
    3: 'Maart',
    4: 'April',
    5: 'Mei',
    6: 'Juni',
    7: 'Juli',
    8: 'Augustus',
    9: 'September',
    10: 'Oktober',
    11: 'November',
    12: 'December'
} %}
{% set dt = state_attr('sensor.ics_1', 'start') | as_datetime %}
{{ dt.hour }} uur {{ dt.minute }}
{{ dt.day }}-{{ m.get(dt.month, 'Unknown') }}

I copied the Dutch month names from an online source; you may need to correct any misspellings.

Here’s a screenshot of my test result:

Oh yes: the locale setting doesn’t do the translation for you, does it? That seems like a shortcoming in the system…

Thanks but if i run your code i get:
TypeError: float() argument must be a string or a number, not ‘datetime.datetime’

{% set m = {
    1: 'Januari',
    2: 'Februari',
    3: 'Maart',
    4: 'April',
    5: 'Mei',
    6: 'Juni',
    7: 'Juli',
    8: 'Augustus',
    9: 'September',
    10: 'Oktober',
    11: 'November',
    12: 'December'
} %}
{% set dt = state_attr('sensor.ics_1', 'start') | as_datetime %}
{{ dt.hour }} uur {{ dt.minute }}
{{ dt.day }}-{{ m.get(dt.month, 'Unknown') }}

With this it works perfect. i dont have allot of knowledge about python but i think

 state_attr('sensor.ics_1', 'start')

Must be in {{ }} of “” “” or ‘’ i tried some combinations but no luck…

{% set m = {
    1: 'Januari',
    2: 'Februari',
    3: 'Maart',
    4: 'April',
    5: 'Mei',
    6: 'Juni',
    7: 'Juli',
    8: 'Augustus',
    9: 'September',
    10: 'Oktober',
    11: 'November',
    12: 'December'
} %}
{% set dt = '2022-07-12 15:45:00+02:00' | as_datetime %}
{{ dt.hour }} uur {{ dt.minute }}
{{ dt.day }}-{{ m.get(dt.month, 'Unknown') }}

Not sure why you received that error.

As an experiment, copy-paste the following into the Template Editor and tell me what it reports:

{{ state_attr('sensor.ics_1', 'start') }}
{{ state_attr('sensor.ics_1', 'start') is string }}

output is: (new date because of new calander item)
2022-07-09 08:00:00+02:00
False

i olso tried float and boolean but they where olso false

According to the results of the test, the start attribute’s value is not considered to be a string. In other words, it’s considered to be a datetime object so there’s no need to use the as_datetime filter to convert it from string to datetime.

Try this version in the Template Editor:

{% set m = {
    1: 'Januari',
    2: 'Februari',
    3: 'Maart',
    4: 'April',
    5: 'Mei',
    6: 'Juni',
    7: 'Juli',
    8: 'Augustus',
    9: 'September',
    10: 'Oktober',
    11: 'November',
    12: 'December'
} %}
{% set dt = state_attr('sensor.ics_1', 'start') %}
{{ dt.hour }} uur {{ dt.minute }}
{{ dt.day }}-{{ m.get(dt.month, 'Unknown') }}