Lovelace - Change the icon based on states

Hi all
I would like to change the icon based on the state of the sensor.life360_rd_baterry_charging]

I have created the below script but the system does not show any icon, Do you have some suggestions?

 - type: icon
            # entity: sensor.life360_rd_battery_charging
            # style: {color: black, left: 30%, top: 90%} 
            # icon: mdi:lock 
   icon: >-
      {% if is_state("sensor.life360_rd_battery_charging", "False") %}
          mdi:lock-open-outline
      {% else %}
          mdi:lock
      {%- endif %}
   style: {color: black,  left: 10%, top: 35%}

Where are you putting this?

In a lovelace card?

What type?

Which custom card are you using that supports templating?

Hi Tom_I,

Yes, I am using in a lovelace crd.
Look at my complete picture-elements.

 - type: picture-elements
        image: /local/RD.jpg
        elements:
               
          - type: state-label
            entity: device_tracker.life360_oescafandrista
            prefix: "Local: "
            style: {color: black, left: 15%, top: 10%}
            
          # - type: state-label
          #   entity: sensor.life360_rd_baterry
          #   style: {color: black, left: 92%, top: 15%}
          # - type: icon
          #   icon: mdi:battery
          #   style: {color: black, left: 85%, top: 15%}
          - type: custom:battery-entity
            name: " "
            entity: sensor.life360_rd_baterry
            style: {color: white,  left: 10%, top: 90%}

          - type: icon
            # entity: sensor.life360_rd_battery_charging
            # style: {color: black, left: 30%, top: 90%} 
            # icon: mdi:lock 
            icon: >-
              {% if is_state("sensor.life360_rd_battery_charging", "False") %}
                mdi:lock-open-outline
              {% else %}
                mdi:lock
              {%- endif %}
            style: {color: black,  left: 10%, top: 35%}

Icon elements in the picture elements card are not natively templatable.

You would have to use something like card-templater.

IS possible to do that in automation?

No. There is no “set_icon” service.