Hi Jacob Lee,
Setting a variable to itself doesn’t work because the variable doesn’t exist until you create it. It would be undefined there.
There you changed scope so it works in the first instance, but a new scope means new variables, so again it is undefined.
That one looks like something an AI dreamed up.
If you want a variable you can refer to in multiple places in the acript/automation, use a helper. create it, load it with your values, and it will be available regardless of scope.
There is also this available:
Trigger based template sensor to store global variables.
But to solve your actual problem Jack has the answer you might be looking for.