@petro
Much more elegant solution, thank you!
Its works!!! Thanksss
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
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.
How to modify this to have european time without âamâ and âpmâ?
I mean 24 hrs format.
Use â%Hâ instead of â%Iâ
{{ as_timestamp(states.sun.sun.attributes.next_setting) | timestamp_custom(' %H:%M') | replace(" 0", "") }}
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.
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.
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.
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
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: