Need help with template, sum of the elements of 2 lists

I have 2 lists and I wan’t the sum of the first element of the first en the second list, …
And this should be a new list

{{states("sensor.solcast_24hrs_forecast_garage")}}
{{states("sensor.solcast_24hrs_forecast_huis")}}


{% set garage = states("sensor.solcast_24hrs_forecast_garage") %}
{% set huis = states("sensor.solcast_24hrs_forecast_huis") %}
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 96, 304, 561, 832, 1117, 1414, 1584, 1583, 1567, 1554, 1500, 1412, 1415, 1492, 1373, 1074, 885, 739, 559, 362, 184, 67, 12, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 30, 443, 694, 1079, 1249, 1503, 1620, 1582, 1552, 1539, 1483, 1394, 1378, 1417, 1303, 1055, 893, 780, 684, 507, 325, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

And this is what I need

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 27, 126, 747, .........]

I’m struggling with this for hours and I can’t find a solution

{% set garage_sum = garage | sum %}
{% set huis_sum = huis | sum %}

EDIT - Wait - I’ve read that wrong.

Right providing that both arrays are the same length - then:

{% set newarray=namespace(items=[]) %}
{% set garage = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 96, 304, 561, 832, 1117, 1414, 1584, 1583, 1567, 1554, 1500, 1412, 1415, 1492, 1373, 1074, 885, 739, 559, 362, 184, 67, 12, 0, 0, 0, 0, 0, 0, 0, 0] %}
{% set huis = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 30, 443, 694, 1079, 1249, 1503, 1620, 1582, 1552, 1539, 1483, 1394, 1378, 1417, 1303, 1055, 893, 780, 684, 507, 325, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] %}
{% for item in garage %}
{% set x = (garage[loop.index-1] + huis[loop.index-1]) %}
{% set newarray.items = newarray.items + [x] %}
{% endfor %}
{{ newarray.items }}

That’s direct from the template editor - so I know it works.

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 27, 126, 747, 1255, 1911, 2366, 2917, 3204, 3165, 3119, 3093, 2983, 2806, 2793, 2909, 2676, 2129, 1778, 1519, 1243, 869, 509, 67, 12, 0, 0, 0, 0, 0, 0, 0, 0]

When I paste this

{{states("sensor.solcast_24hrs_forecast_garage")}}
{{states("sensor.solcast_24hrs_forecast_huis")}}

{% set newarray=namespace(items=[]) %}
{% set garage = states("sensor.solcast_24hrs_forecast_garage") %}
{% set huis = states("sensor.solcast_24hrs_forecast_huis") %}
{% for item in garage %}
{% set x = (garage[loop.index-1] + huis[loop.index-1]) %}
{% set newarray.items = newarray.items + [x] %}
{% endfor %}
{{ newarray.items }}

in my editor I get UndefinedError: str object has no element 199

That’s what I was worried about, the arrays aren’t the same length. So we’ll need to add extra error checking urgh.

Replace

{% for item in garage %}
{% set x = (garage[loop.index-1] + huis[loop.index-1]) %}
{% set newarray.items = newarray.items + [x] %}
{% endfor %}

with

{% for item in garage %}
{% if garage[loop.index-1] is defined %}
{% set a1 = garage[loop.index-1] %}
{% else %}
{% set a1 = 0 %}
{% endif %}
{% if huis[loop.index-1] is defined %}
{% set a2 = huis[loop.index-1] %}
{% else %}
{% set a2 = 0 %}
{% endif %}
{% set x = a1 + a2 %}
{% set newarray.items = newarray.items + [x] %}
{% endfor %}

See how that works to start with, but later we will probably have to check the length of both arrays to find out which one is longest - and do for the for loop on the longest array.

{% set a = states("sensor.solcast_24hrs_forecast_garage")[1:-1].split(',') | map('int') | list %}
{% set b = states("sensor.solcast_24hrs_forecast_huis")[1:-1].split(',') | map('int') | list %}
{% set ns = namespace(items = []) %}
{% for i in range(a) %}
  {% set ns.items = ns.items + [ a[i] + b[i] ] %}
{% endfor %}
{{ ns.items }}

edit: Your error was not making them lists and keeping them strings

2 Likes

I wasn’t too sure about that part, because of course even if it is a string if you ask the template editor to display it - it will claim it is a list.

yah it’s goofy, but you were close. So what you have to do is strip the [ ], then split the string, then map the values to integers. It’s a pita

1 Like

I will try as soon as I can. I’m at work now and we had a crash in a machine.

TypeError: 'list' object cannot be interpreted as an integer

{% set a = states("sensor.solcast_24hrs_forecast_garage")[1:-1].split(',') | map('int') | list %}
{% set b = states("sensor.solcast_24hrs_forecast_huis")[1:-1].split(',') | map('int') | list %}
{% set ns = namespace(items = []) %}
{% for i in range(a | length) %}
  {% set ns.items = ns.items + [ a[i]  + b[i]  ] %}
{% endfor %}
{{ ns.items }}

This works but the solution looks weird

[
  0,
  0,
  0,
  0,
  0,
  0,
  0,
  0,
  0,
  0,
  0,
  0,
  0,
  0,
  0,
  0,
  27,
  126,
  747,
  1255,
  1911,
  2366,
  2917,
  3204,
  3165,
  3119,
  3093,
  2983,
  2806,
  2793,
  2909,
  2676,
  2129,
  1778,
  1519,
  1243,
  869,
  509,
  67,
  12,
  0,
  0,
  0,
  0,
  0,
  0,
  0,
  0
]

And if I do this

{% set a = states("sensor.solcast_24hrs_forecast_garage")[1:-1].split(',') | map('int') | list %}
{% set b = states("sensor.solcast_24hrs_forecast_huis")[1:-1].split(',') | map('int') | list %}
{{a}}
{% set ns = namespace(items = []) %}
{% for i in range(a | length) %}
  {% set ns.items = ns.items + [a[i] + b[i]] %}
{% endfor %}
{{ ns.items }}

I get this

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 96, 304, 561, 832, 1117, 1414, 1584, 1583, 1567, 1554, 1500, 1412, 1415, 1492, 1373, 1074, 885, 739, 559, 362, 184, 67, 12, 0, 0, 0, 0, 0, 0, 0, 0]


  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 27, 126, 747, 1255, 1911, 2366, 2917, 3204, 3165, 3119, 3093, 2983, 2806, 2793, 2909, 2676, 2129, 1778, 1519, 1243, 869, 509, 67, 12, 0, 0, 0, 0, 0, 0, 0, 0]

What do you mean the solution looks weird, it’s doing that you asked. It’s adding the 2 lists. The fact that it’s vertical is meaningless if that’s what you mean.

@petro and @mobile.andrew.jones Thanks for your help
I only had to change

{% for i in range(a) %}

in

{% for i in range(a | length) %}

Just make sure in your actual template - I assume it’s a template sensor -
make sure you use:

state: >-
 {% all your jinja code from the template editor %}
 {% doesn't matter how many lines there are %}
 {% the output will be all be a single line %}

And that will remove all the blank lines.

1 Like