Convert date and time template

i try this:

net_uptime:
      value_template: "{{ as_timestamp(strptime( states('sensor.fritzbox_connection_uptime'), '%Y-%m-%d %H:%M:%S')) | timestamp_custom('%Y-%m-%d %H:%M:%S') }}"
      unit_of_measurement: "Tage"
      friendly_name: "Verbunden"
      icon_template: mdi:calendar

For a other Day-Format. But now i search a “Counter”

here my solution:

net_uptime_pretty:
      friendly_name: Verbunden
      value_template: >
        {%- set up_time = as_timestamp(now())-as_timestamp(states('sensor.fritzbox_connection_uptime')) %}

        {% if states('sensor.last_boot') == '0.0' %}
          Soeben neu gestartet...
        {% else %}

        {%- macro phrase(name, divisor, mod=None) %}
          {%- set value = ((up_time // divisor) % (mod if mod else divisor)) | int %}
          {{- '{} {}'.format(value, name) if value | int > 0 else '' }}
        {%- endmacro %}

        {%- set values = [
          phrase('W.', 60*60*24*7),
          phrase('T.', 60*60*24, 7),
          phrase('Std.', 60*60, 24),
          phrase('Min.', 60),
          phrase('Sek.', 1, 60)
        ] | select('!=','') | list %}

        {{ values[:-1] | join(', ') ~ ' ' ~ values[-1] if values | length > 1 else values | first }}
        {% endif %}
1 Like

i tested the code that you posted, the list says when i use for example %A for the weekday he takes my selected language in HA, but he always make the weekday english, my language is german.

Please read the full thread. It has a solution to your problem.

okay, but i had problems to undestand how to add this to my code, perhaps you can give me a hint?

{{as_timestamp(strptime(state_attr('calendar.famillien_termine', 'start_time'), '%Y-%m-%d %H:%M:%S')) | timestamp_custom("%A %d.%b %-H:%M")}}

Sir, it’s literally posted above. Please read the thread. That requires you to read my post, then read the responses IN THIS THREAD.

Here’s a hint, this post has the code:

Yeah sir,

i understand it but i use it different i dont want to use the now().timestamp() function.

what i understand is

{{as_timestamp(strptime(state_attr('calendar.famillien_termine', 'start_time'), '%Y-%m-%d %H:%M:%S')) | timestamp_custom("%w")}}

this gives me the day as number, i think this is needed for the days variable. (its one day to much but i think i can adjust it with -1 because he counts from 0)

{% set days = ['Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag', 'Sonntag'] %}
{{ as_timestamp(strptime(state_attr('calendar.famillien_termine', 'start_time'), '%Y-%m-%d %H:%M:%S')) | timestamp_custom(days[now().weekday()-1] ~ ' %d.%b %-H:%M') }}

now i must replace the now().weekday()-1 with my value from the entity.
i tried it with this

{% set days = ['Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag', 'Sonntag'] %}
{{ as_timestamp(strptime(state_attr('calendar.famillien_termine', 'start_time'), '%Y-%m-%d %H:%M:%S')) | timestamp_custom(days[as_timestamp(strptime(state_attr('calendar.famillien_termine', 'start_time'), '%Y-%m-%d %H:%M:%S')) | timestamp_custom("%w")] ~ ' %d.%b %-H:%M') }}

but this gives me a error.

UndefinedError: 'list object' has no attribute '6'

strptime(state_attr('calendar.famillien_termine', 'start_time'), '%Y-%m-%d %H:%M:%S')) | timestamp_custom("%w") | int

1 Like

thank you, that was the hint that helps me, here is the code

{{ as_timestamp(strptime(state_attr('calendar.famillien_termine', 'start_time'), '%Y-%m-%d %H:%M:%S')) | timestamp_custom(days[as_timestamp(strptime(state_attr('calendar.famillien_termine', 'start_time'), '%Y-%m-%d %H:%M:%S')) | timestamp_custom("%w") | int -1] ~ ' %d.%b %-H:%M') }}

Oh oh, if you get “Sir’ed” by Petro, you should try a lot harder… :rofl: :rofl: :rofl:

EDIT: sorry, couldn’t resist :rofl:

2 Likes

I tried my best knowledge, I don’t want to let anyone do the work for me here, or annoy me. but I needed the missing hint, what I just don’t know I can’t conjure up. that’s how you learn. I think that’s what the forum is for. nevertheless thank you very much

Hello,
I tried to get rid of unnecessary date/time information of one of my sensors.
This sensor


creates a timestamp and I want to customize this time/date in only hour:minute day-month.

To achieve this I created the following sensor:

buro_time_temp_change:
        unique_id: buro_time_temp_change
        friendly_name: "Büro Time Temp change"
        value_template: "{{ as_timestamp(states(sensor.buro_next_scheduled_change_time)) | timestamp_custom('%h:%m') }}"
        device_class: timestamp

Although I found several varieties of this “value_template…” but after a positive configuration check the new sensor is always unavailable.

Can somebody please help me out?

Wrap the sensor’s entity_id in quotes (without the quotes it’s handled as the name of an undefined variable) and remove device_class: timestamp (a timestamp sensor’s value must contain the date and time and this template is only providing the time).

buro_time_temp_change:
  unique_id: buro_time_temp_change
  friendly_name: "Büro Time Temp change"
  value_template: "{{ as_timestamp(states('sensor.buro_next_scheduled_change_time')) | timestamp_custom('%h:%m') }}"

Great, thanks a lot.
Works as I wanted.

Hi all,

I would like to use the format-tag for weekkday (%A) but HA shows only the en_US names and not the de_DE names. The UI is configured in gernman and I do not understand, why the weekday names are in english.

I am using the VM from HA and the OS is:

cat /etc/*release
3.16.1
NAME="Alpine Linux"
ID=alpine
VERSION_ID=3.16.1
PRETTY_NAME="Alpine Linux v3.16"
HOME_URL="https://alpinelinux.org/"
BUG_REPORT_URL="https://gitlab.alpinelinux.org/alpine/aports/-/issues"

I cannot find any command to check the locales on the OS. Can anyone tell me how to switch to de_DE?

Thanks,
Spartacus

Home Assistant’s templating language is English and doesn’t support the operating system’s locale. That means strftime produce day and month names exclusively in English.

You’ll have to perform the translation yourself. For example:

['Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag', 'Sonntag'][now().weekday()] }}
1 Like

Hi,

ok, thanks, its a little bit strange, that this is not included, but its ok.

I don’t know if this is the right thread to ask this question, but I am having seemingly a timezone issue with timedelta calculation.
I am trying to calculate the remaining printing time by calculating the time delta between now and the

{% set done = as_timestamp(states("sensor.octoprint_estimated_finish_time")) %}
{% set current = now().timestamp() %}
{% set delta = done - current %}
Estimated finishing point is {{ done | timestamp_custom("%H:%M:%S")}} # 15:09:00
Current time is {{ now().timestamp() | timestamp_custom("%H:%M:%S") }} # 15:08:00
The delta between them is {{ delta | timestamp_custom("%H:%M:%S")}} # # 01:01:00

I can see, that the timezone of my current time and the sensor are correct (UTC+1).
But how do I get the formatter to use the UTC-timezone now, since it only describes the timedelta and no timezone?

Use datetimes instead of timestamps and you won’t have this confusion or issue.

{% set done = states("sensor.octoprint_estimated_finish_time") | as_datetime | as_local %}
{{ done - now() }}

It will most likely have microseconds. If you don’t want that…

{% set done = states("sensor.octoprint_estimated_finish_time") | as_datetime | as_local %}
{{ done - now().replace(microsecond=0) }}
2 Likes

I have also : “-1 day, 23:47:00” and need your help to make this example just “13 minutes”.

Thank You.