Unable to add 2 template switch yaml files to config

Hi - I am trying to add 2 different templates to create switches in the configuration file, but I can’t seem to get them to work together. The last reference always seems to overwrite the previous one. I have tried various methods to do this, but must be doing something wrong with the syntax. Can anyone help?

Current version is below

switch:
- platform: template
  switches: !include  switch_template_switch_skytv.yaml
  switches: !include  skyqExtension.yaml

In YAML a key is only allowed once per parent element.
Later keys will always overwrite previous ones.

You have to place the includes differently:

switch:
- platform: template
  switches:
    name1: !include  switch_template_switch_skytv.yaml
    name2: !include  skyqExtension.yaml

Now you will have two different keys which are not overwriting each other.

Or you use the advanced feature to include all yamls of a specific folder like documented here:

switch:
- platform: template
  switches: !include_dir_named switch-templates/

Each filename will contain the name of the switch then (e.g. name1 or name2 like above).

Thanks @Daxi. I tried using the syntax you suggested but I got a configuration error

Invalid config for [switch.template]: [switch_skytv] is an invalid option for [switch.template]. Check: switch.template->switches->switch1->switch_skytv. (See ?, line ?).

The root element at the included yaml is switch_skytv, right?
Remove the root element and name the file switch_skytv.
The filename will be the switch name, the switch include file can only contain the child elements.

For example:

all in one:

switch:
- platform: template
  switches:
    switch_skytv:
      value_template: "YOUR TEMPLATE"

will get:

File configuration.yaml:

switch:
- platform: template
  switches: !include_dir_named switch-templates/

File switch-templates/switch_skytv.yaml:

value_template: "YOUR TEMPLATE"

All files will be attached to switches named by their filename without yaml extension.
All yaml within the file will become child attributes of the name of the file.

the easiest way is to simply just add another template platform

switch:
- platform: template
  switches: !include  switch_template_switch_skytv.yaml
- platform: template
  switches: !include  skyqExtension.yaml
1 Like

Brilliant @petro that worked perfectly. thanks.