Custom sun sensor with independent elevations for sunset and sunrise

Sometimes the elevation that fits the sunset isn’t just right for sunrise. For those situations, as the title describes

sensor:
  #My Sun
  - platform: template
    sensors:
      my_sun:
        entity_id: sun.sun
        value_template: >
          {% if is_state_attr('sun.sun', 'rising', True) and state_attr('sun.sun', 'elevation')|float > -4.4 %}
            above_horizon
          {% if is_state_attr('sun.sun', 'rising', False) and state_attr('sun.sun', 'elevation')|float > -1.5 %}
            above_horizon
          {% else %}
            below_horizon
          {% endif %}

this is the same

sensor:
  #My Sun
  - platform: template
    sensors:
      my_sun:
        entity_id: sun.sun
        value_template: >
          {% if (is_state_attr('sun.sun', 'rising', True) and state_attr('sun.sun', 'elevation')|float > -4.4 ) or (is_state_attr('sun.sun', 'rising', False) and state_attr('sun.sun', 'elevation')|float > -1.5 )%}
            above_horizon
          {% else %}
            below_horizon
          {% endif %}

you can even put weather conditions (cloudy, partly-cloudy, etc), openweathermap updates every 10 minutes I believe and it’s fairly accurate

The first one does not parse but the second one helped me a great deal automating loads when the sun comes over the hills. I took out the - for that and adjusted the values.
Thank you so much for sharing your work!

1 Like