Strange Themes automation problem

I’ve been trying to configure automation for my themes,
I can set them manually, but the automation is not working at all.

- alias: Automatic Theme Change
  trigger:
      - platform: homeassistant
        event: start
      - platform: state
        entity_id: sun.sun
        to: above_horizon
      - platform: state
        entity_id: sun.sun
        to: below_horizon
  action:
      - service_template: frontend.set_theme
        data_template:
          name: >
            {% if states.sun.sun.state == "above_horizon" %}
              clear
            {% else %}
              night 
            {% endif %}

frontend:
themes: !include themes.yaml

The first thing is that you need to make sure you post your code properly formatted for the forum.

But by wading thru your un-formatted code I think I see the issue. try this:

  - alias: Automatic Theme Change
    trigger:
      - platform: homeassistant
        event: start
      - platform: state
        entity_id: sun.sun
        to: 'above_horizon'
      - platform: state
        entity_id: sun.sun
        to: 'below_horizon'
    action:
      - service: frontend.set_theme
        data_template: >
          name: 
            {% if states.sun.sun.state == "above_horizon" %}
              clear
            {% else %}
              night
            {% endif %}

thank you for your reply, i have it correctly formatted now. after trying your fix,HA gives me an error and the automation don’t appear.

crap! I see the error…I think…try this:

- alias: Automatic Theme Change
    trigger:
      - platform: homeassistant
        event: start
      - platform: state
        entity_id: sun.sun
        to: 'above_horizon'
      - platform: state
        entity_id: sun.sun
        to: 'below_horizon'
    action:
      - service: frontend.set_theme
        data_template:
          name: >
            {% if states.sun.sun.state == "above_horizon" %}
              clear
            {% else %}
              night
            {% endif %}

if that doesn’t work then tell me what the error says.

General question:
According to warning, should it be
if states.sun.sun.state ==
or
if states('sun.sun') ==
?