State_class: "measurement" for platform: template

Hi,

I have a zigbee powermeter which doesn’t count correctly (wrong pulse config),
so I thought creating a “virtual” sensor with the right counting.

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.

sensor:
  - platform: template
    sensors:
     util_meter:
       state_class: "measurement"
       friendly_name: 'EndesaOK'
       unit_of_measurement: 'kWh'
       device_class: 'energy'
       value_template: "{{ state_attr('sensor.endesa_energy', 'energy') /1000*400 }}"

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

Invalid config for [sensor.template]: [state_class] is an invalid option for [sensor.template]. Check: sensor.template->sensors->util_meter->state_class. (See ?, line ?).

How can I solve this error?

Seems that the template sensor does not support the state_class: “measurement”

Thanks

1 Like

Use the new modern template sensor format-

Your new template sensor should look like-

template:
  - sensor:
      - name: "EndesaOK Utility Meter"
        unit_of_measurement: "kWh"
        device_class: energy
        state_class: measurement
        state: >-
          {{ state_attr('sensor.endesa_energy', 'energy') /1000*400 }}

Note: Legacy template sensor format does not support measurement state_class.

17 Likes

Great, thank you for your help! @ardysusilo

Hi can you explain how to create this template sensor ?