Template Sensor Self Referencing - Logs Fill With Errors

When using self referencing in Template Sensors, the error log fills with self referencing errors:

For Example, a template sensor produces a list:

{{[solar_generation, grid_import, grid_export, battery_in, battery_out, grid_to_battery, battery_to_grid, solar_to_grid, solar_to_battery, solar_to_house, battery_to_house, grid_to_house, total_to_house]}}

Attributes are then set self referencing the template list:

        solar_generation: >
            {{(this.attributes.power_flow)[0]}}
        grid_import: >
            {{(this.attributes.power_flow)[1]}}
        grid_export: >
            {{(this.attributes.power_flow)[2]}}
        battery_in: >
            {{(this.attributes.power_flow)[3]}}
        battery_out: >
            {{(this.attributes.power_flow)[4]}}
        grid_to_battery: >
            {{(this.attributes.power_flow)[5]}}
        battery_to_grid: >
            {{(this.attributes.power_flow)[6]}}
        solar_to_grid: >
            {{(this.attributes.power_flow)[7]}}
        solar_to_battery: >
            {{(this.attributes.power_flow)[8]}}
        solar_to_house: >
            {{(this.attributes.power_flow)[9]}}
        battery_to_house: >
            {{(this.attributes.power_flow)[10]}}
        grid_to_house: >
            {{(this.attributes.power_flow)[11]}}
        total_to_house: >
            {{(this.attributes.power_flow)[12]}}

But the log is filled with errors:

The sensor and all the attributes work as expected.

Thank you.

Where is power_ flow brought in from?

Thanks for the reply.
power_flow is an attribute of the template sensor that is being self referenced and produces a list:

{{[solar_generation, grid_import, grid_export, battery_in, battery_out, grid_to_battery, battery_to_grid, solar_to_grid, solar_to_battery, solar_to_house, battery_to_house, grid_to_house, total_to_house]}}

Therefore

‘’'{{(this.attributes.power_flow)[0]}}

separates out the solar_generation output, and it works fine. But the logs don’t seem to like it. I’ve attempted self referencing before with the same results.

Try replacing “this.attributes” with state_attr.
If the error occurs at HA startup - probably the sensor is not created yet.

Reading the documentation, I thought that this.attributes was the recommended modern way, but I’ll try the other method. Thank you.

… with “‘this’ variable may be None” feature. Great way.
You may keep using this ‘modern way” - but then provide checks for none or default values.