There’s probably no pressing need to make sun
a namespace variable (because its value isn’t calculated within any for-loop) but you can certainly do that for, I guess, the sake of neatness.
Just keep in mind that they are NOT precisely the same thing. Paste this into the Template Editor and you’ll see that the one based on now()
updates every second whereas the other one, based on sensor.time
is every minute.
{{ now().timestamp() }}
{{ states.sensor.time.last_updated.timestamp() }}
{{ now().timestamp() | timestamp_local() }}
{{ states.sensor.time.last_updated.timestamp() | timestamp_local() }}
For many time calculations, every minute is ‘good enough’. For calculations that are comparing times down to the second, that’s not ‘good enough’.