HELP! REST sensor value_template

I have a rest sensor that returns the following json string.

{“result”:{“stats”:[{“balance”:“0.00002685”,“rejected_speed”:“0”,“algo”:5,“accepted_speed”:“0”},{“balance”:“0.00016852”,“rejected_speed”:“0”,“algo”:7,“accepted_speed”:“0”},{“balance”:“0.00004954”,“rejected_speed”:“0”,“algo”:14,“accepted_speed”:“0”},{“balance”:“0.0000373”,“rejected_speed”:“0”,“algo”:20,“accepted_speed”:“0”},{“balance”:“0.00000381”,“rejected_speed”:“0”,“algo”:21,“accepted_speed”:“0”},{“balance”:“0.00082938”,“rejected_speed”:“0”,“algo”:22,“accepted_speed”:“0.000004”},{“balance”:“0.00013585”,“rejected_speed”:“0”,“algo”:24,“accepted_speed”:“0.00000046”}],“payments”:[{“amount”:“0.00144643”,“fee”:“0.00002952”,“TXID”:"",“time”:“2018-01-25 11:07:55”},{“amount”:“0.00116031”,“fee”:“0.00002368”,“TXID”:"",“time”:“2018-01-23 10:43:39”}],“addr”:“22e5rZiEHzLcMUCVAcMAbSigUgA6981Kqg”},“method”:“stats.provider”}

I am using the following value template statement to capture the balance value in the array:
value_template: '{{ value_json.result.stats[0]["balance"] }}'

This will only capture the first value.

What I am looking to do is capture all of the balance values and sum them together to produce the sensor value. Is this possible?

This should work:

{% for item in value_json.result.stats %}
 {% set sum = sum + item["balance"] | float %}
{% endfor %}

Seems to be returning an unknown value. This is the value template statement
value_template: '{% for item in value_json.result.stats %} {% set sum = sum + item["balance"] | float %} {% endfor %}'

Is that correct?

Just looked it up and found something simpler:

value_template: '{{ value_json.result.stats | sum(attribute="balance") }}'

Or the longer option:

value_template: >-
  {% for item in value_json.result.stats %}
    {% set sum = sum + item["balance"] | float %}
  {% endfor %}
  {{ sum }}

Still not returning a value. This is the complete template config:

https://hastebin.com/hidakahike.scala