Date formatting


#41

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?


#42

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.


#43

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


#44

Exactly :rofl:


#45
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 }}

#46

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.


#47

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)


#48

Yeah, he wanted it in his language though…


#49

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?


#50

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.


#51

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)


#52

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.


#53

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: .....

#54

We’ll check it out today.


#55

@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.


#56

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


#57

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


#58

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.


#59

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

now().day | string

#60

Thank you so much petro it works.