Templating?

I need alot of help on templates.
I’m not completely new to home assistant, but don’t know anything to coding. I have a cupboard with a led light and a switch that triggers the light when the door opens.
I’m want to create a automation with lambda, that does the following:
When the door opens during the day or sun above horizon, then the light is at full brightness. When the door opens at night between 20:00 and 23:00, the brightness is at 50%. And when the door opens at night between 23:00 and 05:00, the brightness is 20%.
Figure that templates is the only way to get this right, but have no idea where to start with it.