Determine if it’s currently daytime with the sun in Home Assistant

This snippet is for a helper that determines if it’s currently daytime based on the sun.

Create a new binary template helper and use this as your template:

{% set next_rising  = as_local( as_datetime( state_attr('sun.sun', 'next_rising' ) ) ) %}
{% set next_setting = as_local( as_datetime( state_attr('sun.sun', 'next_setting' ) ) ) %}
{{ 
not (
  next_rising.date() == now().date()
  or next_setting.date().day == ( now() + timedelta(days=1) ).date().day
)
}}

I use sunrise and sunset for this helper, but you can also use dawn and dusk. Just replace next_rising with next_dawn and next_setting with next_dusk.

Since this helper is a sensor that changes based on the sun states, it can be used in automation triggers like turning on a porch light.

Comments

One response to “Determine if it’s currently daytime with the sun in Home Assistant”

  1. […] the daytime sensor, you can create a template sensor that returns the high (daytime) or low (nighttime) […]

Leave a Reply

Your email address will not be published. Required fields are marked *