I have the same “issue” with some ZWave template sensors.
This seems to be caused by the template sensors being set up before all the referenced entities are ready.
Looking at your code, the sensor is trying to access states.sun.sun.attributes.elevation when states.sun.sun is still undefined (i.e. None).
It seems to be just a cosmetic issue, since - at least for me - my template sensors work as expected as soon as the referenced entities are “up”.
I think it’s safe to ignore this warning message.
That only seems to work with actual states, but not for attributes, i.e. {{ states.sun.sun.attributes.elevation }} returns the elevation value, while {{ states('sun.sun.attributes.elevation') }} returns unknown.
I’d have to look into that - until now, seeing that Jinja syntax kept me away successfully
However, in this case the warning is triggered by trying to access a not (yet) existing entity.
Adding a default value would not change that - it only would make sure that a sensible value is returned.
It should be possible to suppress these warnings using an {% if %} clause as suggested here:
I think though, that inside the if-statement, you’d have to strip the “.attributes.<whatever>” part, since the base entity is still undefined.
I got the same error as before when I tried exactly the suggested statement and then gave up trying and decided to live with the warning message…
But stripping the attributes part should do the trick.