Value_template driving me nuts... Please help

Hi!

I’m totally new here and this is my first post. (Be gentle…)

I have few, 7 of them, sensors that have the attribute totalValue. I’m trying to add a template sensor to HA that add all these together without having to specify all of them separately, the amount and name(s) might change in the future.

This is my entry for the value_template in configuration.yaml.

value_template: "{{states.sensor|selectattr('attributes.totalValue')|map(attribute='attributes.totalValue')|sum}}"

This works great and I do get the sum that I’m looking for, but…

My core log fills up with messages like this, a lot of them:

2021-11-11 15:37:21 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'mappingproxy object' has no attribute 'totalValue' when rendering '{{states.sensor|selectattr('attributes.totalValue')|map(attribute='attributes.totalValue')|sum}}'

If i try the expression (is that the correct word) in the Dev Tools Template section I do get the same error from there. If I then change “|sum}}” in the end to “|list}}” I do see the whole list of 7 attributes that I want to sum up like this. And there is no warnings in the core log, from the Template editor that is.

[101.64, 713.86, 3757.0, 299.2, 8815.0, 8313.75, 2821.0]

I’m at wits´ end here.

Running core-2021.11.2, supervisor-2021.10.8 and Home Assistant OS 6.6 on HA Blue.

Regards,

/Jonas…

The cause of the warning messages is explained here:

The template should confirm the attribute is defined in the entity. Try this version:

{{ states.sensor | selectattr('attributes.totalValue', 'defined') | map(attribute='attributes.totalValue') | sum }}
1 Like

Love you!

Works perfectly. Thanks.

/Jonas…

1 Like