Date formatting

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') }}
1 Like

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.

@mudape
That did the trick!

Thanks,
Tomas

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:

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 }}
1 Like

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

3 Likes

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 !

@petro
Much more elegant solution, thank you!

Its works!!! Thanksss :grinning:

Wow luv it! Thanksss :grinning: @petro

Where in config can I add the Next Sunrise and Next Sunset? Just not sure where to add in configuration.yaml…

Probably template sensor.

Yeah it was… I had a -H in there which Hassio hates. Also wanted to use AM and PM so it was pretty convoluted but I’m using this:

# Weather prediction
sensor:
  - platform: template
    sensors:
      nextsunrise:
        friendly_name: 'Next Sunrise'
        value_template: >
          {{ as_timestamp(states.sun.sun.attributes.next_rising) | timestamp_custom('(%I/%M%p)') | replace("(0", "") | replace("/", ":") | replace (")", "") }}
        icon_template: mdi:weather-sunset-up
      nextsunset:
        friendly_name: 'Next Sunset'
        value_template: >
          {{ as_timestamp(states.sun.sun.attributes.next_setting) | timestamp_custom('(%I/%M%p)') | replace("(0", "") | replace("/", ":") | replace (")", "") }}
        icon_template: mdi:weather-sunset-down

FYI, you can simplify this. In strfttime, characters that are between the ‘date idenfitiers’ are written as is.

In your use case you have the following string in strfttime:

‘(%I/%M%p)’

The characters that are ‘date identifiers’ are: %I for hour (12 hr format), %M for minute, and %p for AM/PM. This means all your characters between them can be changed without impacting your date format. So if you wanted you could have this as your date format for strftime:

‘The hour is %I, The Minute is %M, the AM/PM is %p’

the result would be:

The hour is 05, The Minute is 45, the AM/PM is AM

So in your case, you are using replace to remove characters that you don’t want. You can simply remove them from strfttime. The only exception you have is that you want to only remove the leading 0 for hour. Typically this can be done by placing a minus sign between the % symbol and the trailing character. This, for some reason, does not work in Jinja. That means you need to keep your replace("(0",""). In the end, this is a simplified version and hopefully you learned something about strfttime:

sensor:
  - platform: template
    sensors:
      nextsunrise:
        friendly_name: 'Next Sunrise'
        value_template: >
          {{ as_timestamp(states.sun.sun.attributes.next_rising) | timestamp_custom('(%I:%M%p') | replace("(0", "") }}
        icon_template: mdi:weather-sunset-up
      nextsunset:
        friendly_name: 'Next Sunset'
        value_template: >
          {{ as_timestamp(states.sun.sun.attributes.next_setting) | timestamp_custom('(%I:%M%p') | replace("(0", "") }}
        icon_template: mdi:weather-sunset-down
2 Likes

In the end I got to this one:

sensor:
  - platform: template
    sensors:
      nextsunrise:
        friendly_name: 'Next Sunrise'
        value_template: >
          {{ as_timestamp(states.sun.sun.attributes.next_rising) | timestamp_custom(' %I:%M%p') | replace(" 0", "") }}
        icon_template: mdi:weather-sunset-up
      nextsunset:
        friendly_name: 'Next Sunset'
        value_template: >
          {{ as_timestamp(states.sun.sun.attributes.next_setting) | timestamp_custom(' %I:%M%p') | replace(" 0", "") }}
        icon_template: mdi:weather-sunset-down

Which is very similar to your one. I’m surprised there isn’t a %I equivalent that strips the leading zero like there is for Windows etc.

I think I learnt more about strftime yesterday than I ever wanted to know LOL.!

Yeah, thats what the minus is supposed to do “%-I” but it doesn’t work. Super annoying.

1 Like

How to modify this to have european time without ‘am’ and ‘pm’?
I mean 24 hrs format.

1 Like

Use “%H” instead of “%I”

{{ as_timestamp(states.sun.sun.attributes.next_setting) | timestamp_custom(' %H:%M') | replace(" 0", "") }}
1 Like

Thanks… got it.

As Petro said. You probably don’t want to do the substitute to get rid of the leading zero if you use 24hr format and also probably don’t want the colon : between hours and minutes so %H%M will give a proper 24hr format.