New sensor Template configuration format in sensor.yaml, is it working?

Hi,
I have several template sensors included in configuration.yaml via links to external yaml files.

## configuration.yaml
sensor:

sensor room_1: !include room_1.yaml
sensor room_2: !include room_2.yaml
sensor room_3: !include room_3.yaml
##  room_1.yaml
- platform: template
  sensors:
    temperature_1:
      friendly_name: "Temp Room 1"
      value_template: "{{ states('sensor.temp_1').split(',')[0] }}"
    temperature_2:
      ...

Problem is, that apparently this format is deprecated, here the current format: Template - Home Assistant

For example, instead of friendly_name and value_template, I want to use name and state as reported in the documentation.

I tried to adapt the configuration but it is not working, and all the examples I found online they still use the old format, or they use the new format but they don’t link to an external file for the sensors.
Anybody has a working example?

Thanks

If you are attempting to use the new format but in the sensor domain then that’s a mistake. The new format is used with a new domain called template.

I tried that, I could link one file like this and it works:

# configuration.yaml

template: !include room_1.yaml
##  room_1.yaml
- sensor:
  - name: "temperature_1"
    state: "{{ states('sensor.temp_1').split(',')[0] }}"
  - name: "temperature_2"
      ...

But how would you link a second file, let’s say room_2.yaml that looks exactly the same?

What was the result when you tried it?

# configuration.yaml

template: !include room_1.yaml
template: !include room_2.yaml

## WARNING:homeassistant.util.yaml.loader:YAML file /config/configuration.yaml contains duplicate key "template". Check lines 331 and 332
# configuration.yaml

template: 
  sensor: !include room_1.yaml


# room_1.yaml
- name: "temperature_1"
  state: "{{ states('sensor.temp_1').split(',')[0] }}"
- name: "temperature_2"
  ...

## WORKS
# configuration.yaml

template: 
  sensor r1: !include room_1.yaml
  sensor r2: !include room_2.yaml


# room_1.yaml
- name: "temperature_1"
  state: "{{ states('sensor.temp_1').split(',')[0] }}"
- name: "temperature_2"
  ...

## Invalid config for [template]: [sensor r1] is an invalid option for [template]. Check: template->sensor r1. (See /config/configuration.yaml, line 332). 

No idea if there is any other way to link them that I can try.

try:

# configuration.yaml

template: !include room_1.yaml
template_2: !include room_2.yaml

Doesn’t work, template_2 is not a valid integration.
Anyway I think I fixed it finally using:

# configuration.yaml

template: 
  sensor: !include_dir_merge_list rooms/


# rooms/room_1.yaml
- name: "temperature_1"
  state: "{{ states('sensor.temp_1').split(',')[0] }}"
- name: "temperature_2"
  ...

The problem I think was related to merging the list coming from different files, while all the entries have to go under a single sensor block.

Have you tried just this?

template: !include_dir_merge_list rooms/

Yeah, I’m not sure why they made it so that the “template:” integration woirks differently than the others.

it was worth a shot.

if it’s listed, i.e.

field:
- item1: blah
  ...
- item2: blah
  ...

you use

field: !include_dir_merge_list xyz/

where the file(s) contents look like:

- item1: blah
  ...
- item2: blah
  ...

if the field is keyed:

field:
  item1:
    ...
  item2:
    ...

use

field: !include_dir_merge_named xyz/

where the file(s) contents look like:

item1:
  ...
item2:
  ...

These ‘styles’ are all mixed throughout all the yaml. E.g. sensor, light, binary_sensor, template, automation, are all lists and script (can’t think of others off the top of my head) is keyed.

Read up on it here:

123 provided you with the solution you need.

You’re mixing up the syntax, if you want 2 sections with the same name it’s:

template: !include room_1.yaml
template abc: !include room_2.yaml
template xyz: !include room_3.yaml

EDIT: This only works with lists, it does not work with keyed/named.

1 Like

oops! crap! you’re right.

my bad.

ignore me… :slightly_smiling_face:

It doesn’t work with lists either, it only includes the first file:

# configuration.yaml
template: !include room_1.yaml
template r2: !include room_2.yaml


##  room_1.yaml
- sensor:
  - name: "temperature_1"
    state: "{{ states('sensor.temp_1').split(',')[0] }}"
  - name: "temperature_2"
      ...

To include multiple files like room_1.yaml the solution of 123 works, which is shorter than mine.

You most likely have to name each one then. Merging lists will work, see the automations section of the link I posted.

EDIT: Or that only works on the automation section, but I doubt that.

If you name them all it doesn’t even compile the configuration.
If you are referring to the last paragraph of the link, it doesn’t use !include several times like in your example, it mixes !include_dir_merge_list and !include, each used once.

include vrs include_merge_dir_lists is just how they compile the included file(s) into the main file. You can use both as !include. This must only work on the automation section.

EDIT: Nevermind, just checked the code, it just doesn’t work on the template section.

I don’t think that’s correct. You can’t use an !include if you are including multiple files with lists like in my example. Your first post looks like the way to go.

Can you paste the code you are referring to from your link? I couldn’t find your example anywhere in the page.

The example doesn’t have to match exactly… it’s just how include works. Include just takes a file and plops the contents as yaml into the field. Splitting 1 section between two files is possible with naming, the example is at the bottom. You’re confusing !include and !include_merge_lists as mutually exclusive, which they are not.

This is the example:

# My own handmade automations
automation manual: !include_dir_merge_list automations/

# Automations I create in the UI
automation ui: !include automations.yaml

while this is entirely possible

# My own handmade automations
automation manual: !include_dir_merge_list room1/

# My own handmade automations
automation manual2: !include_dir_merge_list room2/

# My own handmade automations
automation manual3: !include_dir_merge_list room3/


# My own handmade automations
automation manual4: !include some_other_automations.yaml

# Automations I create in the UI
automation ui: !include automations.yaml

I didn’t test the first example block from the documentation, but the configuration from your second block of code doesn’t compile if you replace automation with template, probably the files linked have a different structure, dunno.

Yes, did you even read my response or are you busy arguing…