Time of day Sensor and/or Time

Hi all…I have lots of automations triggered off time of day. And for the morning part at least I have the following set up

- platform: tod
  name: MORNING
  after: "05:00"
  before: sunrise

- platform: tod
  name: DAY
  after: sunrise
  before_offset: "-01:00"
  before: sunset

With has worked really well. DAY when that’s triggered turns off some lights and set the heating at sunrise.

Now getting to this time of the year sunrise heads towards 5 am. Can I change the DAY definition to sunrise or 7am - whichever is later.

I assume I might need a separate entity where I can test if sunrise > 7am and if so set DAY at this time.

So I was thinking of:

- platform: tod
  name: DAY_Sunrise
  after: sunrise
  before_offset: "-01:00"
  before: sunset

a binary sensor (DAY_Time) for hour > 7 and < my evening binary sensor.

I’m just trying to work out the template sensor for saying:

if sunrise < 7am then return DAY_Time else return DAY_Sunrise
  - binary_sensor:
    - name: Day
      state: >
        {% set sunrise = (state_attr('sun.sun', 'next_rising')|as_datetime|as_local).strftime("%H:%M") %}
        {% set sunset = (state_attr('sun.sun', 'next_setting')|as_datetime|as_local).strftime("%H:%M") %}
        {% if today_at(sunrise).hour < 7 %}
        {{ today_at("07:00") < now() < today_at(sunset) - timedelta(hours=1) }}
        {% else %}
        {{ today_at(sunrise) < now() < today_at(sunset) - timedelta(hours=1) }}
        {% endif %}

Hey thanks for this. I’m trying to figure out if I can read it and work out what it’s giving me.

next_dawn: 2022-09-13T17:56:37.606156+00:00
next_dusk: 2022-09-13T06:35:22.474396+00:00
next_midnight: 2022-09-13T12:16:09+00:00
next_noon: 2022-09-14T00:16:08+00:00
next_rising: 2022-09-13T18:24:36.632462+00:00
next_setting: 2022-09-13T06:07:19.507226+00:00


  1. get the next rising eg 2022-09-13T18:24:36.632462+00:00 local time, which will be 2022-09-14 06:24ish
  2. get the next sunset eg 2022-09-13T06:07:19.507226+00:00 local which will be 2022-09-13 18:07 ish
  3. If sunrise hours < 7 (true as it’s 6)
  4. (this is the one I can’t follow) - return true if today_at(“07:00”) (= 2022-09-13 07:00?) < sunset - 1 hour = true?

Do I have the positive part of the if right?
What is this returning for me?

I have adjusted the template above. I had failed to take into account the date aspect of the datetime strings… :confused:

The steps of the template are now:

  1. Get the next rising in HH:MM format.
  2. Get the next setting in HH:MM format.
  3. If the sunrise hour < 7.
  4. Then base the state of the binary sensor on the following criteria:
    The current time is between 7:00 and 1 hour before sunset.
  5. Otherwise base the state of the binary sensor on the following criteria:
    The current time is between sunrise and 1 hour before sunset.

Just like your original TOD “Day” sensor this will return “on” during the times that you have defined and “off” otherwise.

Since the template is based off the Sun integration which only exposes the next rising and setting, it’s kind of cumbersome. If you use the Sun2 integration, it can be simplified a bit because Sun2 exposes today’s rising and setting directly as datetime objects.

Using Sun2

{% set sunrise2 = state_attr(‘sensor.sunrise’, ‘today’) %}
{% set sunset2 = state_attr(‘sensor.sunset’, ‘today’) %}
{% if sunrise2.hour < 7 %}
{{ sunrise2 < now() < sunset2 - timedelta(hours=1) }}
{% else %}
{{ sunrise2 < now() < sunset2 - timedelta(hours=1) }}
{% endif%}

thanks will give it ago.

Also I had a look around and found that if I triggered at DAY (today = 06:24) and Time = 07:00 then it triggers twice.

Then use conditions to say binary_sensor.DAY = on and Time After 06:59 then it looks like it has the effect I was after.

When 7 is > sunrise then turn on at 7
When sunrise > 7 then turn on at sunrise.

Thanks for you help though. I’ll enter in those details to a sensor as I’m really inexperienced with template sensors.