Error in template sensor

I’m trying to add a template sensor, but when checking i get an error Invalid config for [sensor.template]: [state_class] is an invalid option for [sensor.template]

Why is this happening, and how to solve this.

this is what i have in a sepparate file, which is loading as a sensor

- platform: template
  sensors:
    s0energie:
      unit_of_measurement: kWh
      value_template: "{{ (states('counter.energieopgewekt')|float / 2000 ) | round(1) }}"
      device_class: energy
      state_class: total_increasing

energieopgewekt, is a helper which is added through the GUI.
i made an automation, that on every puls the counter is incereased with 1

When leaving the state_class the sensor is accepted. So it’s not the brackets

Try without the 0 (zero), I cannot test this

You’re using the legacy format which doesn’t support a state class.

Either convert it to a modern template sensor or put the state class in your customize.yaml.

Is it possible to load legacy files en modern tenplate files at ones.

Now i think i have them all as legacy files.

Yes, you can have both. They’re configured under different sections.

Then i have to look in the documentation where to put these new modern sensor templates.

I post the solution as a follow up as solution
[edit]

I tryed this, the config check says no error, all good to go.
But after restarting, the sensor isn’t visible.

i loaded as a sensor file.

template:
  - sensor:
      - name: "so1energie"
        unit_of_measurement: kWh
        state: "{{ (states('counter.energieopgewekt')|float / 2000 ) | round(1) }}"
        device_class: energy
        state_class: total_increasing

Did this have a value when you checked the value of the new template sensor after reload/startup?

If you have that configuration in a separate file, is your main configuration.yaml referencing it?

Yes it has a value and should be loaded, all files in the directory are loaded

found the solution, a template in the modern style, should be loaded from within template, not from sensor.

here is a part of the config file

group: !include groups.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
sensor: !include_dir_merge_list entities/sensors
binary_sensor: !include_dir_merge_list entities/binary
utility_meter: !include_dir_merge_named entities/utility_meters
automation: !include automations.yaml
template: !include_dir_merge_list entities/template

and the final template file

- sensor:
    - name: "so1energie"
      unit_of_measurement: "kWh"
      state: "{{ (states('counter.energieopgewekt')|float / 2000 ) | round(1) }}"
      device_class: energy
      state_class: total_increasing

so this could now be closed

3 Likes

I have a different result. Here is my configuration.yaml

sensor:
  - platform: template
    sensors:
      energy_to_grid:
        friendly_name: "Energy To Grid"
        value_template: "{{ states.sensor.solaxmine.attributes['data'][0]['dataDict'][10].value }}"
        device_class: energy
        unit_of_measurement: 'kWh' 

I have the following in customize.yaml

sensor.energy_to_grid:
  unit_of_measurement: kWh
  friendly_name: Energy To Grid
  device_class: energy
  state_class: total_increasing 

but the energy dashboard expects a state_class.
What am I missing please?
Am I not calling the customize.yaml correctly somewhere?

Answering myself. I needed

homeassistant:
  customize: !include customize.yaml

It had been deleted from an earlier edit of config.yaml.