Adding !include to existing entries in configuration.yaml

I have quite a few Template sensor entries in my configuration.yaml for various things.

The majority are for monitoring my electricity use from the solar PV set-up. I was hoping to be able to separate these from all the other template sensors for the sake of clarity… but merge them in the main file via !include.

What I tried without success - was to setup
template: !include template-power.yaml

The file template-power.yaml would include something like:

      - name: Grid Power TEST
        state_class: measurement
        icon: mdi:transmission-tower
        unit_of_measurement: "kW"
        device_class: power
        state: >
          {{ ([0, states('sensor.consumption_power') | float(0) - 
                states('sensor.solar_power') | float ] | max) | round(3)}}

The main configuration.yaml would include something like:

template: !include template-power.yaml

template:
  - sensor:
      - name: SunTriggerStudy
        state_class: measurement
        icon: mdi:blind
        state: >
          {{(state_attr('sun.sun','elevation')) - (atan(2250/(1150/(sin((pi/180)*((state_attr('sun.sun','azimuth')-8.5))))))*(180/pi))}}

and a whole range of unrelated sensor entries. etc etc etc. I got a duplication error (too many “template” entries.
I also tried:

template:
  - sensor:
      !include template-power.yaml

(and then all the rest of the template sensors)

Is this merge possible or do I need to look at other solutions?

There are some solutions here: https://community.home-assistant.io/t/splitting-config-for-template/328636

Probably best to skip to the end of the topic and read back as there are a lot of dead ends in the beginning.

1 Like

Tks

Petro’s suggestion seems to be the closest to what I am thinking:

I just need to test this somehow without breaking my HA (no test version - only production)

After reading through that thread, it would appear you either have all template sensor entries in the configuration.yaml file or you follow Petro’s format. That is, break all of them up in separate files in a separate folder and use:

template: !include_dir_merge_list template

You cannot split off some entries into a separate file and merge them into the main config file with the rest of the template entries.

So in my case, I would need one file say called “energy.yaml” and then lump all other template entries into “other.yaml”. They would be put in a folder called “template” and the main config file would include the “template: !include_dir_merge_list template” command.