Day/Night sensor reporting Day between non-day times

Hi,

I am having an issue with this Day/Night sensor, it is showing Day between Dusk-Day-Night and Late Night-Day-Dawn

template:
  - sensor:
  ## Day/Night ##
    - name: "Day/Night"
      unique_id: day_night
      state: >
        {% set elevation = state_attr('sun.sun', 'elevation') %}
        {% set rising = state_attr('sun.sun', 'rising') %}
        {% if -6 < elevation < 0 and not rising %}
          Dusk
        {% elif -30 < elevation < -6 and not rising %}
          Evening
        {% elif -45 < elevation < -30 and not rising %}
          Night
        {% elif -50 < elevation < -45 and not rising %}
          Late Night
        {% elif -50 < elevation < -6 and rising %}
          Late Night
        {% elif -6 < elevation < 0 and rising %}
          Dawn
        {% elif 0 < elevation < 45 and rising %}
          Morning
        {% else %}
          Day
        {% endif %}
      icon: >
        {% set elevation = state_attr('sun.sun', 'elevation') %}
        {% set rising = state_attr('sun.sun', 'rising') %}
        {% if -6 < elevation < 0 and not rising %}
          mdi:weather-sunset-down
        {% elif -30 < elevation < -6 and not rising %}
          mdi:weather-night
        {% elif -45 < elevation < -30 and not rising %}
          mdi:weather-night
        {% elif -50 < elevation < -45 and not rising %}
          mdi:weather-night
        {% elif -50 < elevation < -6 and rising %}
          mdi:weather-night
        {% elif -6 < elevation < 0 and rising %}
          mdi:weather-sunset-up
        {% elif 0 < elevation < 45 and rising %}
          mdi:weather-sunset-up
        {% else %}
          mdi:weather-sunny
        {% endif %}
      attributes:
        icon_color: >
          {% set elevation = state_attr('sun.sun', 'elevation') %}
          {% set rising = state_attr('sun.sun', 'rising') %}
          {% if -6 < elevation < 0 and not rising %}
            rgb(217, 143, 0)
          {% elif -30 < elevation < -6 and not rising %}
            rgb(250, 217, 101)
          {% elif -45 < elevation < -30 and not rising %}
            rgb(9, 38, 184)
          {% elif -50 > elevation < -45 %}
            rgb(39,40, 87)
          {% elif -50 < elevation < -6 and rising %}
            rgb(39,40, 87)
          {% elif -6 < elevation < 0 and rising %}
            rgb(255, 168, 0)
          {% elif 0 < elevation < 45 and rising %}
            rgb(255, 249, 0)
          {% else %}
            rgb(255, 249, 0)
          {% endif %}

Should there be some <= or >= somewhere in the jinja?

States are strings. You need to cast the elevation with | float(0). And yes, you have a problem when the value will be exactly 0 — it will then have the value Day. Same for -6, -30 and -45.

Also check out the sun2 custom integration.

So should

      state: >
        {% set elevation = state_attr('sun.sun', 'elevation') %}
        {% set rising = state_attr('sun.sun', 'rising') %}
        {% if -6 < elevation < 0 and not rising %}
          Dusk
        {% elif -30 < elevation < -6 and not rising %}
          Evening
        {% elif -45 < elevation < -30 and not rising %}
          Night
        {% elif -50 < elevation < -45 and not rising %}
          Late Night
        {% elif -50 < elevation < -6 and rising %}
          Late Night
        {% elif -6 < elevation < 0 and rising %}
          Dawn
        {% elif 0 < elevation < 45 and rising %}
          Morning
        {% else %}
          Day
        {% endif %}

be

      state: >
        {% set elevation = state_attr('sun.sun', 'elevation') %}
        {% set rising = state_attr('sun.sun', 'rising') %}
        {% if -6 <= elevation| float(0) <= 0 and not rising %}
          Dusk
        {% elif -30 <= elevation| float(0) <= -6 and not rising %}
          Evening
        {% elif -45 <= elevation| float(0) <= -30 and not rising %}
          Night
        {% elif -50 <= elevation| float(0) <= -45 and not rising %}
          Late Night
        {% elif -50 <= elevation| float(0) <= -6 and rising %}
          Late Night
        {% elif -6 <= elevation| float(0) <= 0 and rising %}
          Dawn
        {% elif 0 <= elevation| float(0) <= 45 and rising %}
          Morning
        {% else %}
          Day
        {% endif %}

You could, but it’s not logically correct. If one range goes up to exactly -6 for example (>=), the other needs to start just after (<). It makes little practical difference in this case though: You’re just covering each gap twice and the first if statement that matches will win, if there might be a second matching case.

Attributes can be numbers, right?

They can and this one is.

Dang, yes. Attributes preserve their type.

Should I just replace

{% set attribute = state_attr('sun.sun', 'attribute') %}

with

{% set attribute = state_attr('sun2.sun2', 'attribute') %}

The sun2 integration provides individual sensors, in line with the move away from sensor attributes. From the integration page for sun2, you can enable additional sensors you need. The name of the sensors also depends on the locations you’ve installed it for (you can have multiple instances of this integration).

I thought the phase sensor might have what you needed, but then I realised I don’t know how you intend to use your sensor. Remember to cast the state value this time, but as correctly pointed out to me, you don’t need to do it for attributes.

As for elevation, it’s sensor.home_sun_elevation in the case of sun2.

As for attributes, you actually shouldn’t be accessing the attribute, due to the move away from attributes. It’s deprecated and will be removed. In my case, I have a sensor.sun_solar_elevation sensor. I think that’s why I made my original cast comment, because my brain though of this individual sensor, without reading more carefully. Here too you’d need to enable additional sensors, as they are disabled by default, either via the integration’s page or entities pages.

Basically, it will be used for my external lighting. I use Adaptive Lighting but to keep the conversation on topic I won’t discuss the automation side of things, just the sensor.

  • Dusk: turn lights on
  • Night: change light brightness
  • Late Night: turn on Adaptive Light Night Mode (Sleep Mode) 2 hours after Night has been activated
  • Dawn: turn off Adaptive Light Night Mode (Sleep Mode)
  • Rising: turn lights off

New sun2 sensor (sort of). Commented sections are from the old sun sensor:

{% set elevation = state_attr('sun2.elevation') %}
{% set dawn = state_attr('sun2.dawn', 'today') %}
{% set rising = state_attr('sun2.rising', 'today') %}
{% set setting = state_attr('sun2.setting', 'today') %}
{% set dusk = state_attr('sun2.dusk', 'today') %}
{% set night = state_attr('sun2.night', 'today') %}
{% if dusk = true %}
  Dusk
{% if setting = true %}
  Evening
{% elif night = true %}
  Night
#{% elif -50 >= elevation <= -45 and not rising %}
#  Late Night
#{% elif -50 >= elevation <= -6 and rising %}
#  Late Night
{% elif dawn = true %}
  Dawn
{% elif 0 < elevation < 45 and rising %}
  Morning
{% else %}
  Day
{% endif %}