Frustrating "platform required" yaml error defining basic template sensor

Hi all,

I’m new to HA, trying to implement some basic custom stuff I’d done successfully in OpenHAB.

First task is to create “template sensor” which “allows creating entities which derive their values from other data”, so I am following this basic example in the Template docs

I created config/sensors.yaml and and am including it.

But when I Check Configuration in Developer Tools > YAML, I keep getting this error:

Invalid config for [sensor]: required key not provided @ data[‘platform’]. Got None. (See /config/configuration.yaml, line 11).

But any examples I can find that include platform (platform: template) seem to be legacy syntax we should avoid… right?

So why doesn’t the “correct” “modern” syntax validate? Do I ignore the docs and use platform?

Home Assistant 2023.5.4
Supervisor 2023.04.1
Operating System 10.2
Frontend 20230503.3 - latest
(on Raspberry Pi 3 32-bit)

Here’s my exact variation of the example YAML:

  - sensor:
      - name: "Temp Diff Outdoors Cooler"
        unit_of_measurement: "°F"
        state: >
          {% set int = states('sensor.office_temperature') | float %}
          {% set ext = states('weather.forecast_home.temperature') | float %}
          {{ (int - ext) | round(1, default=0)}}

Oh, I’m editing in vim via ssh; this does not involve VS Code.

Thanks for any tips!

The error suggests that there’s a problem with the include line in configuration.yaml. Since this is a template sensor it should be something like:

template: !include sensors.yaml

Then in the file you have called sensors.yaml you should omit the template: line. !include simply takes a chunk of code and inserts it into configuration.yaml - having template: twice will throw an error.

I personally would have called the file templates.yaml to avoid confusion. I use sensors.yaml for sensors derived from integrations. In these the name of the platform is required.


So now I have templates.yaml (renamed as you suggested) beginning like this:

- sensor:
    - name: "Temp Diff Outdoors Cooler"
      unit_of_measurement: '°F'

and I changed the include:

template: !include templates.yaml

I assume the old key sensor is what triggered the requirement of platform in my included definition(s).

sort of but not exactly.

just to be clear the “sensor:” key isn’t old. Template sensors (“platform: template”) defined under the “sensor” domain (…aka key…) are still completely valid. I still use them extensively. And there are still many sensors that aren’t templates that are configured under the sensor: key.

the issue was that you included a config under the sensor: key that started with the template: key.

sensor: and template: are now both top level domains (keys) and you can’t include them within each others config.

if you had left out “template:” and used the (poorly designated) “legacy” syntax you would have been fine.

just to be clear the “sensor:” key isn’t old.

Sorry, yes, I just meant I had changed the key from sensor to template. And so different rules apply. I understand the sensor key is not “old” as in deprecated.

1 Like