Thank you @TheOtherGlen. That was the trick and now working as expected. See below for code and results.
{% set sa_ns = namespace() %}
{% set sa_ns.sa_today_import = 0 %}
{% set sa_ns.sa_today_export = 0 %}
{% set count = 0 %}
{% for count in range(108,144) %}
{% if (states.sensor.sa_data_by_5min.attributes.data[count].energy_consumed) == None %}
{% set e_c = 0 %}
{% set e_g = 0 %}
{% else %}
{% set e_c = states.sensor.sa_data_by_5min.attributes.data[count].energy_consumed %}
{% set e_g = states.sensor.sa_data_by_5min.attributes.data[count].energy_generated %}
{% endif %}
{{ count }}, {{ states.sensor.sa_data_by_5min.attributes.data[count].t_stamp }}
Current interval {{ e_c }}, {{ e_g }}
Cumulative import/export totals: {{ sa_ns.sa_today_import }}, {{ sa_ns.sa_today_export }}
{% if e_c > e_g %}
consumption is greater
{% set e_i = (e_c - e_g) %}
{% set e_e = 0 %}
Net energy imported {{ e_i }}
{% set sa_ns.sa_today_import = (sa_ns.sa_today_import + e_i) %}
{% elif e_c < e_g %}
generation is greater
{% set e_e = (e_g - e_c) %}
{% set e_i = 0 %}
Net energy exported {{ e_e }}
{% set sa_ns.sa_today_export = (sa_ns.sa_today_export + e_e) %}
{% else %}
equal
{% endif %}
New cumulative import/export totals: {{ sa_ns.sa_today_import }} and {{ sa_ns.sa_today_export }}
{% endfor %}
108, 2022-01-06 09:00:00
Current interval 190, 161
Cumulative import/export totals: 0, 0
consumption is greater
Net energy imported 29
New cumulative import/export totals: 29 and 0
109, 2022-01-06 09:05:00
Current interval 263, 267
Cumulative import/export totals: 29, 0
generation is greater
Net energy exported 4
New cumulative import/export totals: 29 and 4
110, 2022-01-06 09:10:00
Current interval 226, 232
Cumulative import/export totals: 29, 4
generation is greater
Net energy exported 6
New cumulative import/export totals: 29 and 10
111, 2022-01-06 09:15:00
Current interval 161, 163
Cumulative import/export totals: 29, 10
generation is greater
Net energy exported 2
New cumulative import/export totals: 29 and 12
112, 2022-01-06 09:20:00
Current interval 169, 182
Cumulative import/export totals: 29, 12
generation is greater
Net energy exported 13
New cumulative import/export totals: 29 and 25
113, 2022-01-06 09:25:00
Current interval 193, 187
Cumulative import/export totals: 29, 25
consumption is greater
Net energy imported 6
New cumulative import/export totals: 35 and 25
114, 2022-01-06 09:30:00
Current interval 199, 203
Cumulative import/export totals: 35, 25
generation is greater
Net energy exported 4
New cumulative import/export totals: 35 and 29
115, 2022-01-06 09:35:00
Current interval 288, 291
Cumulative import/export totals: 35, 29
generation is greater
Net energy exported 3
New cumulative import/export totals: 35 and 32
116, 2022-01-06 09:40:00
Current interval 215, 218
Cumulative import/export totals: 35, 32
generation is greater
Net energy exported 3
New cumulative import/export totals: 35 and 35
117, 2022-01-06 09:45:00
Current interval 190, 193
Cumulative import/export totals: 35, 35
generation is greater
Net energy exported 3
New cumulative import/export totals: 35 and 38
118, 2022-01-06 09:50:00
Current interval 206, 211
Cumulative import/export totals: 35, 38
generation is greater
Net energy exported 5
New cumulative import/export totals: 35 and 43
119, 2022-01-06 09:55:00
Current interval 200, 204
Cumulative import/export totals: 35, 43
generation is greater
Net energy exported 4
New cumulative import/export totals: 35 and 47
120, 2022-01-06 10:00:00
Current interval 215, 219
Cumulative import/export totals: 35, 47
generation is greater
Net energy exported 4
New cumulative import/export totals: 35 and 51
121, 2022-01-06 10:05:00
Current interval 221, 225
Cumulative import/export totals: 35, 51
generation is greater
Net energy exported 4
New cumulative import/export totals: 35 and 55
122, 2022-01-06 10:10:00
Current interval 188, 192
Cumulative import/export totals: 35, 55
generation is greater
Net energy exported 4
New cumulative import/export totals: 35 and 59
123, 2022-01-06 10:15:00
Current interval 214, 218
Cumulative import/export totals: 35, 59
generation is greater
Net energy exported 4
New cumulative import/export totals: 35 and 63
124, 2022-01-06 10:20:00
Current interval 212, 216
Cumulative import/export totals: 35, 63
generation is greater
Net energy exported 4
New cumulative import/export totals: 35 and 67
125, 2022-01-06 10:25:00
Current interval 223, 227
Cumulative import/export totals: 35, 67
generation is greater
Net energy exported 4
New cumulative import/export totals: 35 and 71
126, 2022-01-06 10:30:00
Current interval 238, 227
Cumulative import/export totals: 35, 71
consumption is greater
Net energy imported 11
New cumulative import/export totals: 46 and 71
127, 2022-01-06 10:35:00
Current interval 264, 260
Cumulative import/export totals: 46, 71
consumption is greater
Net energy imported 4
New cumulative import/export totals: 50 and 71
128, 2022-01-06 10:40:00
Current interval 380, 381
Cumulative import/export totals: 50, 71
generation is greater
Net energy exported 1
New cumulative import/export totals: 50 and 72
129, 2022-01-06 10:45:00
Current interval 355, 368
Cumulative import/export totals: 50, 72
generation is greater
Net energy exported 13
New cumulative import/export totals: 50 and 85
130, 2022-01-06 10:50:00
Current interval 361, 365
Cumulative import/export totals: 50, 85
generation is greater
Net energy exported 4
New cumulative import/export totals: 50 and 89
131, 2022-01-06 10:55:00
Current interval 317, 319
Cumulative import/export totals: 50, 89
generation is greater
Net energy exported 2
New cumulative import/export totals: 50 and 91
132, 2022-01-06 11:00:00
Current interval 224, 228
Cumulative import/export totals: 50, 91
generation is greater
Net energy exported 4
New cumulative import/export totals: 50 and 95
133, 2022-01-06 11:05:00
Current interval 297, 301
Cumulative import/export totals: 50, 95
generation is greater
Net energy exported 4
New cumulative import/export totals: 50 and 99
134, 2022-01-06 11:10:00
Current interval 234, 238
Cumulative import/export totals: 50, 99
generation is greater
Net energy exported 4
New cumulative import/export totals: 50 and 103
135, 2022-01-06 11:15:00
Current interval 246, 250
Cumulative import/export totals: 50, 103
generation is greater
Net energy exported 4
New cumulative import/export totals: 50 and 107
136, 2022-01-06 11:20:00
Current interval 230, 234
Cumulative import/export totals: 50, 107
generation is greater
Net energy exported 4
New cumulative import/export totals: 50 and 111
137, 2022-01-06 11:25:00
Current interval 230, 234
Cumulative import/export totals: 50, 111
generation is greater
Net energy exported 4
New cumulative import/export totals: 50 and 115
138, 2022-01-06 11:30:00
Current interval 0, 0
Cumulative import/export totals: 50, 115
equal
New cumulative import/export totals: 50 and 115
139, 2022-01-06 11:35:00
Current interval 0, 0
Cumulative import/export totals: 50, 115
equal
New cumulative import/export totals: 50 and 115
140, 2022-01-06 11:40:00
Current interval 0, 0
Cumulative import/export totals: 50, 115
equal
New cumulative import/export totals: 50 and 115
141, 2022-01-06 11:45:00
Current interval 0, 0
Cumulative import/export totals: 50, 115
equal
New cumulative import/export totals: 50 and 115
142, 2022-01-06 11:50:00
Current interval 0, 0
Cumulative import/export totals: 50, 115
equal
New cumulative import/export totals: 50 and 115
143, 2022-01-06 11:55:00
Current interval 0, 0
Cumulative import/export totals: 50, 115
equal
New cumulative import/export totals: 50 and 115