Can not put "modern style template sensor" in sensors.yaml

While this works pefectly in configuration.yaml:

template: 
  - sensor:
    - name: "heizung_gas_momentan_verbrauch"
      unique_id: "7a9942f6-db7a-4ead-8c8d-1d7231e71380"
      state_class: "measurement"
      unit_of_measurement:  "kwh"
      state: "{{states('sensor.vicare_heating_gas_consumption_today')}}"

I can’t split it out to sensors.yaml, VSCode throws errors:

What I’m doing wrong here?

Did you split your configuration.yaml correctly according to Splitting up the configuration - Home Assistant
?

The way sensors.yaml is typically configured, everything in the file is assigned a top level configuration key of sensor… then you use the platform property to designate the type of sensor you are setting up. The “modern” template sensor format requires a top level key of template. You may already have a template.yaml file in you config folder, otherwise you need to set one up following the guidelines in the link @pedolsky provided.

that’s because “template:” is it’s own top level key and equal to “sensor:”.

you need to split your config like this in configuration.yaml:

template: !include template_sensors.yaml ## or whatever file name you want

then in the newly created “template_sensors.yaml” you will put this:

- sensor:
    - name: "heizung_gas_momentan_verbrauch"
      unique_id: "7a9942f6-db7a-4ead-8c8d-1d7231e71380"
      state_class: "measurement"
      unit_of_measurement:  "kwh"
      state: "{{states('sensor.vicare_heating_gas_consumption_today')}}"

## then you can add other "template" stuff below as well...

- binary_sensor:
    - name: ...

- cover:
   - name: ...

# etc

@pedolsky I tried it, but obvious I made a mistake …

Thank you. I didn’t realise that “template :” is an top level section. After using your suggestion, it works!
I don’t think that YAML & I will be best friends one time :woozy_face: , same with Jinja.

yaml isn’t as hard as many try to make it.

just use it for a bit and it makes sense and I think it’s WAY easier than the tedious “click, click, scroll, click…etc…ad nauseam” of the UI.

Jinja is not as easy because there is just so much you can do with it. Figuring out all the filters/methods is the hard part. And that’s why it’s almost a necessity to learn to be able to do the slick advanced stuff. But it’s not too bad either when you use it for a bit.

If you simply want all your sensors in one file you can also do this pretty easily with packages. Just make a package called sensors like this:

template:
  # put all template sensors here
sensor:
  # put all other sensors here
mqtt:
  # put all Mqtt sensors here
  # (just to show the pattern applies to anything, idk if you use Mqtt)
...

Then include it in configuration.yaml like this:

homeassistant:
  packages:
    sensors: !include my_sensors_package.yaml