Skip to content

pnbruckner/ha-sun2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

76 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sun2 Sensor Sun2 Sensor

Creates sensors that provide information about various sun related events.

Follow the installation instructions below. Then add the desired configuration. Here is an example of a typical configuration:

sensor:
  - platform: sun2
    monitored_conditions:
      - sunrise
      - sunset
      - sun_phase
binary_sensor:
  - platform: sun2
    monitored_conditions:
      - elevation

Installation

With HACS

hacs_badge

You can use HACS to manage the installation and provide update notifications.

  1. Add this repo as a custom repository:
https://github.com/pnbruckner/ha-sun2
  1. Install the integration using the appropriate button on the HACS Integrations page. Search for "sun2".

Manual

Place a copy of:

__init__.py at <config>/custom_components/sun2/__init__.py
binary_sensor.py at <config>/custom_components/sun2/binary_sensor.py
const.py at <config>/custom_components/sun2/const.py
helpers.py at <config>/custom_components/sun2/helpers.py
sensor.py at <config>/custom_components/sun2/sensor.py
manifest.json at <config>/custom_components/sun2/manifest.json

where <config> is your Home Assistant configuration directory.

NOTE: Do not download the file by using the link above directly. Rather, click on it, then on the page that comes up use the Raw button.

Versions

This custom integration supports HomeAssistant versions 2023.3 or newer.

Sensors

Configuration variables

  • monitored_conditions: A list of sensor types to create. One or more of the following:

Point in Time Sensors

type description
solar_midnight The time when the sun is at its lowest point closest to 00:00:00 of the specified date; i.e. it may be a time that is on the previous day.
astronomical_dawn The time in the morning when the sun is 18 degrees below the horizon.
nautical_dawn The time in the morning when the sun is 12 degrees below the horizon.
dawn The time in the morning when the sun is 6 degrees below the horizon.
sunrise The time in the morning when the sun is 0.833 degrees below the horizon. This is to account for refraction.
solar_noon The time when the sun is at its highest point.
sunset The time in the evening when the sun is 0.833 degrees below the horizon. This is to account for refraction.
dusk The time in the evening when the sun is a 6 degrees below the horizon.
nautical_dusk The time in the evening when the sun is a 12 degrees below the horizon.
astronomical_dusk The time in the evening when the sun is a 18 degrees below the horizon.
time_at_elevation See Time at Elevation Sensor
elevation_at_time See Elevation at Time Sensor

Length of Time Sensors (in hours)

type description
daylight The amount of time between sunrise and sunset.
civil_daylight The amount of time between dawn and dusk.
nautical_daylight The amount of time between nautical dawn and nautical dusk.
astronomical_daylight The amount of time between astronomical dawn and astronomical dusk.
night The amount of time between sunset and sunrise of the next day.
civil_night The amount of time between dusk and dawn of the next day.
nautical_night The amount of time between nautical dusk and nautical dawn of the next day.
astronomical_night The amount of time between astronomical dusk and astronomical dawn of the next day.

Other Sensors

type description
azimuth The sun's azimuth (degrees).
elevation The sun's elevation (degrees).
min_elevation The sun's elevation at solar midnight (degrees).
max_elevation The sun's elevation at solar noon (degrees).
deconz_daylight Emulation of deCONZ Daylight Sensor. Entity is sensor.deconz_daylight instead of sensor.daylight.
sun_phase See Sun Phase Sensor
Time at Elevation Sensor
key optional description
time_at_elevation no Elevation
direction yes rising (default) or setting
icon yes default is mdi:weather-sunny
name yes default is "DIRECTION at [minus] ELEVATION °"

For example, this:

- time_at_elevation: -0.833

Would be equivalent to:

- time_at_elevation: -0.833
  direction: rising
  icon: mdi:weather-sunny
  name: Rising at minus 0.833 °

Which would result in an entity with the ID: sensor.rising_at_minus_0_833_deg

Elevation at Time Sensor
key optional description
elevation_at_time no time string or input_datetime entity ID
name yes default is "Elevation at <value of elevation_at_time>"

When using an input_datetime entity it must have the time component. The date component is optional. If the date is not present, the result will be the sun's elevation at the given time on the current date. If the date is present, it will be used and the result will be the sun's elevation at the given time on the given date. Also in this case, the sensor entity will not have yesterday, today and tomorrow attributes.

Sun Phase Sensor
Possible states
state description
Night Sun is below -18°
Astronomical Twilight Sun is between -18° and -12°
Nautical Twilight Sun is between -12° and -6°
Civil Twilight Sun is between -6° and -0.833°
Day Sun is above -0.833°
Attributes
attribute description
rising True if sun is rising.
blue_hour True if sun is between -6° and -4°
golden_hour True if sun is between -4° and 6°

Binary Sensors

Configuration variables

  • monitored_conditions: A list of sensor types to create. One or more of the following:

elevation

'on' when sun's elevation is above threshold, 'off' when at or below threshold. Can be specified in any of the following ways:

elevation

elevation: THRESHOLD

elevation:
  above: THRESHOLD
  name: FRIENDLY_NAME

Default THRESHOLD (as with first format) is -0.833 (same as sunrise/sunset).

Default FRIENDLY_NAME is "Above Horizon" if THRESHOLD is -0.833, "Above minus THRESHOLD" if THRESHOLD is negative, otherwise "Above THRESHOLD".

entity_id will therefore be, for example, binary_sensor.above_horizon (-0.833), or binary_sensor.above_minus_5_0 (-5) or binary_sensor.above_10_5 (10.5).

Optional Location

The following configuration parameters are optional, and can be used with all types of sensors. All four parameters are required, and should be specified once per platform entry. These can be used to create sensors that show sun data for another (or even multiple) location(s.) The default is to use Home Assistant's location configuration.

Configuration variables

type description
latitude The location's latitude (in degrees.)
longitude The location's longitude (in degrees.)
time_zone The location's time zone. (See the "TZ database name" column at https://en.wikipedia.org/wiki/List_of_tz_database_time_zones.)
elevation The location's elevation above sea level (in meters.)

Entity Namespace

When using the optional entity_namespace configuration parameter, not only will this affect Entity IDs, but it will also be used in creating the entity's friendly_name. E.g., in the configuration show below, the sunrise and sunset entities for London will be named "London Sunrise" and "London Sunset".

Example Full Configuration

sensor:
  - platform: sun2
    monitored_conditions:
      - solar_midnight
      - astronomical_dawn
      - nautical_dawn
      - dawn
      - sunrise
      - solar_noon
      - sunset
      - dusk
      - nautical_dusk
      - astronomical_dusk
      - daylight
      - civil_daylight
      - nautical_daylight
      - astronomical_daylight
      - night
      - civil_night
      - nautical_night
      - astronomical_night
      - azimuth
      - elevation
      - min_elevation
      - max_elevation
      - sun_phase
      - deconz_daylight
      - time_at_elevation: 10
      - time_at_elevation: -10
        direction: setting
        icon: mdi:weather-sunset-down
        name: Setting past 10 deg below horizon
      - elevation_at_time: '12:00'
      - elevation_at_time: input_datetime.test
        name: Elv @ test time
  - platform: sun2
    entity_namespace: London
    latitude: 51.50739529645933
    longitude: -0.12767666584664272
    time_zone: Europe/London
    elevation: 11
    monitored_conditions:
      - sunrise
      - sunset
binary_sensor:
  - platform: sun2
    monitored_conditions:
      - elevation
      - elevation: 3
      - elevation:
          above: -6
          name: Above Civil Dawn