Sensor template Icon color

Hi, I find many topics about icon colors, but none of these give me the correct answer. I want to change the icon color of a sensor depending on the status value. The color must be the variable state-icon-active-color: "#5F9BEA" I want this color when state is “Open”

How should I do this?

this is the config of my sensor.

- platform: template
  sensors:
    achterdeur:
      friendly_name: 'Achterdeur'
      value_template: '{% if is_state("binary_sensor.aeon_labs_zw089_recessed_door_sensor_gen5_achterdeur", "on") %}Open{% else %}Dicht{% endif %}'
      icon_template: >-
        {% if is_state('sensor.achterdeur', 'Open') %}
          mdi:lock-open-variant
        {% else %}
          mdi:lock
        {% endif %}

If you have already defined that colour in a theme you are using just add:

state_color: true

To any Lovelace card you display that entity in, no need to put a colour in the template.

I have not added anything special to my theme config. I only want the same color icon as all standard entities when they have a ON state. I have added state_color: true but the icon still has no color at “Open” or “Close” status. This is my card.

      - type: glance
        state_color: true
        entities:
          - entity: sensor.voordeur
          - entity: sensor.achterdeur

That’s because you have defined a template sensor. Try a template binary sensor with device class: lock. That way you don’t need to define an icon template.

I now have set the sensor as binary sensor but still no color when state is changing. Sorry this is new for me.

binary_sensor: 
 - platform: template
   sensors:
     achterdeur:
       friendly_name: 'Achterdeur'
       device_class: lock
       value_template: '{% if 
 is_state("binary_sensor.aeon_labs_zw089_recessed_door_sensor_gen5_achterdeur", "on") 
%}Open{% else %}Dicht{% endif %}'

Your value template is not valid. It needs to resolve to true or false. Like this:

value_template: '{{ is_state("binary_sensor.aeon_labs_zw089_recessed_door_sensor_gen5_achterdeur", "on") }}'

Thanks, it is working now. I have a state Lock/Unlocked with a color when it is Unlocked. But still not exactly what i want, is it possible to change the state string Lock -> Dicht and Unlocked -> Open?