Next_dawn next_dusk in automation

I have created 2 sensors in yaml with both templates:

- platform: template
  sensors:
    period_of_day:
      friendly_name: 'period of the day'
      value_template: >-
        {% if (as_timestamp(states.sun.sun.attributes.next_dusk)) - (as_timestamp(states.sun.sun.attributes.next_setting)) < 0 %}
          dusk
        {% elif (as_timestamp(states.sun.sun.attributes.next_rising)) - (as_timestamp(states.sun.sun.attributes.next_dawn)) < 0 %}
          dawn
        {% elif (states.sun.sun.attributes.elevation) < 0 %}
          night
        {% else %}
          day
        {% endif %}
      icon_template: >-
        {% if is_state('sensor.period_of_day', 'day') %}
          mdi:weather-sunny
        {%- elif is_state('sensor.period_of_day', 'dawn') %}
          mdi:weather-sunset-down
        {%- elif is_state('sensor.period_of_day', 'night') %}
          mdi:weather-night
        {%- elif is_state('sensor.period_of_day', 'dusk') %}
          mdi:weather-sunset-up
        {%- else %}
          mdi:alert-circle
        {%- endif %}

- platform: template
  sensors:
    period_of_day2:
      friendly_name: 'period of the day 2'
      value_template: >-
        {% set rising = (as_timestamp(states.sun.sun.attributes.next_rising)) < (as_timestamp(states.sun.sun.attributes.next_dawn)) %}
        {% set setting = (as_timestamp(states.sun.sun.attributes.next_dusk)) < (as_timestamp(states.sun.sun.attributes.next_setting)) %}
        {%- if is_state('sun.sun', 'above_horizon') %}
          day
        {%- elif is_state('sensor.period_of_day2', 'day') and setting %}
          dawn
        {%- elif is_state('sensor.period_of_day2', 'dawn') and not setting %}
          night
        {%- elif is_state('sensor.period_of_day2', 'unavailable') and is_state('sun.sun', 'below_horizon') %}
          night
        {%- elif is_state('sensor.period_of_day2', 'night') and rising %}
          dusk
        {%- else %}
          {{ states('sensor.period_of_day2') }}
        {%- endif %}
      icon_template: >-
        {% if is_state('sensor.period_of_day2', 'day') %}
          mdi:weather-sunny
        {%- elif is_state('sensor.period_of_day2', 'dawn') %}
          mdi:weather-sunset-down
        {%- elif is_state('sensor.period_of_day2', 'night') %}
          mdi:weather-night
        {%- elif is_state('sensor.period_of_day2', 'dusk') %}
          mdi:weather-sunset-up
        {%- else %}
          mdi:alert-circle
        {%- endif %}

template from @mastermarkush works great:
image

template from @mama_mia I can’t get to work
image

run in template tab:

There is something missing here.

Also, I don’t understand why the template from @mastermarkush would be a problem
???

Thanks anyway…