Picture-Elements card - "if" not working to change style

Hi all, I’m a new member but I’m using HA since a couple of years.
I cannot do a simple thing…I searched everywhere but cannot find the issue.
I want to change the color or the background color of a “state-label” element inside a picture-elements card, but my “if” doesn’t work. Is there anything wrong with the following code?

type: picture-elements
image: /local/Immagini/Sh3D/3D - piatta/sera - 0.png
elements:
  - type: state-label
    entity: sensor.lanyard_mini_hygrometer_2307_temperature
    style:
      top: 3%
      left: 16%
      color: white
      border-radius: 50%
      background-color: >
        [[[
          if (entity.state < 20) return 'gray';
          else return 'yellow';
        ]]]

I also tried:

type: picture-elements
image: /local/Immagini/Sh3D/3D - piatta/sera - 0.png
elements:
  - type: state-label
    entity: sensor.lanyard_mini_hygrometer_2307_temperature
    style:
      top: 3%
      left: 16%
      color: white
      border-radius: 50%
      background-color: '{{ "gray" if states("sensor.lanyard_mini_hygrometer_2307_temperature") < 20 else "yellow" }}'

Do you have any suggestions? Am I missing something?
Thanks!!!

take a look at this post. this might be doing just what you’re asking for:

1 Like