Home Assistant Period of the Day
Here’s a quick template sensor that I found on the interwebs that’s really useful for automations. Basically the sensor translates Home Assistant’s sun component into dusk, dawn, night, or day. It also shows a corresponding icon. We’ve found it to be very useful for conditions in automations. This requires that you have your correct latitude and longitude in your configuration.yaml.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
- 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 %} |
To get it to show up in your UI, just add – sensor.period_of_day to your ui-lovelace.yaml file (if you’ve made the switch to lovelace).