Sensor not showing the correct icon

I created a sensor that should show the combined status of two binary sensors, that is working as intended. However , I can´t get the Icon template to behave as I´d like.

It works fine in the template editor:

But if I check the state, wrong icon is showing:
image

What am I doing wrong?

The sensor:

  - platform: template
    sensors:
      front_door_combined:
        friendly_name: "Ytterdörr"
        entity_id:
          - binary_sensor.1_front_door_lock
          - binary_sensor.2_front_door
        value_template: >-
          {% if not states('binary_sensor.2_front_door') == 'undefined' %}
            {% if is_state('binary_sensor.2_front_door', 'off') %}
              {% if is_state('binary_sensor.1_front_door_lock', 'off') %}
                Låst
              {% else %}
                Stängd
              {% endif %}
            {% else %}
              Öppen
            {% endif %}
          {% else %}
            Undefined
          {% endif %}
        icon_template: >-
          {% set front_door = states('sensor.front_door_combined') %}
          {% if front_door == 'Låst' %}
            mdi:cellphone-lock
          {% elif front_door == 'Stängd' %}
            mdi:door-closed
          {% elif front_door == 'Öppen' %}
            mdi:door-open            
          {% else %}
            mdi:alert
          {% endif %}

Maybe a bad idea to determine the icon for a sensor based on the state of the same sensor. Not sure that the sensor state is always updated before the icon is determined.

Ok, that seems to be the case.It works if I use the state of binary sensors for determining the icon.

Got any better idea how to accomplish the icon change?
The goal is to simplify the card showing the state of doors and window status.
Any suggestions would be appreciated.

You could use ‘custom ui’ to accomplish this.