Summation of individual sensors via sensor template not working

can anyone tell me what I am doing wrong? When I test the template via the template editor everything works. If I use it as a sensor template, it does not sum up correct my values. I do not know what I’m doing wrong. Does anyone have a tip?

The values are:

[0.0, 0.3, 0.85, 0.51, 0.48, 0.09, 1.18, 0.2, 0.6]

The result of my sensor is 1.12 instead of 4.21. When I test this via template editor I get the correct result back.

This is my sensor:

platform: template
sensors:
  house_energy_daily_consumption:
    friendly_name: 'Hausstromverbrauch pro Tag'
    unit_of_measurement: 'kwh'
    value_template: >
      {% set ns = namespace(states=[]) %}
      {% for s in states.sensor %}
        {% if s.object_id.endswith('_energy_daily') %}
          {% set ns.states = ns.states + [ s.state | float ] %}
        {% endif %}
      {% endfor %}
      {{ ns.states | sum | round(2) }}

figured it out myself :wink:

platform: template
sensors:
  house_energy_daily_consumption:
    entity_id: sensor.time
    friendly_name: 'Hausstromverbrauch pro Tag'
    unit_of_measurement: 'kwh'
    value_template: >
      {% set ns = namespace(states=[]) %}
      {% for s in states.sensor %}
        {% if s.object_id.endswith('_energy_daily') %}
          {% set ns.states = ns.states + [ s.state | float ] %}
        {% endif %}
      {% endfor %}
      {{ ns.states | sum | round(2) }}

How many sensors do you have whose name ends with “_energy_daily”?

currently there are 9 sensors ends with ‘_energy_daily’.