Roundrobin energy dispatch and script shared variable (solved)

Hello,
I have been trying to find a solution to create a Round Robin energy dispatch to go around my flat power limitation.
As I could not find any ready made solution, I had to start a new one (but I failed and I need help).
The problem seems simple:

  • I have seven 1kW radiators
  • I can only draw 5kW
  • I want to heat all the rooms in the flat which is well isolated and does not required radiators to be on all the time.
  • Some time I use other electric equipments what would required to reduce the number of activate radiators dynamically.

I have implemented a solution by zone, but it fails to keep all the room heated. It works but provides a poor service.

In order to do so I need to manage several variables and lists (see mine below) which will be used to implement the system.
The idea is simple.

  • Thermostat: it pilots a virtual switch created via a helper instead of piloting directly the radiator.
  • Virtual Switch: Trigger and automation that update a list of radiator desired mode (radiator_request_mode) and trigger a RoundRobin script
  • Script:
    • implement the logic that will create a list of radiator active mode (radiator_actual_mode) based on the request mode, the RoundRobin schedule and the number of radiators which can be active at the same time.
    • trigger the actual radiator switches based on the list of radiator active mode.

An other automation triggers the RoundRobin script every 5mn.

My Problem

I get a syntax error that I am not able to correct even after reading a lot of documentation. It seems that manipulated persistent lists via template has not been much documented.
Could someone points me on my error?
Any help would be much appreciated. Here is my script and the reported error.

Reported error when trying to save

Message malformed: string value is None for dictionary value @ data[‘alias’]


alias: Round Robin Run
sequence:
  - service: var.set
    data:
      entity_id: var.radiator_actual_mode
      data_template:
        value: >-
          {% set radiator_actual_mode = [0,0,0,0,0,0,0] %}
          {% for room,index in var.radiator_list_inv() %}
            {% set radiator_actual_mode[(index+var.round_robin_index)%var.radiator_total_count] = var.radiator_request_mode[(index+var.round_robin_index)%var.radiator_total_count] %}
          {% endfor %}
         {{radiator_actual_mode}}
mode: queued
icon: mdi:radiator-off
max: 10

-------------- variable and list ---------------

  radiator_max_active:
  radiator_total_count:
  radiator_list:
    initial_value: [ 'tv', 'office', 'bedroom','lounge_window', 'kitchen' , 'bathroom' ,  'lounge_entrance' ]
  radiator_list_inv:
    initial_value: { 'tv' :0, 'office' :1, 'bedroom' :2,'lounge_window' :3, 'kitchen' :4, 'bathroom' :5, 'lounge_entrance' :6 }
  radiator_actual_mode:
    initial_value: [ 0 , 0 , 0 , 0 , 0 , 0 , 0 ]
  radiator_request_mode:
    initial_value: [ 0 , 0 , 0 , 0 , 0 , 0 , 0 ]
  radiator_max_active:
    initial_value: 4
    restore: false
  round_robin_index:
    initial_value: 0

I have given up and moved to Python.
My solution is now in Pyscript and works.
The code can be found here:
GitHub - dominig/pyscript_power_load_balance: Pyscript for Home Automation enabling a power dispatch via power balancing