Display sun rising in UI

i have searched and tried, but i think on the wrong places.

i want to show the time the sun is rising and stting in the frontend.
i made a templatesensor and it works.

but it shows like: 2016-08-03T19:07:03+00:00

i cant figure out how to show this time just like: 19:07

I haven’t tested this but you can try:

{{ [datetime].strftime('%H:%M') }}

Replace [datetime] with whatever attribute you use for getting the time.

This will most likely not work in this case as

{{ states.sun.sun.attributes.next_setting.strftime('%H:%M') }}

for example will return an error stating that the object str does not have a method called strftim.

I would really like someone who got datetimes working in HA answer here. I have had my fair share of trouble with datetimes and till this day not got it working.

My main problem seems to be that all the datetimes are strings…even after using filters like “timestamp_local” in Jinja. And all the templating functions expect me to deliver datetime objects.

Any help would be appriciated.

Greetings

at least now i know it wasnt something simple i have overseen.

i tried what @PhyberApex wrote before. that returns unavailable.

i know i have read somewhere that someone used a temp variable.
i guess i need to use something like that, but i still have problems making the right code from it.

must be somthing like: VAR=states.sun.sun.attributes.next_setting and then VAR.strftime(’%H:$M’)

sometimes i feel a bit … :wink:
the language between the {{ and the }} is python?’

Hey,
you know you can test the templates in the developer tools before using them right? It’s the next to last icon in the bar on the bottom of the left side panel in HA iirc.

The language between the {{}} is jinja 2 a templating engine for Python. To set variables is jinja you would use

{% set time = states.sun.sun.attributes.next_setting %}

And then you could use time in between {{}} again. But this gives me the exact same results.

1 Like

thx for reminding me @PhyberApex
I knew it but had forgotten it. so i restarted HA every time :wink:

i’m getting old. so many languages to learn in such a short amount of time and they all are somewhat the same, but all so different.

i would think this shouldnt be that hard. i have switches that are goiing on 1 hour before sunset, i just want that same time displayed as a sensor in the gui, so my wife doesnt have to ask me: “why arnt the light switched on?” 2 minutes before they switch on :wink:

1 Like

i tried:

{{as_timestamp(states.sun.sun.attributes.next_setting) | timestamp_local }}

and that gives at least a better readable printout: 2016-08-03 21:07:03
but it is still a string, so strftime is no option.

As the sun attribute and the existing custom filters don’t seem to output datetime but strings or floats, a new custom filter is needed, as far as I can tell.

1 Like

i found a way.
maybe not the best one, but it functions.

value_template: '{% set tempvar = ((as_timestamp(states.sun.sun.attributes.next_setting)) | timestamp_local) %}
{% set tempvar = (tempvar | list) %}
{{ tempvar[11] + tempvar[12] + tempvar[13] + tempvar[14] + tempvar[15] }}'

edit: now all i need to figure out is how to manipulate that time so it shows 1 hour earlier

2 Likes

the complete codes for different options:

above you find sunset time.
to see Sunrise time change next_setting to next_rising.

to see a sunset/rise time with an offset (in this case -(60minutes*60seconds))

value_template: '{% set tempvar = ((as_timestamp(states.sun.sun.attributes.next_setting)-(60*60)) | timestamp_local) %}
{% set tempvar = (tempvar | list) %}
{{ tempvar[11] + tempvar[12]  + tempvar[13] + tempvar[14] + tempvar[15] }}'

to see the time left till next sunset/rise

value_template: '{% set tempvar = ((as_timestamp(states.sun.sun.attributes.next_setting)-(as_timestamp(now())) | timestamp_local) %}
{% set tempvar = (tempvar | list) %}
{{ tempvar[11] + tempvar[12]  + tempvar[13] + tempvar[14] + tempvar[15] }}'

i hope it helps others to.

5 Likes

Great work…sadly I want to use relative_time filter :confused:

1 Like

i tried relative_time, but it gives this: 1970-1-1 05:21

the last option gives the same but shows: 05:21

‘{% set tempvar = ((as_timestamp(states.sun.sun.attributes.next_setting)) | timestamp_local) %}{{ tempvar[11:16] }}’

2 Likes

thx.
now i feel stupid for turning it into a list before taking a part from the string :wink:

and i guess i did something wrong when i tried it with : because that didnt function at that moment.

i can be even more simple as i figured out now:

‘{{ ((as_timestamp(states.sun.sun.attributes.next_setting)) | timestamp_local) [11:16] }}’

3 Likes

That works nice, thanks.
I used it in a template sensor to display the rising and setting times directly in the GUI.

1 Like

I found this is a more convenient way to do it:

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

Edit: removed wrong “(” at the start.

3 Likes

Thanks for this. Works perfectly! I did remove the first ‘(’

And if anyone’s wondering how it looks in a sensors.yaml file:

  - platform: template
    sensors:
      sunset_time:
        value_template: '{{ as_timestamp(states.sun.sun.attributes.next_setting)  | timestamp_custom("%H:%M") }}'

  - platform: template
    sensors:
      sunrise_time:
        value_template: '{{ as_timestamp(states.sun.sun.attributes.next_rising)  | timestamp_custom("%H:%M") }}'
5 Likes

i guess you are right.
that option wasnt there at the time i wrote it, but now that should be the way.

1 Like

Yeah my bad. I copied it from my config where I use it to display a time which is offset by a certain amount by a slider to enable variable home automation.

See it the example here:

@ReneTode: I revived this topic since I was struggling as well with the same issue and found the timestamp_custom function very useful also for my automation (see the link above)

2 Likes

This is great, just what I was looking for :slight_smile:

Is there any way to change the format of the time? For example from 05:59 to 5:59 AM, and from 18:24 to 6:24 PM?