Skip to content

Instantly share code, notes, and snippets.

@jceloria
Last active June 23, 2020 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jceloria/ff8cef37dcf97db97782858b111f3f99 to your computer and use it in GitHub Desktop.
Save jceloria/ff8cef37dcf97db97782858b111f3f99 to your computer and use it in GitHub Desktop.
HA sun2 package
---
# #################################################################################################################### #
# #################################################################################################################### #
sensor:
# ------------------------------------------------------------------------------------------------------------------ #
- platform: sun2
entity_namespace: sun2
monitored_conditions:
- solar_noon
- elevation
binary_sensor:
# ------------------------------------------------------------------------------------------------------------------ #
- platform: sun2
entity_namespace: sun2
monitored_conditions:
- elevation
- elevation: 30
- elevation:
above: -12
- elevation:
above: -18
- platform: template
sensors:
sun_rising:
value_template: >-
{{ as_timestamp(states("sensor.date_time_iso")) <
as_timestamp(state_attr('sensor.sun2_solar_noon', 'today')) }}
# Sun is between nautical dawn (-12º) and 30º whilst rising
morning:
value_template: >-
{{ is_state('binary_sensor.sun2_above_minus_12_0', 'on') and
is_state('binary_sensor.sun2_above_30_0', 'off') and
is_state('binary_sensor.sun_rising', 'on') }}
# Sun is above 30º and rising
day:
value_template: >-
{{ is_state('binary_sensor.sun2_above_30_0', 'on') and
is_state('binary_sensor.sun_rising', 'on') }}
# Sun is above 30º and falling
afternoon:
value_template: >-
{{ is_state('binary_sensor.sun2_above_30_0', 'on') and
is_state('binary_sensor.sun_rising', 'off') }}
# Sun is below 30º and falling
evening:
value_template: >-
{{ is_state('binary_sensor.sun2_above_30_0', 'off') and
is_state('binary_sensor.sun2_above_minus_18_0', 'on') and
is_state('binary_sensor.sun_rising', 'off') }}
# Sun is between astronomical dusk (-18º) and nautical dawn (-12º)
night:
value_template: >-
{% if is_state('binary_sensor.sun_rising', 'off') %}
{{ is_state('binary_sensor.sun2_above_minus_18_0', 'off') }}
{% else %}
{{ is_state('binary_sensor.sun2_above_minus_12_0', 'off') }}
{% endif %}
automation:
# ------------------------------------------------------------------------------------------------------------------ #
- alias: Sun position
initial_state: true
trigger:
- platform: state
entity_id:
- binary_sensor.morning
- binary_sensor.day
- binary_sensor.afternoon
- binary_sensor.evening
- binary_sensor.night
action:
- service: notify.john
data_template:
message: >-
{% set sun = (states('sensor.sun2_elevation')|float) %}
{% set name = state_attr(trigger.entity_id, 'friendly_name') %}
{% set from = trigger.from_state.state %}
{% set to = trigger.to_state.state %}
{% set modes = [
states.binary_sensor.morning, states.binary_sensor.day, states.binary_sensor.afternoon,
states.binary_sensor.evening, states.binary_sensor.night
] %}
{{ name }} has just changed from {{ from }} to {{ to }}, the sun is at {{ sun }}.
{%- for i in modes -%}
{%- if loop.first %}{% elif loop.last %} and {% else %}, {% endif -%}
{{ i.name }} is {{ i.state }}
{%- endfor %}. input_select.house_mode is {{ states('input_select.house_mode') }}.
# #################################################################################################################### #
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment