State_class: “measurement” for platform: template give error

Hi all,

I have a zigbee power meter which is working perfectly.

My problem is to be able to track energy in the new energy dashboard of HA, my sensor needs to have a measurement class definition, which I can’t set using the new format.

This is my configuration.yaml file:

sensor: !include_dir_merge_list sensors/

And this is the file boiler.yaml inside sensors/ directory:


- platform: template
  - sensor:
    - name: "Consumption today kW"
        unit_of_measurement: "kW" # or W depending on your daily energy sensor
        device_class: energy
        state_class: measurement
        state: >
          {% set hrs = [ now().hour + now().minute/60, 0.01]|max %} {# this template only updates every minute so we don't have to worry about seconds, we do have to worry about it being zero though #}
          {{ (states('sensor.daily_energy_consumption')|float(0) / hrs )|round(2) }}
        availability: "{{ states('sensor.daily_energy_consumption')|is_number and (now().hour + now().minute/60) > 0 }}"

Having this in my config, I receive the following error:

Error loading /config/configuration.yaml: while parsing a block mapping
in "/config/sensors/boiler.yaml", line 23, column 3
expected <block end>, but found '-'
in "/config/sensors/boiler.yaml", line 24, column 3

I want to use the new modern template sensor format but also including a directory with more sensors.

Please help me!
Thanks!

    - name: "Consumo electrico medio hoy kW"
        unit_of_measurement: "kW" # or W depending on your daily energy sensor
      ^^

you’ve got some stray indenting going on

Thanks so much @Tinkerer !

I modified as follows but continue giving the same error:

- platform: template    <---------- error here
  - sensor:
    - name: "Consumption today kW"
      unit_of_measurement: "kW" # or W depending on your daily energy sensor
      device_class: energy
      state_class: measurement
      state: >
        {% set hrs = [ now().hour + now().minute/60, 0.01]|max %} {# this template only updates every minute so we don't have to worry about seconds, we do have to worry about it being zero though #}
        {{ (states('sensor.daily_energy_consumption')|float(0) / hrs )|round(2) }}
      availability: "{{ states('sensor.daily_energy_consumption')|is_number and (now().hour + now().minute/60) > 0 }}"

Error loading /config/configuration.yaml: while parsing a block mapping
in "/config/sensors/boiler.yaml", line 23, column 3
expected <block end>, but found '-'
in "/config/sensors/boliler.yaml", line 24, column 3

I think the problem is that you’re mixing old style template sensors, and new style.

The legacy style is:

sensor:
  - platform: template
    sensors:
      solar_angle:
        friendly_name: "Sun angle"

the new style is:

template:
  - sensor:
      - name: "Average temperature"
        unit_of_measurement: "°C"

You need to use one, or the other, and ideally the new style.

2 Likes

Thanks so much @Tinkerer. Now is working perfectly!