So, I’ve set up a template sensor that defines the various times of the day according to my preferences. It mostly uses the sun2 integration and the current time. I use this to trigger various other things, such as lights and curtains, but I was wondering if there is a way to get the various states it supports in the state dropdown in the automation section. I tried using the option attribute, but apparently that’s not correct for template sensors…
- platform: template
sensors:
time_of_day:
friendly_name: "Time of day"
unique_id: '3a2df494-95c6-441c-b16b-9bb1fdff33f9'
value_template: >
{% set time = now().strftime('%Y-%m-%dT%H:%M:%S%z') %}
{% set startofday = now().strftime('%Y-%m-%d') + 'T00:00:00' + now().strftime('%z') %}
{% set sunrisevaxjo = states('sensor.vaxjo_sunrise_2') | as_datetime | as_local %}
{% set sunrise = (as_timestamp(sunrisevaxjo) + 3600) | timestamp_custom('%Y-%m-%dT%H:%M:%S%z') %}
{% set earliestdawn = now().strftime('%Y-%m-%d') + 'T05:00:00' + now().strftime('%z') %}
{% set solarnoonvaxjo = states('sensor.vaxjo_solar_noon') | as_datetime | as_local %}
{% set solarnoon = (as_timestamp(solarnoonvaxjo) + 3600) | timestamp_custom('%Y-%m-%dT%H:%M:%S%z') %}
{% set sunsetvaxjo = states('sensor.vaxjo_sunset_2') | as_datetime | as_local %}
{% set dusk = (as_timestamp(sunsetvaxjo) - 3600) | timestamp_custom('%Y-%m-%dT%H:%M:%S%z') %}
{% set sunset = (as_timestamp(sunsetvaxjo)) | timestamp_custom('%Y-%m-%dT%H:%M:%S%z') %}
{% set endofday = now().strftime('%Y-%m-%d') + 'T23:59:59' + now().strftime('%z') %}
{% set startofday = now().strftime('%Y-%m-%d') + '00:00:00' + now().strftime('%z') %}
{% if (startofday <= time < earliestdawn) %}
Night
{% elif (earliestdawn <= time < sunrise) %}
Dawn
{% elif (sunrise <= time < solarnoon) %}
Morning
{% elif (solarnoon <= time < dusk) %}
Afternoon
{% elif (dusk <= time < sunset) %}
Dusk
{% elif (sunset <= time <= endofday) %}
Evening
{% else %}
Unknown
{% endif %}