Placing templates into their own directory

I have a few yaml files that tell HA where to locate certain files. For example:

script: !include_dir_merge_named …/scripts
binary_sensor: !include_dir_list …/entities/binary_sensors
input_boolean: !include_dir_merge_named …/entities/input_booleans

What I need is to start putting templates into their own directory. Is there a format like above to direct HA where to locate templates? I’m trying to add some entity cards for weather, but the entities are in meters/sec. These templates convert meters/sec to MPH, and I would like to reference these entities so they can display imperial units instead.

1 Like

it seems as if you want to use those templates as macros. If so then see this section of the docs:

Not necessarily as macros, but a conversion from one unit to another; defined as a unique sensor:

template:
  - sensor:
      - name: openweathermap_wind_speed_mph
      - unit_of_measurement: "mph"
      - value_template: "{{ (states('sensor.openweathermap_wind_speed')| float * 2.23694) | round(2) }}"

  - sensor:
      - name: wind_gust_mph"
      - unit_of_measurement: "mph"
      - value_template: "{{ (states('sensor.wind_gust')| float * 2.23694) | round(2) }}"

OK so you just want to split out your “template:” domain sensors into their on file/folder?

If so then it works just like splitting out any other top level key in your config.

Put this in the same location as you did your above examples:

template: !include_dir_merge_list template_sensors/

then create a folder named “template_sensors” and put those sensor yaml configs in files in there.

I added template: !include_dir_merge_list ../entities/templates and placed the template file there but that did not add the sensor. I had to change the code, then move it into the Integrations directory which is where packages are stored (packages: !include_dir_named integrations).

Now, it appears I can only have one sensor per yaml file. Otherwise, none of the sensors load. Is there a way I can add multiple conversion sensors into one file? Here is the single sensor that now works in the “integrations” folder:

sensor:
  - platform: template
    sensors:
      openweathermap_wind_speed_mph:
        friendly_name: "Openweathermap_wind_speed_mph"
        unit_of_measurement: 'mph'
        value_template: "{{ states('sensor.openweathermap_wind_speed') | float * 2.23694 | round(2) }}"

In my configuration.yaml, I use:

template: !include_dir_merge_list ./templates/

Then I use a file structure like this to keep it all sorted:

You are trying to put the legacy template sensor config into the “template:” domain key. And you are also mixing up the syntax. Modern template sensors don’t use “value_template”. they use “state” instead.

in the first example above you have:

template:
  - sensor:
      .
      .

that is the modern format template sensor syntax.

in the example you just posted you are using the legacy template syntax:

sensor:
  - platform: template
    sensors:
      .
      .

the reason it works in the packages is because packages allow every top level key to be used in the package. So “sensor:” works there. You could have also put your (syntax corrected) “template:” sensor version there and it would have worked there as well.

your config structure should look like this:

config/
  |
  -> configuration.yaml
  |
  -> entities/
      |
      - binary_sensors/
      |
      -> templates/
          |
          -> some_file_name.yaml

put this into the “some_file_name.yaml” and it should work.:

  - sensor:
      - name: openweathermap_wind_speed_mph
      - unit_of_measurement: "mph"
      - state: "{{ (states('sensor.openweathermap_wind_speed')| float * 2.23694) | round(2) }}"

  - sensor:
      - name: wind_gust_mph"
      - unit_of_measurement: "mph"
      - state: "{{ (states('sensor.wind_gust')| float * 2.23694) | round(2) }}"

Never understood an intention to store entities dependently on their domains.
i.e. templates in one folder, command_line in another.
Suggest to organize your config for “tasks” instead of domains.
Like here.

For instance - “audi_q7.yaml” keeps all entities & automations related to your car.
Or (better in some cases) - a folder “audi_q7” has files “audi_q7_xxxx.yaml” && “audi_q7_yyyy.yaml” with a similar approach for separate tasks.
Easy, organized, manageable.
Moved Audi Q7 to another household - just move the whole folder to another HA setup.

Different minds organize differently. :raccoon:

1 Like

Ofc, do not insist )

1 Like

So therein lies the problem. I pasted your code into a new file under /config/entities/templates, and I lose the sensors again. Pasting it into the template editor, Jinja says its ok, and its returns the correct values:
image

But the sensors/entities don’t load. Using the modern format, I changed the code as such. This does not work:

template:
  - sensor:
      - name: openweathermap_wind_speed_mph
      - unit_of_measurement: "mph"
      - state: "{{ (states('sensor.openweathermap_wind_speed')| float * 2.23694) | round(2) }}"

  - sensor:
      - name: wind_gust_mph"
      - unit_of_measurement: "mph"
      - state: "{{ (states('sensor.wind_gust')| float * 2.23694) | round(2) }}"

It doesn’t matter if I have the file in the /config/entities/templates folder or the /config/integrations folder. It seems if value_template isn’t used, and when placed in the Integrations (packages) directory, neither sensor loads. Reverting the code like this, coupled with moving the file back into the Integrations directory, both sensors then load.

sensor:
  - platform: template
    sensors:
      openweathermap_wind_speed_mph:
        friendly_name: "Openweathermap_wind_speed_mph"
        unit_of_measurement: 'mph'
        value_template: "{{ states('sensor.openweathermap_wind_speed') | float * 2.23694 | round(0) }}"
  - platform: template
    sensors:
      wind_gust_mph:
        friendly_name: "wind_gust_mph"
        unit_of_measurement: 'mph'
        value_template: "{{ states('sensor.wind_gust') | float * 2.23694 | round(0) }}"

It’s really hard to know exactly what the problem is since you aren’t putting in all the details of where exactly you put the code listed above. and what your file structure really looks like. And what the !include command looks like.

however the (or at least one ) issue with the code you posted is that I steered you wrong so I’m sorry for that. It is putting hyphens (-) in places where it shouldn’t. You had those in your code above and unfortunately I just copy/pasted them without realizing it (I did it when I was half asleep. :wink:)

here is the correct version:

template:
  - sensor:
      - name: openweathermap_wind_speed_mph
        unit_of_measurement: "mph"
        state: "{{ (states('sensor.openweathermap_wind_speed')| float * 2.23694) | round(2) }}"

      - name: wind_gust_mph"
        unit_of_measurement: "mph"
        state: "{{ (states('sensor.wind_gust')| float * 2.23694) | round(2) }}"

the issue with that is that the code you put into the TEMPLATE editor isn’t a template. and it’s not Jinja. It’s yaml. So the template editor won’t tell you anything about the code being correct.

it works in packages with the legacy format because that is the correct syntax for that type of sensor in a package.

@finity thank you for the adjusted code. Both of the sensors work if I have it in the Integrations folder. So, for now, I think I’ll stick with keeping it there. The idea was to keep adding on as needed, and it looks like I can do that now.

I started separating by domain because a few enthusiasts I follow also worked in this way, and it was easier for me to mimic that format pulling from github to implement some of their ideas. It quickly became too labor-intensive to try (and fail) to translate it into what I had.

The structure I have is:
Automations: /automations
Binary Sensors: /entities/binary_sensors
Device Trackers: /entities/device_trackers
Groups: /entities/groups
Notify: /entities/notify
Packages: /integrations
Scenes: /scenes
Scripts: /scripts
Themes: /themes

Sticking with your current structure format you should be able to create a folder called “templates” under your “entities” folder (so /entities/templates).

then in that folder create some yaml file (we’ll call it “weather.yaml”).

then in your configuration.yaml add an !include to that folder:

template: !include_dir_merge_list entities/template_sensors/

then in the “weather.yaml” file put this in it:

  - sensor:
      - name: openweathermap_wind_speed_mph
        unit_of_measurement: "mph"
        state: "{{ (states('sensor.openweathermap_wind_speed')| float * 2.23694) | round(2) }}"

      - name: wind_gust_mph"
        unit_of_measurement: "mph"
        state: "{{ (states('sensor.wind_gust')| float * 2.23694) | round(2) }}"

and all of that should work. You need to make sure you keep the indentation just as I have it in the code posted above.

Just to be sure I changed my config to match exactly as I told you and everything worked as expected.

image

The second entity shows as “unavailable” because I don’t have the “sensor.wind_gust” anywhere in my config. If I did it would work just like the first one.