Help please: Entity definition format under template error

Hi,

I have a few template sensors (under templates.yaml), which count the amount of lights that are on and climate devices that are on.
They work fine (they give the amount I’m looking for), but I get the following syntax error when reloading my templates. I really can’t figure it out why. Any help is much appreciated. I’m sure it’s something silly I’m overlooking. I tried giving them the device_class ‘enum’, but that didn’t change anything.

The entity definition format under template: differs from the platform configuration format. See https://www.home-assistant.io/integrations/template#configuration-for-trigger-based-template-sensors

The sensors are defined as such:

templates:
  - sensors:
     hoeveel_lampen_aan:
        friendly_name: "Aantal lampen die nu aan staan"
        unit_of_measurement: 'on'
        value_template: "{{ states.light | selectattr('state', 'eq', 'on') | rejectattr('attributes.is_deconz_group', 'eq', false) | list | count }}"
  - sensors:
      hoeveel_radiatoren_aan:
        friendly_name: "Aantal radiatoren die nu aan staan"
        unit_of_measurement: 'on'
        value_template: "{{ states.climate | selectattr('attributes.supported_features', 'eq', 1) | rejectattr('attributes.hvac_action', 'eq', 'idle')  | list | count }}"

have you tried your template in devtools/template ?

If you look in the documentation, template sensor are defined as:

template:
  - sensor:
      - name: "Something"
        unit_of_measurement: "something"
        state: >

No value_template here.

You are using, in my opinion (I can be wrong), the wrong way to define what you try to achieve.
You should use the sensor tag:

sensor:
  - platform: template
    sensors:
      hoeveel_lampen_aan:
        friendly_name: "Aantal lampen die nu aan staan"
        unit_of_measurement: 'on'
        value_template: "{{ states.light | selectattr('state', 'eq', 'on') | rejectattr('attributes.is_deconz_group', 'eq', false) | list | count }}"
      hoeveel_radiatoren_aan:
        friendly_name: "Aantal radiatoren die nu aan staan"
        unit_of_measurement: 'on'
        value_template: "{{ states.climate | selectattr('attributes.supported_features', 'eq', 1) | rejectattr('attributes.hvac_action', 'eq', 'idle')  | list | count }}"

Which accept the value_template tag

TBH, I’m surprised they work at all.

you are using the new “template:” domain to configure a legacy template sensor.

also the domain key you are using is also wrong. It should be “template:” not “templates:”

you need to pick one version (modern or legacy - either one is fine) and then use the syntax for that version as explained in the docs.

Thanks all!
As I said, it was probably something stupid and mixing modern and legacy syntax counts for that I think.

I have the following and that works, without the error :wink:

Thanks again!!

- sensor:
    - name: "hoeveel_lampen_aan"
      unique_id: "hoeveel_lampen_aan"
      unit_of_measurement: 'on'
      state: "{{ states.light | selectattr('state', 'eq', 'on') | rejectattr('attributes.is_deconz_group', 'eq', false) | list | count }}"
- sensor:
     - name: "hoeveel_radiatoren_aan"
       unique_id: "hoeveel_radiatoren_aan"
       unit_of_measurement: 'on'
       state: "{{ states.climate | selectattr('attributes.supported_features', 'eq', 1) | rejectattr('attributes.hvac_action', 'eq', 'idle')  | list | count }}"

Just another bit of info…

You don’t need to have “- sensor:” twice.

The dash in front of “name:” creates a list of sensors.

Thanks. That makes it a bit cleaner :slight_smile: