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.
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.
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 %}