Change icon when state changes

I would like to change the icon picture in a lovelace dashboard when the state of a presence-detection changes.
I cannot find an example of how to do that. Please help

Hi, take a look:

(search for " state_image")

Or old school:
define a template sensor on your own

Thank you for your answer, but somehow I don’t get it. Syntax kills me.
The picture are taken from customize.yaml and I don’t understand why the pictures are not taken from this code. Location = 100% correct .
views:

  • title: Thuis
    cards:
    #- type: entity-filter
    • type: glance
      entities:
      • type: picture-entity
        entity: device_tracker.8cf5a3e8b6e9
        icon: /local/images/jme.jpg
      • entity: device_tracker.4c66413ac41a
        icon: wife.png
      • entity: device_tracker.409c282b1587
      • type: picture-entity
        entity: device_tracker.4491608d7644
        tap-action: toggle
        state-image:
        ‘home’: /local/images/daughter.jpg
        ‘not_home’: /local/images/daughter.jpg

I can’t tell because of the formatting (and I will not chastise, that’s done enough here) but… check your spacing indentation and lose the single quotes on home:: and not_home: and see what happens.

You are right about the formatting and I am sorry about that. I copy my code from the original yaml file (I use notepad++), select all lines and push the preformatted text button. That is how I the instruction above every topic says ( Or my english is too bad to understand the text).

Losing double quotes or adding single quotes doesn’t help.

I use this in a template sensor:

    ready_to_arm:
      value_template: '{{ states.sensor.home_alarm_keypad.attributes["ready"] }}'
      friendly_name: Ready to Arm?
      icon_template: '{%- if is_state("sensor.ready_to_arm", "False") %}mdi:lock-open-outline{% else %}mdi:lock{%- endif %}'

The advantage is that it shows properly in both, the legacy UI and Lovelace.

Not sure, though, if it needs to be amended to work with the latest HA version, i.e. > 0.80.x, due to a breaking hange for template sensors.

No problem on the formatting… I get it. Now, is there a reason “icon: wife.png” doesn’t have a full path and your other icons do?

That is just because I first want to try whether the code is right (with daugther).
I use full path with jme because the icon differs from that in customize.yaml

But as said, it still uses the icon/picture from customize.yaml.

Im trying to get the icon to change when I turn on and off a switch.

entities:
  - entity: switch.doorbell_chime_status
    name: deurbel geluid
    icon: >-
        {% if is_state('switch.doorbell_chime_status', 'off') %}
          hass:bell-remove-outline
        {% else %}
          hass:bell-ring
        {% endif %}

What am I doing wrong?