Sun Interrogation

The SUN integration has a state below_horizon and above_horizon could this technically mean night and day? as in below_horizon is considered for a better term night? (After sunset and before sunrise)???

Trying to create a template for the night as I have much automations that only fires at night.

Thoughts, suggestions or help? Thanks

I am happy with state below_horizon and above_horizon because it is a nice way to run your automations. Example 1hr after below_horizon is dark (night). It is a nice consistent point that you can always rely on.

1 Like

I have this in my configuration.yaml:

sensor:
  - platform: template
    sensors:
      period_of_day:
        friendly_name: "period of the day"
        value_template: >-
          {% if (as_timestamp(states.sun.sun.attributes.next_dusk) - as_timestamp(states.sun.sun.attributes.next_setting)) < 0 %}
            dusk
          {% elif (as_timestamp(states.sun.sun.attributes.next_rising) - as_timestamp(states.sun.sun.attributes.next_dawn)) < 0 %}
            dawn
          {% elif (states.sun.sun.attributes.elevation) < -4 %}
            night
          {% else %}
            day
          {% endif %}
        icon_template: >-
          {% if (as_timestamp(states.sun.sun.attributes.next_dusk) - as_timestamp(states.sun.sun.attributes.next_setting)) < 0 %}
            mdi:weather-sunset-down
          {% elif (as_timestamp(states.sun.sun.attributes.next_rising) - as_timestamp(states.sun.sun.attributes.next_dawn)) < 0 %}
            mdi:weather-sunset-up
          {% elif (states.sun.sun.attributes.elevation) < -4 %}
            mdi:weather-night
          {% else %}
            mdi:weather-sunny
          {% endif %}

Found it a long time ago on this community :slight_smile:
The format should be updated, I’ll look at this later. No hurry though, the above format still works.

1 Like

Yes that is exactly what it means.

For example to only run your automation at night include the following condition:

condition:
  condition: state 
  entity_id: sun.sun
  state: "below_horizon"
1 Like

When I test the sensor it seams to work, however in the logs I get som errors for the code in the ICON_TEMPLATE.

If I test the code separately in the DEV TOOLS → TEMPLATE, it appears to be ok.

Any ideas?

Is this a bug that should be reported else where?

2022-10-02 11:00:48.300 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'attributes' when rendering '{% if (as_timestamp(states.sun.sun.attributes.next_dusk) - as_timestamp(states.sun.sun.attributes.next_setting)) < 0 %}
mdi:weather-sunset-down
{% elif (as_timestamp(states.sun.sun.attributes.next_rising) - as_timestamp(states.sun.sun.attributes.next_dawn)) < 0 %}
mdi:weather-sunset-up
{% elif (states.sun.sun.attributes.elevation) < -4 %}
mdi:weather-night
{% else %}
mdi:weather-sunny
{% endif %}'

I think this has to do with the fact that the method to create a template-sensor is outdated.
See Template - Home Assistant for the new format.

Updated using the template format:

template:
  - sensor:
      - name: "Period of the day"
        state: >
          {% if (as_timestamp(state_attr('sun.sun', 'next_dusk')) - as_timestamp(state_attr('sun.sun', 'next_setting'))) < 0 %}
            dusk
          {% elif (as_timestamp(state_attr('sun.sun', 'next_rising')) - as_timestamp(state_attr('sun.sun', 'next_dawn'))) < 0 %}
            dawn
          {% elif (state_attr('sun.sun', 'elevation')) < -4 %}
            night
          {% else %}
            day
          {% endif %}
        icon: >
          {% if (as_timestamp(state_attr('sun.sun', 'next_dusk')) - as_timestamp(state_attr('sun.sun', 'next_setting'))) < 0 %}
            mdi:weather-sunset-down
          {% elif (as_timestamp(state_attr('sun.sun', 'next_rising')) - as_timestamp(state_attr('sun.sun', 'next_dawn'))) < 0 %}
            mdi:weather-sunset-up
          {% elif (state_attr('sun.sun', 'elevation')) < -4 %}
            mdi:weather-night
          {% else %}
            mdi:weather-sunny
          {% endif %}