Sun Attribute - "today" sunrise/sunset

The “sun” integration presently has attributes for the next rising/setting, however sometimes it is desirable to know or compare with when today’s (calendar day) sunrise/sunset is or was whether or not it has passed yet.

It would be good if the current day’s sunrise/sunset times were available instead of just the next one.

This could be resolved by a template sensor, so not sure if I would vote for this, can you provide a use case?
EDIT: if use case is uncommon, then why add…

How can it be resolved by a template sensor? I’ve been unsuccessfully trying to find a way to off and on for a while. There was a thread I found but it didn’t seem like they ended up with a simple sensor that did that at the end.

I’ve been trying to eliminate some edge cases where I have automations that trigger based on sunrise/sunset with an offset and if I’m working on the system when the times fire then I have no way to correct the state when it finishes loading. This was compounded when from Dec-Feb there was a bug causing extremely slow startup of HA which made it more likely I’d miss an automation trigger and get out of sync so lights would be stuck on or off requiring manual interaction.

I’d like to be able to build a simple binary-sensor state template that is based on fixed state data rather than triggers like (psudocode) 6AM < now() < (sunrise + 1hr) but with only next_sunrise available this throws the time off by several minutes from what it ought to be.

Similarly, it would be nice to have a simple dashboard that tells me when the sunrise/sunset for the same day happened so I can look and go as a human “oh the sun set 15 minutes ago” when I want to know that information.

This doesn’t seem like it should be so uncommon of a usecase, its common enough most home-assistants like Google voice-assistant can correctly answer “when is sunset/sunrise today” and it will tell you “it is at X” or “it was at X”.

It seems like this would make more sense than the “next-sunrise” and “next-sunset” already implemented which I’m not sure I understand a usecase for since they may not even be the same day.

I was also looking for this as I wanted a bar graph showing how many hours of daylight left. I needed a total hours between sunrise to sunset today so I can show it as a percentage. In the end I just used the one that’s available as it’s only a couple of minutes different day to day.

You may prefer to use the Sun2 custom integration.

This information is also available by using Sun2 and can be made into a % remaining sensor using a Template sensor.

template:
  - sensor:
      - name: Daylight Percent Remaining
        state: > 
          {% set total = state_attr('sensor.daylight', 'today') %}
          {% set used = (now() -  state_attr('sensor.sunrise', 'today')).total_seconds() / 3600 %}
          {{  100 * ((total - used)/total ) | round(2, 0) }}
        unit_of_measurement: %
        state_class: measurement
        availability: >
          {{ ['sensor.daylight', 'sensor.sunrise'] 
          | select('is_state', ['unavailable', 'unknown'])
          | list | count > 0 }}
2 Likes

I’ve not heard of “Sun2” until now. Is that an addon somewhere?

It is a custom integration (in HA, “addons” are different than integrations). Sun2 is available through HACS or manual download at the link I posted above.

Oh oops, I totally missed the hyperlink. Sounds like it would add the sensors but I still think the basic sunrise-sunset for the same day ought to be a built-in one. This seems a good work-around potentially though.

Try this Trigger-based Template Sensor. At the start of every day (‘00:00:01’) it computes the length of the current day’s daylight hours and reports sunrise and sunset. It won’t work for sub-Arctic regions because it assumes sunrise/sunset always occur after `00:00:00’ every day.

Feel free to change the time format to whatever you prefer.

template:
  - trigger:
      - platform: time
        at: '00:00:01'
    sensor:
      - name: Today
        state: >
          {% set sunrise = state_attr('sun.sun', 'next_rising') | as_datetime %}
          {% set sunset = state_attr('sun.sun', 'next_setting') | as_datetime %}
          {{ (sunset - sunrise).seconds | timestamp_custom('%H:%M', false) }}
        attributes:
          sunrise: >
            {{ (state_attr('sun.sun', 'next_rising') | as_datetime | as_local).isoformat() }}
          sunrise: >
            {{ (state_attr('sun.sun', 'next_setting') | as_datetime | as_local).isoformat() }}

Note

Initially, its value will be unknown. You have to wait for it to trigger at least once in order to display proper values

1 Like

I’m curious what the .isoformat() at the end of that template does? Why is that better than just leaving it with | as_local?

My usecase is that I have a card with simply todays sunrise time, todays daylight and todays sunset time.
For the moment I am using just the available figures(next rising, next setting), but I would like to have the correct number in there.

Kind regards,

Bert

Hi, I have got it working - sort of!

create a template sensor called sunset with the state template:

{{ states.sun.sun.attributes['next_rising']| as_timestamp | timestamp_custom('%H:%M') }}

No need to set unit of measure, device class or state class.

In the sunset helper make sure that Voice Assistants expose Assist is turned on.

Then your voice device should respond to “what is sunset”. When isnt working yet!