Date formatting


#1

Hi,
I would like to display date and time like: May 10, 2017

Is there a simple way to do that?

Thanks,
Tomas


Weekday and month sensor
Help Formatting Sun Events
As_timestamp and timestamp_custom local language
#2

This may help: http://strftime.org/ (Python strftime reference). It helped me to get proper formatting on things like this:

#
#
#  Almanac Sensors
#
#

     nextsunrise:
       friendly_name: 'Next Sunrise'
       value_template: "{{ as_timestamp(states.sun.sun.attributes.next_rising) | timestamp_custom('%D %-I:%M %P') }}"

     nextsunset:
       friendly_name: 'Next Sunset'
       value_template: "{{ as_timestamp(states.sun.sun.attributes.next_setting) | timestamp_custom('%D %-I:%M %P') }}"
       
     dayofyear:
       friendly_name: 'Day Number'
       value_template: "{{ now().strftime('%j') }}"

     weekofyear:
       friendly_name: 'Week Number'
       value_template: "{{ now().strftime('%U') }}"


#3

Thanks. I played with it a little, but haven’t been able to figure out the formatting mysteries yet. What I am after is a long date format such as “Feb 11, 2017” or “February 11, 2017”. I have tried %MMM %DD %YYYY, but I only get unknown as a result.


#4

Hi, have you tried:

%b %d, %Y
Or
%B %d, %Y


#5

I tried the following, but it generates errors as “unknown” or “none”:
value_template: "{{ as_timestamp(states.sensor.date) | timestamp_custom('%B %d, %Y') }}" value_template: "{{ states.sensor.date.strftime('%B %d, %Y') }}"

What am I doing wrong?


#6

Use states.sensor.date.state wherever you use states.sensor.date.


#7

Also - use the template dev tool. It’ll let you test templates without having to repeatedly restart.


#8

Where do I find the templates dev tool?

Also, does anybody have a complete template example of this. I tried states,sensor.date.state with the above, but it didn’t work.


#9

Open left toolbar. Bottom row of icons. 2nd from right.

No comma after “states”.

{{states.sensor.date.state}}


#10

Oops, seems like a typo in the code there. I will look into it when I get home tonight.

I agree that YAML is kind of picky. Small things can prevent the whole system from reloading, but this template thing was even nastier. An error in the code here didn’t allow me to reload HA through the command
sudo systemctl restart homeassistant.service
I had to restart the HA server completely. I will revisit the templates dev tool again. I have looked there, but didn’t understand how to test a template.

Thanks for the advice!
Tomas


#11

Thanks for the help, love this community!
{{ now().strftime('%d %b, %Y') }} yields “12 May, 2017”

Now, if I could bother you a bit more, I would like to translate the months to Swedish.
If I use {{ now().strftime(’%m’) }}` I get the month number. Can I use that in a template to pick the number from an array, such as (januari, februari, mars, april, maj, juni, etc…)?

Thanks in advance!
Tomas


#12

There’s prob a way to do this with localization (but I don’t know how). Try:

value_template: >
  {% set mo = now().strftime('%m') | int %}
  {% set months = ["Januari","Februari", "Mars", "April", "Maj", "Juni"] %}
  {{months[mo - 1]}}

(subtracting one since arrays are zero-based)


#13

Excellent, finally got it! In templates dev tool that is. The code:

{% set mo = now().strftime('%m') | int %} {% set months = ["januari", "februari", "mars", "april", "maj", "juni", "juli", "augusti", "september", "oktober", "november", "december"] %} {{ now().strftime('%d') + ' ' + months[mo - 1] + ' '+ now().strftime('%Y')}}

gives me “12 maj 2017” which is what I am looking for.

BUT! adding that code into my template section like this:

date_long: friendly_name: 'Datum' value_template: '{% set mo = now().strftime('%m') | int %} {% set months = ["januari", "februari", "mars", "april", "maj", "juni", "juli", "augusti", "september", "oktober", "november", "december"] %} {{ now().strftime('%d') + ' ' + months[mo - 1] + ' '+ now().strftime('%Y')}}'

gives the following error:
found character '%' that cannot start any token in "/home/homeassistant/.homeassistant/sensors/templ-sensors.yaml", line 26, column 26

I’m at a loss here.


#14

Missing > after value_template: ?
I’m just guessing here, but this seem to work:

- platform: template
  sensors:
    date_long: 
      friendly_name: 'Datum'
      value_template: >
        {% set months = ["januari", "februari", "mars", "april", "maj", "juni", "juli", "augusti", "september", "oktober", "november", "december"] %}
        {% set month = months[now().strftime('%m') | int -1] %}  
        {{ now().strftime('%d') + ' ' + month + ' '+ now().strftime('%Y') }}

#15

Try making your outside quotes on the value template DOUBLE quotes and then use single quotes for everything inside. Or vice versa. But YAML seems to have issues with mixing them.


#16

@mudape
That did the trick!

Thanks,
Tomas


#17

Hi Guys! Im trying to translate days the same way, but its get stuck in Sunday:

  - platform: template
    sensors:
      date_long:
        friendly_name: 'Fecha'
        value_template: >
          {% set months = ["Ene", "Feb", "Mar", "Abr", "May", "Jun", "Jul", "Ago", "Sep", "Oct", "Nov", "Dic"] %}
          {% set month = months[now().strftime('%m') | int -1] %}
          {% set days = ["Lunes ", "Martes ", "Miercoles ", "Jueves ", "Viernes ", "Sabado ", "Domingo "]  %}
          {% set day = days[now().strftime('%A') | int -1] %}
          {{ day + ' ' + now().strftime('%d') + ' ' + month }}

Any ideas why? :upside_down_face:


#18

You could try this:

platform: template
    sensors:
      date_long:
        friendly_name: 'Fecha'
        value_template: >
          {% set months = ["Ene", "Feb", "Mar", "Abr", "May", "Jun", "Jul", "Ago", "Sep", "Oct", "Nov", "Dic"] %}
          {% set month = months[now().strftime('%m') | int -1] %}
          {% set days = ["Domingo ", "Lunes ", "Martes ", "Miercoles ", "Jueves ", "Viernes ", "Sabado "]  %}
          {% set day = days[now().strftime('%w') | int] %}
          {{ day + ' ' + now().strftime('%d') + ' ' + month }}

#19

@tomoqv, @ssaavedra

Seems like everyone in this thread is using strftime for no reason. The now() object has built in methods that do not need to be converted to integers:

{{ now().month }}
{{ now().day }}
{{ now().weekday() }}
{{ now().year }}
{{ now().hour }}
{{ now().minute }}
{{ now().second }}
    value_template: >
          {% set months = ["Ene", "Feb", "Mar", "Abr", "May", "Jun", "Jul", "Ago", "Sep", "Oct", "Nov", "Dic"] %}
          {% set days = ["Lunes", "Martes", "Miercoles", "Jueves", "Viernes", "Sabado", "Domingo"]  %}
          {{ 'day ' + days[now().weekday()] + ' ' + months[now().month-1] }}

weekday() starts at 0 being monday.
all the rest are based off 1 equaling the first month/day/year etc.


#20

Yes and how irritating is it that on (for example) a Monday

{{ now().strftime("%w") }} returns 1 and
{{ now().weekday() }} returns 0

:scream:

I only know because I too am using strftime unnecessarily so I went to change it !