Some time-based template sensors stopped working suddenly

I recently found out that the formatting of some of my template sensors is probably wrong… but in any case, they’ve been working consistently for quite a while. I’m going to start migrating everything into a templates directory, and I’ve confirmed this entry in configuration.yaml is working for me:

template: !include_dir_merge_list templates

Here are the first three sensors in one of the yaml files in that directory:

# formatted date and time
  - sensor:
    - unique_id: date_formatted
      name: Date Formatted
      device_class: timestamp
      state: "{{ now().strftime('%B %d, %Y') }}"
  - sensor:
    - unique_id: time_formatted
      name: Time Formatted
      device_class: timestamp
      state: "{{ now().strftime('%-I:%M %p') }}"
  - sensor:
    - unique_id: day_date_formatted
      name: Day Date Formatted
      device_class: timestamp
      state: "{{ now().strftime('%A, %B %d, %Y') }}"

None of them are working correctly. They are all in state “unknown.” Now… I’ve been told I could do without the - sensor line before each of them, but all of the sensors but these three are working. For example:

  - sensor:
    - unique_id: outside_temperature
      name: Outside Temperature
      device_class: temperature
      unit_of_measurement: "°F"
      state: >
        {% set darkskytemp = states('sensor.dark_sky_temperature') | float %}
        {% set pooltemp = states('sensor.pool_heater_water_temperature') | float %}
        {% if is_state('input_boolean.pool_open', 'on') %}
          {{ darkskytemp }}
        {% else %}
          {{ pooltemp }}
        {% endif %}

This is updating this morning just fine, and has continued to update after I moved it into the templates directory and rebooted.

So this morning, I set about altering each of my yaml files containing binary sensors, template sensors, etc. to the correct format, but the end result was that new entities were created for each of my sensors: sensor.day_date_formatted_2, for example. I’ve got a lot of template sensors, and I really don’t want to go through all of my automations and change every reference. So instead, I figured I’d delete the newly-orphaned original entities and just change the name of each “_2” sensor in the UI… right?

Well… the new “_2” entities were unavailable to edit in the UI:

This entity (‘xxxx.xxxxx’) does not have a unique ID, therefore its settings cannot be managed from the UI. See the documentation for more detail."

So, I’m stuck. In the near term, I want to fix these simple little time & date sensors I have. In the long term, I want to figure out how to “fix” my split-out yaml files in a way that won’t cause me to have to spend several hours renaming things.

Any ideas?

Remove this line:

      device_class: timestamp

A timestamp sensor’s value must contain date and time in ISO format. None of the three Template Sensors reports it in that format so the result is unknown.

Ooof. I was sure I’d done something, and yes, I just went through and added device classes to some of the sensors. I didn’t realize that would affect state. Reloaded those entities and… yes, it works. Thanks!

Any insight into what I should do about my yaml files, or are they fine as-is? All of the sensors work (now).

Is it possible you created the “modern format” Template Sensors before you deleted the “legacy format” Template Sensors? If you create a duplicate of an existing Template Sensor, it’s normal for Home Assistant to append _2 to the duplicate in order to differentiate it from the existing one.

Oh yes, that was the sequence of events… I suppose I shouldn’t be scared of clearing house of all template sensors before creating them in the new format. Backups are there for a reason.