Template sensor not working for me

I have been sitting with this template far to long now… what am I doing wrong? Its just using sun.sun and sensor.time… it cannot be that complicated : )

  - platform: template
    sensors:
      timeofday:
        friendly_name: "Time of day"
        value_template: >
          {% if (states.sun.sun.attributes.elevation | float > -2.0) and (states.sun.sun.attributes.elevation | float < 4) and (states.sensor.time.state < 12:00)%}sunrise
          {% elif (states.sun.sun.attributes.elevation | float > 4.0) and (states.sensor.time.state < 09:00)%}morning
          {% elif (states.sun.sun.attributes.elevation | float > 4.0) and (states.sensor.time.state > 09:00) and (state.sensor.time.state < 19:00)%}day
          {% elif (states.sun.sun.attributes.elevation | float > 4.0) and (states.sensor.time.state > 19:00)%}evening
          {% elif (states.sun.sun.attributes.elevation | float > -2.0) and (states.sun.sun.attributes.elevation | float < 4) and (states.sensor.time.state > 12:00)%}sunset
          {% else %}night
          {% endif %}

Copy and paste your template in Dev Tools/templates and you will see what you get.
The problem are the states.sensor.time.state > 09:00
-> TypeError: ‘>’ not supported between instances of ‘str’ and ‘int’
Use now().strftime('%H') | int > 9 instead.

ok, thanks alot.
Seems that putting “09:00”, also soved it.

1 Like

This is something that i didn’t know. :slightly_smiling_face:

I offer you this revised version.

  • By using variables it avoids duplicated processing and enhances legibility.
  • By specifying entity_id: sensor.time it ensures the template sensor is evaluated once every minute.
  - platform: template
    sensors:
      timeofday:
        friendly_name: "Time of day"
        entity_id: sensor.time
        value_template: >-
          {% set elev = state_attr('sun.sun', 'elevation') %}
          {% set hour = now().hour %}
          {% if elev > -2 and elev < 4 and hour < 12 %} sunrise
          {% elif elev > 4 and hour < 9 %} morning
          {% elif elev > 4 and hour >= 9 and hour < 19 %} day
          {% elif elev > 4 and hour >= 19 %} evening
          {% elif elev > -2 and elev < 4 and hour >= 12 %} sunset
          {% else %} night
          {% endif %}

Confirmed it works on my system.

how about using TOD binary sensor(s)?

@123, in the time that i wrote my first responce, i thought

100% sure, @123 provides a better solution

:slightly_smiling_face:
I’ve never seen a profile picture that fits better to a user!
That must be said!
Thanks for this!

You’re welcome!

Here’s an even more compact version of the template. However, it might become less legible for some because it uses this convention:

-2 < elev < 4

where elev is greater than -2 yet less than 4. You could read it like “minus two is less than elev and elev is less than four”.

      {% if -2 < elev < 4 and hour < 12 %} sunrise
      {% elif elev > 4 and hour < 9 %} morning
      {% elif elev > 4 and 9 <= hour < 19 %} day
      {% elif elev > 4 and hour >= 19 %} evening
      {% elif -2 < elev < 4 and hour >= 12 %} sunset
      {% else %} night
      {% endif %}