Time of day sensor not rolling over to next day

I’m trying to create a sensor for labelling the time of day (morning, afternoon, evening, night). Here’s what I have:

- platform: template
  sensors:
    time_of_day:
      friendly_name: "Time of Day"
      value_template: >
        {% if as_timestamp(now()) < as_timestamp(states.sensor.date.state ~ ' 06:00:00') %}
          night
        {% elif as_timestamp(now()) < as_timestamp(states.sensor.date.state ~ ' 12:00:00') %}
          morning
        {% elif as_timestamp(now()) < as_timestamp(states.sensor.date.state ~ ' 18:30:00') %}
          afternoon
        {% elif as_timestamp(now()) < as_timestamp(states.sensor.date.state ~ ' 22:30:00') %}
          evening
        {% elif as_timestamp(now()) >= as_timestamp(states.sensor.date.state ~ ' 22:30:00') %}
          night
        {% else %}
          undefined
        {% endif %}

It works correctly in the template editor developer tool, and also when I restart Home Assistant. However, it gets stuck on ‘night’, I’m guessing this is because, once the time passes 22:30:00 the date state then always appears to be > ‘22:30:00’.

If this is the cause of the issue, how can I modify the template to recognise a new day?

maybe if you DELETE the last elif statement and put night in the else statement (instead of undefined)