Automatisierung mit 'If' und 'State'

I have created the following automation:

# Wohnzimmer-Lampensteckdose ***********************
 - alias: Lampe ein-aus-ein-aus
     trigger:
       - platform: time
          at: '05:30:00'
        - platform: time
          at: '23:59:00'
        - platform: state
          entity_id: binary_sensor.hell 
          to: 'on' 
        - platform: state
          entity_id: binary_sensor.dunkel 
          to: 'on' 
          action:
     service_template: >
          {% if trigger.platform == 'time' %}
             switch.turn_{{ 'on'  if now().hour == 5}}  
             switch.turn_{{ 'off' if now().hour == 23 }}
          {% endif %}
          {% if trigger.platform == 'state' %}
             switch.turn_{{ 'off' if trigger.entity_id == 'binary_sensor.hell'}}
             switch.turn_{{ 'on'  if trigger.entity_id == 'binary_sensor.dunkel'}}
          {% end
     entity_id: switch.38272685dc4f22ead1ee

but it does not do what it should. Where did I make a mistake? can someone help me?

The automation should switch ON the lamp at first at 5:30 and switch OFF at the latest 23:59.
If the brightness sensor detects ‘dark’, the lamp should also be switched on. If the brightness sensor detects ‘bright’, the lamp should be switched off.
The result should be that the lamp is on from 5:30 in the morning until sufficient daylight is available and turns ON when it gets dark again and then turns off again at 23:59.

Many thanks for the help

You first need to correct the indenting otherwise it will be misinterpreted. Compare this to what you have.

# Wohnzimmer-Lampensteckdose ***********************
 - alias: Lampe ein-aus-ein-aus
   trigger:
     - platform: time
       at: '05:30:00'
     - platform: time
       at: '23:59:00'
     - platform: state
       entity_id: binary_sensor.hell 
       to: 'on' 
     - platform: state
       entity_id: binary_sensor.dunkel 
       to: 'on' 
   action:
     service_template: >
       {% if trigger.platform == 'time' %}
          switch.turn_{{ 'on'  if now().hour == 5}}  
          switch.turn_{{ 'off' if now().hour == 23 }}
       {% endif %}
       {% if trigger.platform == 'state' %}
          switch.turn_{{ 'off' if trigger.entity_id == 'binary_sensor.hell'}}
          switch.turn_{{ 'on'  if trigger.entity_id == 'binary_sensor.dunkel'}}
       {% end
       entity_id: switch.38272685dc4f22ead1ee