Date formatting

I’m confused, it’s doing exactly what you are telling it. its displaying the word ‘day’ with a space, then the weeday with a space then the month… whats not working?

Hmm I thought I would get the day but now I understood that I should put another variable. I tried something like {{now (). Day () + ‘de’ + months [now (). Month-1] + ’ ]}} to get “DAY de MONTH de YEAR” but its not working.

If you could help me I’d be grateful.

So do you want numerical days, word month, and numerical year?

Exactly :rofl:

platform: template
sensors:
  date_long:
    friendly_name: ‘Dia e Hora’
    value_template: >
      {% set months = ["Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"] %}
      {% set days = ["Segunda-Feira", "Terça-Feira", "Quarta-Feira", "Quinta-Feira", "Sexta-Feira", "Sábado", "Domingo"] %}
      {{ days[now().weekday()] + ' ' + months[now().month-1] + ' ' + now().day + ', ' now().year }}
1 Like

Thank you very much for the help but it did not work. So I did some testing on the dev template and got the following syntax:

{{ days[now().weekday()] }}, {{ now().day }} de {{ months[now().month-1] }} de {{ now().year }}

I do not know if this is the best way but it was the only one that worked.

Thanks again for your help.

A bit late but here’s my favorite:

"{{ as_timestamp(states.calendar.richard.attributes.end_time) | timestamp_custom('%a, %b %d at %r') }}"

It will return a text like this: Tue. Oct 16 at 11:30:00 AM
(an upper case a will give you full name of day, upper case b will give you full name of month)

2 Likes

Yeah, he wanted it in his language though…

The script I use is unix based. So the question is, on hass.io, when you select the country, does it also change the lower level unix/linux country code?

I don’t know to be honest. It uses pythons datetime library. I always though you needed to set the location (timezone and language) for that to work.

Guys help to solve a question at such code:

   - platform: template
      sensors:
        date_long: 
          friendly_name: 'Datum'
          value_template: >
            {% set months = ["Ene", "Feb", "Mar", "Abr", "May", "Jun", "Jul", "Ago", "Sep", "Oct", "Nov", "Dic"] %}
            {% set month = months[now().strftime('%m') | int -1] %}  
            {{ now().strftime('%d') + ' ' + month + ' '+ now().strftime('%Y') }}

number does not change. Like plain text, 09 Nov 2018 (the sensor does not change the long date of the month)

I have noticed this too in the past few days. My config looks like this:

    value_template: >
      {% set days = ["måndag", "tisdag", "onsdag", "torsdag", "fredag", "lördag", "söndag"] %}
      {% set weekday = days[now().weekday()] %}
      {{ weekday }}

and

    value_template: >
      {% set months = ["jan", "feb", "mar", "apr", "maj", "jun", "jul", "aug", "sep", "okt", "nov", "dec"] %}
      {% set month = months[now().month -1] %}
      {{ now().day | string + ' ' + month + ' '+ now().year | string }}

None of the above update automatically anymore, but they update after a HA restart.

There was a change in template_sensor.

Template sensors will no longer auto update if we can’t find relevant entities in the template. 
You’ll want to review your template sensors and consider adding relevant `entity_id` entries or 
use the new `homeassistant.update_entity` service.

Add entity_id to your template sensor.

      entity_id: sun.sun
      value_template: .....

We’ll check it out today.

@dionisgr
You’ll need to add an entity_id that updates once per day. Preferably at midnight, if you implement sun.sun it will only update when sun.sun updates which may not be at midnight. You can implement the datetime sensor and then link that in your templates:

sensor:
  - platform: time_date
    display_options:
      - 'time'
      - 'date'
  - platform: template
    sensors:
      date_with_correct_language:
        entity_id: sensor.date
        value_template: >
          {{ ["måndag", "tisdag", "onsdag", "torsdag", "fredag", "lördag", "söndag"][now().weekday()] }}

EDIT: Actually, you can use sun.sun. That updates once per minute. So it should update at midnight. Forgot about that bit.

And as in this template to make the month words and dd-mm-yyyy

Well the sensor.date should already have that if you implement it.

Hello i would like to use your Code for the date in French but i don’t know how to inclue the day:

date_fr:
  entity_id: sensor.date
  friendly_name: "Date du jour"      
  value_template: >
    {% set months = ["Janvier", "Fevrier", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Decembre"] %}
    {% set days = ["Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi", "Dimanche"]  %}
    {{ days[now().weekday()] + ' ' + months[now().month-1] }}

When i use the coming code to add the day it doesn’t work:

date_day_fr:
  entity_id: sensor.date
  friendly_name: "Mois"      
  value_template: >
    {% set months = ["Jan", "Fev", "Mar", "Avr", "Mai", "Juin", "Juil", "Août", "Sep", "Oct", "Nov", "Dec"] %}
    {% set days = ["Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi", "Dimanche"]  %}
    {{ days[now().weekday()] + ' ' + now().day + ' ' + months[now().month-1] }}      

I don’t understand why, do you have an idea ?

Thanks.

now().day is a integer, so you can’t add it to a string. Use

now().day | string
1 Like

Thank you so much petro it works.