Trouble placing templates into own file

Hello,

Apologies for opening yet another thread for what appears to be a so simple topic - but it’s driving me nuts .

I have two sensors like this. Placing the definition into configuration.yaml works as desired:

template:
  - sensor:
      - name: "Schicht heute"
        unique_id: schicht_heute
        icon: mdi:calendar-today
        state: >
          {% set start = strptime('2026-02-09', '%Y-%m-%d') %}
          {% set today = now().replace(hour=0, minute=0, second=0, microsecond=0) %}
          {% set delta_days = ((as_timestamp(today) - as_timestamp(start)) / 86400) | int %}
          {% set idx = ((delta_days % 42) + 42) % 42 %}
          {% set shifts = [
            "Tag","Tag","Tag","Tag","Tag",
            "Frei","Frei",
            "Früh","Früh",
            "Spät","Spät",
            "Nacht","Nacht","Nacht",
            "Frei","Frei","Frei","Frei",
            "Früh","Früh","Früh",
            "Spät","Spät",
            "Nacht","Nacht",
            "Frei","Frei","Frei","Frei","Frei",
            "Früh","Früh",
            "Spät","Spät","Spät",
            "Nacht","Nacht",
            "Frei","Frei","Frei","Frei","Frei"
          ] %}
          {{ shifts[idx] }}

So I wanted to move this and other stuff into a separate file schicht.yaml. The reference in configuration.yaml is

template: !include_dir_merge_named templates/

The content of the file is like the template definition, except for “template”. But now the sensor gets status “unavailable”.

sensor:
  - name: "Schicht heute"
    entity_id: schicht_heute
    icon: mdi:calendar-today
    state: >
      {% set start = strptime('2026-02-09', '%Y-%m-%d') %}
      {% set today = now().replace(hour=0, minute=0, second=0, microsecond=0) %}
      {% set delta_days = ((as_timestamp(today) - as_timestamp(start)) / 86400) | int %}
      {% set idx = ((delta_days % 42) + 42) % 42 %}
      {% set shifts = [
        "Tag","Tag","Tag","Tag","Tag",
        "Frei","Frei",
        "Früh","Früh",
        "Spät","Spät",
        "Nacht","Nacht","Nacht",
        "Frei","Frei","Frei","Frei",
        "Früh","Früh","Früh",
        "Spät","Spät",
        "Nacht","Nacht",
        "Frei","Frei","Frei","Frei","Frei",
        "Früh","Früh",
        "Spät","Spät","Spät",
        "Nacht","Nacht",
        "Frei","Frei","Frei","Frei","Frei"
      ] %}
      {{ shifts[idx] }}

WTH am I doing wrong?

I do a similar thing for automations.

Try something like this instead.

automation: !include_dir_merge_list automation/

Thank you. Unfortunately no change, the sensors remain unavailable.

The file gets loaded when I remove the merge

template: !include_dir_named templates

But this throws

Invalid config for ‘template’ at templates/schicht.yaml, line 1: ‘schicht’ is an invalid option for ‘template’, check: schicht Invalid config for ‘template’ at templates/schichtzeiten_sensor.yaml, line 1: ‘schichtzeiten_sensor’ is an invalid option for ‘template’, check: schichtzeiten_sensor

template: !include_dir_list templates 

throws no error, but still no active sensors.

OK, I solved it.

The include needs to reference list

template: !include_dir_merge_list templates/

The sensor definition in the files requires the following structure:

- sensor:
    - default_entity_id: sensor.schicht_heute
      name: "Schicht heute"
      unique_id: unique_id_schicht_heute
      icon: mdi:calendar-today
      state: >
      ...
1 Like