Blueprint template with multiple inputs

Hi, my template result is 0 when I try multiple inputs. When I simplified to 1 input, it works.
Please, help.

blueprint:
   name: power
   description: "Order sensitive: 1st current must be drawn from 1st voltage etc."
   domain: template
   input:
      voltage:
         selector:
            entity:
               domain: sensor
               multiple: true
      current:
         selector:
            entity:
               domain: sensor
               multiple: true

variables:
   voltage: !input voltage
   current: !input current

sensor:
   device_class: power
   unit_of_measurement: W
   state: >
      {% set total = 0 %}
      {% for i in range (voltage | length) %}
         {% set total = total + float (states (voltage[i])) * float (states (current[i])) %}
      {% endfor %}
      {{ total | round (0) }}

This is solution:

      {% set ns = namespace (total = 0) %}
      {% for i in range (voltage | length) %}
         {% set ns.total = ns.total + float (states (voltage[i])) * float (states (current[i])) %}
      {% endfor %}
      {{ ns.total | round (0) }}

For what it’s worth:

  • You haven’t included any protection against unavailable or unknown states.
  • Depending on your situation, this might be a time where using Devices could be useful. As long as the voltage and current are coming from the same device, you could use the device ID to get them both without requiring the user to input entities in order.

You haven’t included any protection against unavailable or unknown states.

I want to see in resulting sensor, that something is wrong, so unavailable would be nice when one of source is unavailable.

Depending on your situation, this might be a time where using Devices could be useful. As long as the voltage and current are coming from the same device, you could use the device ID to get them both without requiring the user to input entities in order.

No, at least currents are from different sensors. Maybe voltages.