Issue when referencing attributes in a template sensor

Because the state option is computed before the attributes option.

Change your sensor’s state template so that it doesn’t reference its own attributes.