I’ve just lost a few hours searching for a solution to the same problem. Even an example on the Home Assistant documentation has it wrong (https://www.home-assistant.io/docs/automation/condition/):
- condition: template
value_template: '{{ state_attr('sun.sun', 'elevation') < 4 }}'
I would like to modify the documentation, but I’m not familiar at al with github
PS: http://www.yamllint.com/ is also a good place to check your yaml