Cannot add energy sensor to energy dashboard

Hi,
I am trying to add a template based energy sensor to energy dashboard, but it does not show in the pick list

What am I doing wrong in my sensor definition?

      - name: lights_total_consumption
        state: >
          {% set ns = namespace(states=[]) %}
            {% for s in states.sensor |
                selectattr('attributes.device_class', 'defined') | 
                selectattr('attributes.device_class', '==', 'energy') |
                selectattr('attributes.source_domain', 'defined') |
                selectattr('attributes.source_domain', '==', 'light')%}
                {% set ns.states = ns.states + [ s.state | float ] %}
            {% endfor %}
          {{ns.states | sum | round(2)}}
        device_class: energy
        state_class: measurement
        unit_of_measurement: KWh

Thanks.

Change this

to:

unit_of_measurement: kWh

@ tom_I
Actually I already thought that capitalization can matter, but despite changing KWh to kWh, still not able to find it in the drop down list.
I also played with the state_class, adding state_class: total as attribute

Restarted HA, still unable to find it in that dropdown list

thanks

Oh I messed that. Yeah, state_class has to be total or total_increasing as well as the unit change.

You will probably also need an availability template to prevent odd results if a device goes offline.

Well,
still with both these changes inmplemented (state_class: total and unit_of _measure: kWh) still no luck

I see in the that dropdown a lot of other energy sensors, but this one… driving me crazy

Try the state class total_increasing also, are you restarting HA after making the changes?

Tom,
Thanks but no luck.
Tried also restarting HA, that stubborn entity does not want to come up.

I am on 2022.11.4 FWIW

–B