Date formatting

I stumbled upon this thread while looking for a solution to a similar problem. What I’ve discovered is that the “-” notation in Jinja2 for strftime (like %-I) works on normal Home Assistant installs on Rasbian or Ubuntu but doesn’t work on Hass.IO. I wanted a 12 hour clock with no leading zero and came up with the following:

{{(now().strftime('%I')|int)~now().strftime(':%M')}}

By casting the hour to an integer Jinja is stripping the leading zero and the result is something like 1:23 as I would expect.

1 Like

yeah, but in that regard, you wouldn’t need to use strfttime, you could just use .hour

{{ now().hour~now().strftime(':%M') }}

Using now() is much easier than using an as_timestamp function where you are forced to use strfttime.

True, but only for 24 hour time. Is there a way to make that 12 hour?

No, the dot functions in datetime are all 24 hr i believe. There may be one I don’t know about…

I waded through the python docs and it appears only 24 hour results are offered, hence my goofy template above for 12 hour time.

@xirixiz

HI Xirixiz,
seems this would be of interest to the MijnAfvalWijzer component also? both the component and template sensors might be simplified?
have a look if you would.
Cheers,
Marius

Thats not working for me :frowning_face:

My code:

  • 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”] %}
    {{ 'day ’ + days[now().weekday()] + ’ ’ + months[now().month-1] }}

Im getting this:

Untitled-2

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