Date formatting


#21

@petro
Much more elegant solution, thank you!


#22

Its works!!! Thanksss :grinning:


#23

Wow luv it! Thanksss :grinning: @petro


#24

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


#25

Probably template sensor.


#26

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

#27

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

#28

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


#29

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


#30

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


#31

Use “%H” instead of “%I”

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

#32

Thanks… got it.


#33

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.


#34

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.


#35

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.


#36

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


#37

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


#38

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


#39

@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


#40

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