Sum power sensors even if some sensors are unavailable

Hi I created a template sensor to sum the actual power consumption of (all) sensors in my house.
Code I use;

    state: >-
        {{(
        (states("sensor.bijkeuken_buitenlamp_achter_202_power") | float) +
        (states("sensor.bijkeuken_buitenlamp_berging_200_power") | float) +
        (states("sensor.bijkeuken_wasdroger_407_power") | float) +
        (states("sensor.gang_ganglamp_110_power") | float) +
        (states("sensor.garage_327_1_330_power") | float) +
        (states("sensor.garage_garagelight_328_power") | float) 
        ) | round(0) }}

When any sensor in the template sensors reports a value ‘unavaliable’, the template sensor seems to become unavailable. Some sensors in my home report unavailable when device is off or when no power is consumed.

Q1: How can I make the template sensor to sum power consumption while ignoring sensor states that are unavailable?

Q2: Is there a way to do this sum in the template sensor without having to list each individual sensor one by one? Could I for example use wildcards (*power) ? Any other smart(er) suggestions?

Thanks!

Regards
Jules

1 Like
      state: >-
        {{ states.sensor
          | selectattr('object_id', 'search', 'power$')
          | map(attribute='state') | map('float', 0)
          | sum | round(0) }}

Hi, I’m having a similar problem and this sums all the power sensors correctly but if you have solar panels it will also sum the power you are generating which defeats the purpose.

Post your question in a separate topic.

1 Like

an alternative aproach can be like this:

          {{expand('group.lights_inside_total_device_power')
             |map(attribute='state')
             |select('is_number')
             |map('float')|sum}}

the select('is_number') filter all non numerical states, including unavailable and unknown, and even allows the map('float') to be used as is, no need for a default

on my energy sensors that sometimes erratically reset to 0 mysteriously… I filter those out with an additional reject:

          {{expand('group.heaters_total_device_energy')
             |map(attribute='state')
             |select('is_number')
             |map('float')
             |reject('==',0.0)|sum}}

now that I look at that, the last addition might be useless as adding 0 does not change the output :wink:

There’s no need for select('is_number') if you simply supply float with a default value of 0.

{{expand('group.lights_inside_total_device_power')
  |map(attribute='state')
  |map('float',0)|sum}}

It’s a sum, so the presence of zeros doesn’t skew the calculation.

haha, well, options galore…

I’ve tried to incorporate that option when it was added to our toolkit (as I always try to do with new features, now where could I use that…).
First I had some availability templates use it, and then figured it could be used in the state. I like the verbiage, which makes more sense to me than the |map('float',0) at first glance.

the reject was from another template where I only wanted to list the correct entities, and not the temporary flawed ones. in that template it actually was very much required.
consider it a c&p pollution in these examples…

Hi Jules,

did you find any solution for your first question “sum power consumption while ignoring sensor states that are unavailable?” ?

I do have a similar issue and couldn’t find any solution.

Any help is appreciated.

Knut

@KNH
Sorry for necroing the thread. For me, the solution was to change float to float(0).