My setup is very much like @tom_l described. I actually modeled mine off of @frenck where all integrations are loaded from a folder that then points to an entity folder with subfolders for each type of entity and even sub-sub folders for templates.
So for example, regarding your question:
configuration.yaml
homeassistant:
customize: !include customize.yaml
packages: !include_dir_named integrations
And in the ‘integrations’ folder I have all my YAML files for the various integrations I do. One is for templates:
template.yaml
template: !include_dir_list ../entities/templates/sensors
Which I create folders of YAML files that I organize categorically (i.e., a folder called Weather for my weather template sensors).
This is where I differ a bit from others, I throw all my templates into the “sensors” folder (which was probably an oversight when I first set it up) because it was when there was a transition to the newer way of doing templates, so I wanted to break out the new versus the old. You are probably better off doing what Tom described and putting each sensor type into its own folder. Mine works the same but reads in a confusing way.
Once there, the templates themselves follow this format inside their own YAML:
sensor:
- name: "Downstairs CO2 Health"
unique_id: 13fe52a0-15a1-4d5b-956c-00114943d642
state: >-
{% set co2 = float(states('sensor.basement_air_quality_carbon_dioxide'), 0) %}
{% set level = "Normal" %}
{% if co2 >= 800 and co2 < 1000 %}
{% set level = "Moderate" %}
{% elif co2 >= 1000 %}
{% set level = "High" %}
{% endif %}
{{ level }}
icon: mdi:brain
The thing that often throws people is that they want to copy-paste the documented examples or examples posted here, but once you nest everything into packages you don’t start your template the same.
You start at the domain and work down:
sensor:
- name: "Downstairs CO2 Health"
Instead of:
template:
- sensor:
- name:
...
It can get confusing to wrap your head around at first, but when you do packages then you’ve already created the dictionary so you don’t have to indent or prepend a dash in most cases (depending on how you include the file(s)).
It was a bit confusing at first but now that I’m used to it I love it this way rather than having to dig through hundreds or thousands of lines in a single YAML, plus it makes it really simple to add or remove integrations or entities on the fly.