Date formatting

I already have this

  life360_name_since:
    friendly_name: "Naam's Locatie Sinds"
    value_template: >
    {%- set wdays = ["maandag", "dinsdag", "woensdag", "donderdag", "vrijdag", "zaterdag", "zondag"]  %}
    {{ as_timestamp(state_attr('device_tracker.name', 'at_loc_since')) | timestamp_custom(wdays[now().weekday()] + (' om %H:%M')) }}

But this gives me not the correct day value and a headache what do I miss/wrong?

Maybe I’m too stupid but I appreciate any help.
I really need to learn templating please point me the right way.

Fixed with help on a Dutch channel.

{%- set tracker_timestamp = as_timestamp(state_attr('device_tracker.name', 'at_loc_since')) %}
{%- set wdays = ["zondag", "maandag", "dinsdag", "woensdag", "donderdag", "vrijdag", "zaterdag"] %}
{%- set wday =  tracker_timestamp | timestamp_custom('%w') | int %}
{{ tracker_timestamp | timestamp_custom(wdays[wday] + ' om %H:%M') }}

Hi Alwin, is this also possible in combination with strftime?

I have this:

  - platform: afvalinfo
    resources:
      - pbd
      - gft
      - restafval
      - papier
      - trash_type_today
    city: !secret CTY
    postcode: !secret PSC
    streetnumber: !secret NMB
    dateformat: '%a %d %B'
    timespanindays: 365

afval
I really like to change the %a (weekday) from english to dutch. Can you help me with this?

strftime uses pythons set language which will always be english. You’ll have to take those values and pass them through a template sensor. For that, it would probably be best to take your current sensors and change them all to numerical values, then you can you strptime to convert them into your values.

  - platform: afvalinfo
    resources:
      - pbd
      - gft
      - restafval
      - papier
      - trash_type_today
    city: !secret CTY
    postcode: !secret PSC
    streetnumber: !secret NMB
    dateformat: '%d-%m-%Y'
    timespanindays: 365
sensor:
- platform: template
  sensors:
    paper_pickup_day:
      value_template: >
        {% set current = strptime(states('sensor.papier'), '%d-%m-%Y') %}
        {%- set days = ["zondag", "maandag", "dinsdag", "woensdag", "donderdag", "vrijdag", "zaterdag"] %}
        {{ current | timestamp_custom(days[current.weekday()] + ' %d %B') }}
1 Like

Hi guys,

i´m actually stealing a theme :slight_smile: and this has a cool Markdown card which shows the current date. My goal is to have it in the following format (in german :slight_smile: )

“Mittwoch, der 28. Mai”

I have no idea about templating and where to put stuff so maybe one of you could help me on this. THis is how the code currently looks:

- type: markdown
        content: |
          {% if now().month in (1,) %}
          ## {{ now().day }} Januar
          {% elif now().month in (2,) %}
          ## {{ now().day }} Februar 
          {% elif now().month in (3,) %}
          ## {{ now().day }} März 
          {% elif now().month in (4,) %}
          ## {{ now().day }} April 
          {% elif now().month in (5,) %}
          ## {{ now().day }} Mai 
          {% elif now().month in (6,) %}
          ## {{ now().day }} Juni 
          {% elif now().month in (7,) %}
          ## {{ now().day }} Juli 
          {% elif now().month in (8,) %}
          ## {{ now().day }} August 
          {% elif now().month in (9,) %}
          ## {{ now().day }} September
          {% elif now().month in (10,) %}
          ## {{ now().day }} Oktober 
          {% elif now().month in (11,) %}
          ## {{ now().day }} November 
          {% elif now().month in (12,) %}
          ## {{ now().day }} December 
          {% endif %}
        style: |
          ha-card {
             margin: 12px 14px -5px 14px;
             box-shadow: none;
             --paper-card-background-color: rgba(0, 0, 0, 0.0)
           }
           h2 {
             font-size: 18px;
             font-weight: 400;
             padding-left: 10px;
             border-left: 3px solid rgba(81, 134, 236);
           }

Thanks a lot in advance.

I read this post carefully. However, I do not get my problem solved. In lovelace.ui the timestamp is displayed as an invalid date:

invalid_date

The date is displayed correctly in the template engine:
Template engine Result type: string

  • platform: template
    sensors:
    bwm_flur_last_motion:
    friendly_name: “BWM Flur Letzte Bewegung”
    device_class: timestamp
    value_template: “18.11.2020 16:07”

Template Code:

- platform: template
  sensors:
    bwm_flur_last_motion:
      friendly_name: "BWM Flur Letzte Bewegung"
      device_class: timestamp
      value_template: "{{ as_timestamp(state_attr('automation.bwm_flur', 'last_triggered')) | timestamp_custom('%d.%m.%Y %H:%M') }}"

There must be something wrong with the timestamp_custom. Does anyone have a solution here?

Use this instead

"{{ as_timestamp(state_attr('automation.bwm_flur', 'last_triggered')) | timestamp_custom('%Y.%m.%dT%H:%M:%S') }}"

But you can also be super lazy and just output the last_triggered and it should work

"{{ state_attr('automation.bwm_flur', 'last_triggered') }}"
1 Like

Sorry, but
the first example does not fix the problem. The message “Invalid date” remains.
The second example gives me not the required format. Last seen info, i.e. 45 minutes, instead of the fixed datum: %d.%m.%Y %H:%M

Hello all,

call me stup** but I dont know, how to change date format from YYYY-mm-dd to dd-mm-YYYY?

I`m using simple time_date sensor platform:

sensor:
  - platform: time_date
    display_options:
      - 'time'
      - 'date'
      - 'date_time'
      - 'date_time_utc'
      - 'date_time_iso'
      - 'time_date'
      - 'time_utc'
      - 'beat'

12

Many thanks for help!

1 Like

Hey! Add this to your sensors.yaml file (I suggest you create one if you don´t use it, it cleans up your configuration.yaml file significantly!):

# Day, month and YEAR, Norwegian format
- platform: template
  sensors:
    date_full_norwegian_format:
      friendly_name: "Full date formatted for Norwegian locale"
      value_template: "{{ as_timestamp(states('sensor.date_time_iso')) | timestamp_custom('%d %m %Y ') }}"
      icon_template: mdi:calendar-blank-outline
1 Like

I want to output a sensor value in a different formatting for a notification. I get under sensor.washer_remaining_program_time a value like 2022-09-07T12:48:33+00:00 and I don`t manage to extract the time. So for example 12:48.
Is this possible or do I have to create an extra sensor for this?

I have a Mushroom Title card, and I’m using:

secondary: Next {{states(‘sensor.jewish_calendar_upcoming_shabbat_candle_lighting’)}}

How do I change the format inline?
Thanks

You can use a template (or put it into a helper):

{{ as_timestamp(states("sensor.washer_remaining_program_time")) | timestamp_custom("%H:%M") }}