Create TOD sensor problem

In configuration.yaml, this works great and it changes properly thru the day.

binary_sensor:
 - platform: tod
   name: Morning
   after: sunrise
   before: "11:00"
 
 - platform: tod
   name: Afternoon
   after: "11:01"
   before: sunset
   before_offset: "-01:00"
 
 - platform: tod
   name: Evening
   after: sunset
   after_offset: "-01:00"
   before: "23:00"
 
 - platform: tod
   name: Goodnight
   after: "23:01"
   before: sunrise

I put this next code into my sensors.yaml to create a sensor based on this and it doesn’t show up. What’s weird is i finally got it to work then couple hours later I looked at nope no sensor again. Restarted, readded, etc and it just won’t show up again. Why did it work once, then not again??

- platform: template
  sensors:
    time_of_day:
      friendly_name: "Time of Day"
      value_template: >-
        {% if is_state('binary_sensor.morning','on') %}
          Morning
        {% elif is_state('binary_sensor.afternoon','on') %}
          Afternoon
        {% elif is_state('binary_sensor.evening','on') %}
          Evening
        {% elif is_state('binary_sensor.goodnight','on') %}
          Goodnight
        {% endif %}

I think you need to follow the yaml syntax described here:

Template sensor

I attempted, not working for me. What I had listed above was from a youtube video on creating TOD and again somehow it worked and now doesn’t. I also tried moving it to configuration.yaml. I guess I just don’t get it.

Your YouTube source is probably out of date, and as a consequence you are mixing bits of the modern template format with the legacy format (now deprecated)

As templeton_nash says, you are better off following the documentation rather than YouTube content which can get outdated. Maybe try this (not tested):

template:
   - sensor:
      - name: "Time of Day"
        state: >-
         {% if is_state('binary_sensor.morning','on') %}
etc

(Apologies for multi edits - I kept getting it wrong myself!)

Yea, that was throwing me off alot. I think i got it, i have the sensor now but it has no value. I assumed it would read the current value but maybe it will not populate till the next change happens. Shall see. Thanks.