Is it possible to have a running total variable in a loop?

If something like this:

  for_each: '{{ elements }}'
    - if: '{{ blaha }}'
        - variables:
            repeat_item_value: '{{ some template logic }}'

Could I sum up all repeat_item_value fields while repeating?
And also based on a criteria?
I want the last item in the list to have access to the running total. Possible?

No because of how variable scope works.

What are you trying to find the sum of? It may be possible to compute it with a template.

Not like that.
You can if you create a number helper and accumulate the count in that. As 123 said, variables will not work because of scope, but number helpers have a global scope.

Tricky and messy, but it’s possible.

Here I store an input_boolean and update it in an inner loop in order to tell the end of the automation what happened in the inner loop and decide on a final action, for instance. I suggest you create this as hidden because it will only be referenced within this BP/Automation.

I would love to see automations get a variable that the user could write to with global scope within the running automation. Then you could store transient things in a global (to the automation) scope. Would be very handy.

I have a collection of production and consumption sensors that the user can add to the BP. I would like to get a total of them without having the user to create additional template/helper sensors.

Thanks for the tip. Kind of messy when end user needs to add a helper, but better then a template sensor. Hopefully global scope variables comes in the near feature.

I haven’t seen that suggested anywhere, so don’t hold your breath, It is an idea, not even a FR yet.

This sounds like something you can compute with a templated script variable (within the blueprint).