Template to add values of all sensors _actueel

cleaning/checking my template sensors, Ive stumbled on this verbose state addition template:

           {{(states('sensor.freezer_garage_actueel')|int)
           + many more
           + (states('sensor.cv_garage_actueel')|int)
           + (states('sensor.cv_stookhok_actueel')|int)
           + (states('sensor.cv_zolder_actueel')|int)
           + (states('sensor.zonneboiler_zolder_actueel')|int)
           + (states('sensor.patchboard_zolder_actueel')|int)}}

I suppose this must be possible in a more intelligent way, but don’t see it at the moment…
add the values of all sensors ending with _actueel

anyone with a hint getting me on the road please? Ive had it before, long time ago… see Template to add values of all sensors with certain name ending.

but then I had the sensors in a group (no longer available) and the python script was way to powerful and at a high HA processor cost, because it was triggered by an automation that constantly ran…

so, hope while time has flowed, new possibilities have arisen… or skills for that matter :wink:

this almost selects the right sensors

{% for state in states.sensor if '_actueel' in state.entity_id  %}
 {{state.state}}
{% endfor %}

should only filter out None and unknown

like this:

{% for state in states.sensor if '_actueel' in state.entity_id and state.state != 'unknown' and state.state != 'None' %}
 {{state.state}}
{% endfor %}

thanks!

Unless I’ve misunderstood your requirements, I think you already have 95% of the solution. Here’s the remaining 5%:

{% set ns = namespace(total = 0) %}
{% for state in states.sensor if '_actueel' in state.object_id %}
  {% set ns.total = ns.total + (state.state | int) %}
{% endfor %}
{{ ns.total }}

If you wish, you can filter out state values None and unknown. If you don’t filter them out, the template will still work because the int filter converts non-numeric strings to integer 0.

cool! although it doesn’t work just yet :wink:

must be a typo I dont see…?

No, it was my mistake. I used total instead of ns.total. I’ve corrected the example I posted.

bingo!

and especially love the |int trick . use that myself quite a lot in other cases when brightness of lights is concerned.

thank you very much @123!

1 Like

just as a small but:
will this update live?

You’re welcome!


Live? Like in a Template Sensor? Unlikely because the template lacks identifiable entities. You’ll need to add entity_id: with an entity that changes state periodically, like sensor.time.

I realize the optimal scenario is to have the Template Sensor update whenever one of the “_actueel” sensors changes its state. Unfortunately, that means you have to add each one of them to entity_id: or as a list in the template but then that makes the whole for-loop concept pointless.

yes, getting back to this, it didn’t, as I expected…
instead of sensor.time which is onlynonce per minute of course, Ive added the sensor that reads my smart meters usage realtime (probably per the seconds). this makes it update just as frequent as the other verbose sensor. They are all sent from my mqtt hub, so update at the same time, whenever a change is sensed and sent over the subscribed topics.