Time template question

hello there!

i’ve set up template sensor for tracking which part of the day is - morning, day, evening, night.
all is perfect besides timeperiod 23.00-23.59 when sensor just don’t know what part to use. i’ve been trying to change timezone in HA - same result: between 23.00-23.59 sensor showing blank value. what might be problem here?
code:
{% if states.sensor.time.state > ‘08.01’ and states.sensor.time.state < ‘12.00’ %}
Hommik
{% elif states.sensor.time.state > ‘12.01’ and states.sensor.time.state < ‘19.00’ %}
Päev
{% elif states.sensor.time.state > ‘19.01’ and states.sensor.time.state < ‘23.59’ %}
Õhtu
{% elif states.sensor.time.state > ‘00.00’ and states.sensor.time.state < ‘02.00’ %}
Õhtu
{% elif states.sensor.time.state > ‘02.01’ and states.sensor.time.state < ‘08.00’ %}
Öö
{% endif %}

Try this

  • platform: template
    sensors:
    time_of_day:
    friendly_name: ‘Time of Day’
    value_template: >-
    {% if states.sun.sun.state == ‘above_horizon’ and as_timestamp(now()) < as_timestamp(states.sensor.date.state ~ ’ 12:00:00’) %}
    morning
    {% elif states.sun.sun.state == ‘above_horizon’ and as_timestamp(now()) >= as_timestamp(states.sensor.date.state ~ ’ 12:00:00’) %}
    afternoon
    {% elif states.sun.sun.state == ‘below_horizon’ and states.sensor.date.state == states.sun.sun.attributes.next_rising | truncate(10, True, ‘’) %}
    twilight
    {% elif states.sun.sun.state == ‘below_horizon’ and as_timestamp(now()) < as_timestamp(states.sensor.date.state ~ ’ 17:00:00’) %}
    early_evening
    {% elif states.sun.sun.state == ‘below_horizon’ and as_timestamp(now()) < as_timestamp(states.sensor.date.state ~ ’ 22:00:00’) %}
    evening
    {% elif states.sun.sun.state == ‘below_horizon’ and as_timestamp(now()) >= as_timestamp(states.sensor.date.state ~ ’ 22:00:00’) %}
    late_evening
    {% endif %}

will try. thanks. can you please explain why my approach is wrong?

Try this, it worked on my installation (and yours actually didn’t):

{% if states.sensor.time.state > '08:01' and states.sensor.time.state < '12:00' %}
Hommik
{% elif states.sensor.time.state > '12:01' and states.sensor.time.state < '19:00' %}
Päev
{% elif states.sensor.time.state > '19:01' and states.sensor.time.state < '23:59' %}
Õhtu
{% elif states.sensor.time.state > '00:00' and states.sensor.time.state < '02:00' %}
Õhtu
{% elif states.sensor.time.state > '02:01' and states.sensor.time.state < '08:00' %}
Öö
{% endif %}

Basically change the . by : (makes more sense for time, no?)

2 Likes

thank you!

1 Like