Enhanced Sun component

Released 3.3.0b0

The astral package this integration uses can accept two different types of data for certain events like sunrise & sunset to describe where these events are seen relative to the observer.

The first, which has been the only supported option so far, is the horizon. In this case, the elevation of the observer relative to ground level defines how much to adjust the event by due to the curvature of the Earth.

The second is an obstruction, such as the top of a mountain. This is defined by the horizontal distance to the obstruction, and the relative height of the obstruction from the observer (both expressed in meters), which can be negative if the obstruction is lower than the observer (e.g., the observer is on an even higher mountain.)

This PR adds support for defining either type for easterly events (sunrise, dawn) and westerly events (sunset, dusk.)

NOTE: The documentation has not yet been updated to describe how to use this new feature from YAML configuration. Using it from UI configuration is obviously much easier. If you do want to try it from YAML, here is an example:

sun2:
  - unique_id: home
    observer_elevation:
      above_ground: 0
      sunrise_obstruction:
        distance: 50000
        relative_height: 2000

The old elevation config option has been deprecated by observer_elevation. It is still supported, however, in case you want to go back to a previous version of this integration. These options are no longer “coupled” with the location options (latitude, etc.)

Do you think a sunrise azimuth sensor and a sunset azimuth sensor would help?

“People want this. They just don’t know it yet.” - Steve Jobs

:slight_smile:

I have to check out the sunrise_obstruction configuration and see if it works well for me.

1 Like

For those that would like to follow along with what I’m planning for the 3.3.0 release, please see:

Allow more flexible configuration of what affects sunrise & sunset events by pnbruckner · Pull Request #116 · pnbruckner/ha-sun2 (github.com)

1 Like

Released 3.2.1b0
Released 3.2.1

Only reload config entry if it’s in a state where it can be reloaded

Depending on startup timing, it’s possible for an attempt to be made to reload a configuration entry when it’s still in the process of being initially setup (i.e., loaded.) This change avoids reloading a configuration entry when it is not in a state where it can be reloaded.

For more details, see issue #117.

Released 3.3.0b1

Add services & new sensor options

Add sun2.get_location & sun2.update_location services. Also add new sunrise_azimuth & sunset_azimuth sensor options (which result in sensors named “Rising azimuth” & “Setting azimuth”, respectively.)

Together these allow dynamically updating location parameters (latitude, longitude and time zone), and/or parameters that define what affects sunrise & sunset events.

One use case might be for vacation. Let’s say you add a config entry to get sun related info for a vacation location. You can use the services to update its location maybe once a day, or when you push a button in the UI, based on the current location indicated by a device tracker entity. (My Entity Time Zone integration can help with determining the time zone where a device tracker is located.)

Another use case might be when, e.g., sunrise is based on a mountain, but the horizontal distance to, and the relative height of, the top of that mountain changes throughout the year as the sunrise azimuth changes.

Note this also includes the fix from 3.2.1.

EDIT: Oops, forgot to provide some detail on the services, since again, I haven’t gotten around to updating the docs yet…

sun2.get_location

Can be used with any integration entry. Looks like this:

sun2.update_location

Can only be used with UI created integration entries (since it doesn’t update YAML config files.) Can specify the location paramters, or the observer elevation, or both. Looks like this:

image

Please try out the 3.3.0b1 release (described below.) It should have all the features you’re looking for.

It should be possible to create an automation that adjusts the distance & relative height of the mountains to the east (via the sun2.update_location service) as the year goes by (based on the state of the “Rising azimuth” entity.)

E.g., maybe something like:

automation:
  - id: sunrise_adjust
    alias: Sunrise adjustment
    trigger:
      - platform: numeric_state
        entity_id: sensor.home_sun_rising_azimuth
        below: 70
        variables:
          dist: 10000
          relh: 5000
      - platform: numeric_state
        entity_id: sensor.home_sun_rising_azimuth
        above: 70
        below: 105
        variables:
          dist: 9000
          relh: 7000
      - platform: numeric_state
        entity_id: sensor.home_sun_rising_azimuth
        above: 105
        variables:
          dist: 11000
          relh: 6000
    action:
      - service: sun2.update_location
        data:
          location: Home
          observer_elevation:
            above_ground: 0
            sunrise_obstruction:
              distance: "{{ dist }}"
              relative_height: "{{ relh }}"
3 Likes

Nice. I am very interested in photographing and have a card for showing me when golden and blue hours start and stop both in the morning and the evening.

image

This is now updated to use my phone location instead of being my home location. The sensors used for latitude, longitude and time_zone is from the companion app for Android.
I set a new sun2 location if my phone location has changed more than 50km.

alias: Ny posisjon til sun2
description: ""
trigger:
  - platform: state
    entity_id:
      - device_tracker.pixel_7
condition: []
action:
  - service: sun2.get_location
    data:
      location: phone
    response_variable: location
  - if:
      - condition: template
        value_template: >-
          {{ distance(location.latitude, location.longitude,
          'device_tracker.pixel_7') > 50 }}
    then:
      - service: sun2.update_location
        metadata: {}
        data:
          location: phone
          latitude: "{{ state_attr('device_tracker.pixel_7', 'latitude') }}"
          longitude: "{{ state_attr('device_tracker.pixel_7', 'longitude') }}"
          time_zone: "{{ state_attr('sensor.pixel_7_current_time_zone', 'time_zone_id') }}"
mode: single

2 Likes

Released 3.3.0b2

Updated documentation, services.yaml & en.json. Also, ru.json was added (but does not include all the new functionality in this beta.)

As far as I know, this is the final beta release for 3.3.0. Assuming I get no negative feedback, I’ll release 3.3.0 shortly.

1 Like

Thank you! I cover alot of area each summer on vacation, so this is great!

Any chance you can chare the card?

And a big thank you to you Phil!

**Edit:**deconz_daylight

How do i get the time of golden/blue hour? I’ve tried to research but my brain is getting error. I figured it might have something to do with azimuth time/angle and maybe a making a sensor?

Edit2: I see that the deCONZ daylight sensor have a golden hour 1 and 2, but how do i get that to display on a card?

I will, but you have to wait until later i May. I am not at home at the moment.

1 Like

Do you want to get a time? If so, which time? When they start? When they end? Each one, or both together?

Or do you just want a sensor that indicates when it is in the golden hour (or blue hour, or both)?

Released 3.3.0

1 Like

Hello Phil and thank you for the reply!

I was looking for start/end time for both golden and blue hours and replicate stigvis card.

I don’t need to know if i’m in the golden, just the time of when golden/blue hour starts and ends

/Keellan

Hello and sorry to bother you again Phil. I’ve been reading and reading but i can’t figure this out. I usually find an answer after alot of searching and trial and error, but this nut I can’t crack on my own and would really appriciate your help

Blue hour is between -6° and -4°, and golden hour is between -4° and 6°.

So, in the morning, blue hour starts at -6° and ends at -4°, and golden hour starts at -4° and ends at 6°.

In the evening, golden hour starts at 6° and ends at -4°, and blue hour starts at -4° and ends at -6°.

So, to do what you ask, you need eight time_at_elevation sensors, four with direction set to rising, and four more with direction set to setting.

1 Like

Hi

Here are the sensors I have created. The code below is from the storage to HA, but you have to use the sun2 configuration to create them

{
“entry_id”: “be87d11dcd1a9aa7d4de8fd1b2e7d4eb”,
“version”: 1,
“minor_version”: 1,
“domain”: “sun2”,
“title”: “phone”,
“data”: {},
“options”: {
“elevation”: 0.0,
“time_zone”: “Europe/Oslo”,
“latitude”: 58.737857,
“longitude”: 5.6400118,
“sensors”: [
{
“time_at_elevation”: -6.0,
“direction”: “rising”,
“icon”: “mdi:weather-sunset-down”,
“name”: “blue hour starts in the morning”,
“unique_id”: “dcf3e30175de6772cf298682ec037045”
},
{
“time_at_elevation”: -4.0,
“direction”: “rising”,
“icon”: “mdi:weather-sunset-down”,
“name”: “golden hour starts in the morning”,
“unique_id”: “1b08550a104a4140818439ee6f8500c6”
},
{
“time_at_elevation”: 6.0,
“direction”: “rising”,
“icon”: “mdi:weather-sunset-down”,
“name”: “golden hour ends in the morning”,
“unique_id”: “3e888eb383f9ae6ec949aa3a7d365616”
},
{
“time_at_elevation”: 6.0,
“direction”: “setting”,
“icon”: “mdi:weather-sunset-down”,
“name”: “golden hour starts in the evening”,
“unique_id”: “a2e734f5e58222825d46dde64ee6c9f7”
},
{
“time_at_elevation”: -4.0,
“direction”: “setting”,
“icon”: “mdi:weather-sunset-down”,
“name”: “golden hour ends in the evening”,
“unique_id”: “48b4b258edfbacaa41cbf2e470341a9f”
},
{
“time_at_elevation”: -6.0,
“direction”: “setting”,
“icon”: “mdi:weather-sunset-down”,
“name”: “blue hour ends in the evening”,
“unique_id”: “d6db91cbedab062536d361294e7694f6”
}
],
“observer_elevation”: [
0.0,
0.0
]
},

Here is the card (in norwegian)

type: entities
entities:
  - entity: sensor.phone_sun_bla_time_slutter_kveld
    icon: mdi:weather-sunset-down
    type: custom:multiple-entity-row
    state_header: Blå
    name: Gylden time kveld
    format: time
    entities:
      - entity: sensor.phone_sun_gylden_time_starter_kveld
        name: Start
        format: time
      - entity: sensor.phone_sun_gylden_time_slutter_kveld
        name: Stopp
        format: time
  - type: divider
  - entity: sensor.phone_sun_gylden_time_slutter_morgen
    attribute: tomorrow
    icon: mdi:weather-sunset-up
    type: custom:multiple-entity-row
    state_header: Stopp
    name: Gylden time morgen
    format: time
    entities:
      - entity: sensor.phone_sun_bla_time_starter_morgen
        name: Blå
        format: time
        attribute: tomorrow
      - entity: sensor.phone_sun_gylden_time_starter_morgen
        name: Start
        format: time
        attribute: tomorrow
  - type: divider
  - entity: sensor.phone_sun_setting_azimuth
    format: precision0
    state_header: Ned
    name: Sol, retning
    icon: mdi:sun-compass
    type: custom:multiple-entity-row
    entities:
      - entity: sensor.phone_sun_rising_azimuth
        format: precision0
        name: Opp
  - entity: sensor.phone_sun_daylight
    attribute: tomorrow_hms
    state_header: I morgen
    icon: mdi:sun-clock
    name: Daglengde
    type: custom:multiple-entity-row
    entities:
      - entity: sensor.phone_sun_daylight
        name: I dag
        attribute: today_hms
  - entity: sensor.pixel_7_geocoded_location
    name: ' '
title: Fototid

image

And here is the automation for setting a new location when on travel

alias: Ny posisjon til sun2
description: ""
trigger:
  - platform: state
    entity_id:
      - device_tracker.pixel_7
    attribute: latitude
condition: []
action:
  - service: sun2.get_location
    data:
      location: phone
    response_variable: location
  - if:
      - condition: template
        value_template: >-
          {{ distance(location.latitude, location.longitude,
          'device_tracker.pixel_7') > 10 }}
    then:
      - service: sun2.update_location
        metadata: {}
        data:
          location: phone
          latitude: "{{ state_attr('device_tracker.pixel_7', 'latitude') }}"
          longitude: "{{ state_attr('device_tracker.pixel_7', 'longitude') }}"
          time_zone: "{{ state_attr('sensor.pixel_7_current_time_zone', 'time_zone_id') }}"
mode: single

1 Like

I’m currently burnt out and generally need more direct instructions as my brain doesn’t quite keep up, so this helps alot! Appreciate your answer!

Going to sit down and try this!

Tusen takk!

Do you have any idea why the sensors created with negative elevation shows up as “unknown”?

The two sensors that works is the golden hour start in the morgon and ending in the evening, when the elevation is above horizon

Read through the readme but i couldn’t find an answer

Edit. Nothing was wrong, I just remembered where i live and the sun almost doesn’t set atm… :person_facepalming:

2 Likes

Hello again!

I was wondering if there’s a way to get a sensor like ‘time at elevation’, but instead of elevation i would like ‘time at azimuth’. I want the times when the sun is shining on my balcony, so I can open the windows and lower the temperature so my flowers don’t die