Display sun rising in UI

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?

These are template sensors, and the line before these config entries must be:

sensor:

If you the get an error message saying something like “duplicate sensor” then you already have such a line somewhere in your config file, and need to move your above config entries underneath that existing list of sensors.

1 Like

Oki, i will try that.