Display sun rising in UI

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?

I struggled with stripping out the leading 0, as well as the am/pm setting. I was finally able to get it working correctly using the following code in my sensors YAML file:

- platform: template
  sensors:
    sunset_time:
      value_template: '{% set timestamp = as_timestamp(states.sun.sun.attributes.next_setting) | timestamp_custom("%I:%M") %} {{ timestamp.lstrip("0") }}'
      friendly_name: "Sunset"

- platform: template
  sensors:
    sunrise_time:
      value_template: '{% set timestamp = as_timestamp(states.sun.sun.attributes.next_rising) | timestamp_custom("%I:%M") %} {{ timestamp.lstrip("0") }}'
      friendly_name: "Sunrise"
6 Likes

Great, thanks a lot for that!

fyi, @jeremyowens using %-I should remove the 0.

"%-I:%M %p" should give you something like 9:05 PM, remove the %p if you don’t want am/pm.

more reference http://strftime.org/

4 Likes

Thanks Jeremy… using your sensor code. One little mod people may like… use customize to set the sensor badge unit of measurement to AM or PM and it displays nicely under the time

  customize:
    sensor.sunrise_time:
      unit_of_measurement: AM
    sensor.sunset_time:
      unit_of_measurement: PM

Looks like this: https://www.screencast.com/t/K3hdqHq6YiXa

2 Likes

Hi
How do i get hold of these sensors:
sensor.sunrise_time
sensor.sunset_time

/Jerry

Just scroll up a little bit.

You mean this:

  • platform: template
    sensors:
    sunset_time:
    /Jerry

Yep, that’s where the two template sensors are defined.

Oki then, i will try this out.

If i paste this in config.yaml

  • platform: template
    sensors:
    sunset_time:
    value_template: ‘{% set timestamp = as_timestamp(states.sun.sun.attributes.next_setting) | timestamp_custom("%I:%M") %} {{ timestamp.lstrip(“0”) }}’
    friendly_name: “Sunset”

  • platform: template
    sensors:
    sunrise_time:
    value_template: ‘{% set timestamp = as_timestamp(states.sun.sun.attributes.next_rising) | timestamp_custom("%I:%M") %} {{ timestamp.lstrip(“0”) }}’
    friendly_name: “Sunrise”

I get this error:
Error loading /config/configuration.yaml: while parsing a block mapping
in “/config/configuration.yaml”, line 5, column 1
expected , but found ‘-’
in “/config/configuration.yaml”, line 34, column 1

How come?