Sum energy values of list of entities

Hi there,

I want to measure the energy used by all lights in my home. I use a bunch of shelly devices that are all named with ‘light’ in the id.

{{ states
  | selectattr('attributes.device_class', 'eq', 'energy')
  | selectattr('entity_id', 'search', 'light')
  | map(attribute='state')
  | list
}}

works as expected and lists the energy consumption of the devices. But it seems that the energy is presented as a string, not a float.

[
  "0.221692",
  "0.212433",
  "0.0",
  "0.156039",
  "0.00156666666666667",
  "0.0504",
  "0.0",
  "0.00666666666666667",
  "5.84976666666667",
  "0.399117",
  "0.0457333333333333",
  "0.00203333333333333",
  "0.4977",
  "0.6587",
  "0.00596666666666667",
  "0.0",
  "0.0",
  "0.0143",
  "0.360583333333333"
]

so when I use

| sum 

at the end, I get

TypeError: unsupported operand type(s) for +: 'int' and 'str'

Is there any way to convert the single values to floats?

Thanks!
Michael

Powercalc may work for you
I use it in a sankey chart to show energy/lights per room:

{{ states
  | selectattr('attributes.device_class', 'eq', 'energy')
  | selectattr('entity_id', 'search', 'light')
  | map(attribute='state')
  | map('float', 0)
  | sum
}}

You can restrict the list to light entities by starting with states.light as opposed to states (which represents all entities in the system).

{{ states.light
  | selectattr('attributes.device_class', 'eq', 'energy')
  | map(attribute='state')
  | map('float', 0)
  | sum
}}

Your version of the template starts with all entities, narrows it down to the ones containing an energy device class, then narrows it down again to the ones that contain the string light anywhere in their entity_id. That means it will include light.kitchen as well as switch.bedroom_light (assuming both have the specified device class).

Perfect! That did it! Thank you.

The states.light will not work as there are some Shelly devices that I couldn’t get them to recognize themselves as light :wink:

Michael