In my automations, mostly to control lights, I have to set two conditions in which one is “before sunrise” and another is “after sunset” to define something so trivial as “at night”.
Not perfect but
condition: state
entity_id: sun.sun
state: below_horizon
You could use one condition, that being ‘below_horizon’
What you consider night might be different to what I consider night so the above works best and takes into account the seasons.
You could also if you want more control, use a time based sensor:
Thea above two options should help some and achieve a day / night sensor or condition that works for you.
Following the “technical” definitions, there are at least 4 types of night.
- Sun below horizon
- Between Civil Twilight and Civil Dawn
- Between Nautical Twilight and Nautical Dawn
- Between Astronomical Twilight and Astronomical Dawn
Add to that personal, cultural, and/or legal/governmental definitions… then a one-size-fits-all “at night” condition becomes a lot less trivial.
This is my sensor for this:
- name: Time of the day status
state: >-
{% set sun_angle = state_attr('sun.sun', 'elevation') %}
{% if sun_angle in ['unknown','undefined','none'] %}
unknown
{% elif sun_angle | float(0) < -6 %}
night
{% elif sun_angle | float(0) < 0 %}
twilight
{% else %}
day
{% endif %}
icon: >-
{% if is_state("sensor.time_of_the_day_status", "night") %}
mdi:weather-night
{% elif is_state("sensor.time_of_the_day_status", "day") %}
mdi:weather-sunny
{% elif is_state("sensor.time_of_the_day_status", "twilight") %}
{% if is_state_attr('sun.sun', 'rising', true) %}
mdi:weather-sunset-up
{% else %}
mdi:weather-sunset-down
{% endif %}
{% else %}
mdi:help
{% endif %}
So I use this to set some automations to run only at night or twilight, etc.
Basically I’m using the civil night.
And I use two different conditions/helpers - one for “night” based on the start of local sunset, the other for “dark”, based on a luminance sensor.
And this is mine:
sensor:
- platform: template
sensors:
# Period of the day
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) < 0 %}
night
{% else %}
day
{% endif %}
icon_template: >-
{% if is_state('sun.sun', 'above_horizon') %}
mdi:weather-sunny
{% else %}
mdi:weather-night
{% endif %}
making dawn, day, dusk and night available 🥹
I liked your sensor name more…
It’s better with 4 icons.
I already tested the same kind of template found on the forum, but I get a quick night state between dawn and day…
I’ll try with elevation instead of above_horizon.
Lots of productive options here, so I’ll throw mine in there too lol. Simply an input_boolean
connected to an automation triggered by sun.sun
.
I didn’t like time based schedule as an option because for me “night” changes. I created a toggle for “night mode” then used an offset time from sunset and sunrise to flip it automatically. It’s a toggle so I can turn it on or off manually. For example I may get up early and my eyes are adjusted to the dark. In that case I may want to watch the sun rise rather then have a lamp pop on.
A night example but can be different for others
a binary sensor with
- platform: tod
name: Night
after: sunset
before: sunrise
deconz have also a good virtual entity for those things