Template numeric comparison not working?

I’m hoping someone can help me keep my sanity :slight_smile:

The below icon colour logic isn’t providing expected results:

  1. The uptime is 7 days and showing green, but should be an amber icon?
  2. The storage utilisation is 94.86% and showing red, but should be a green icon?

Screenshot 2022-06-29 at 09.07.29

type: custom:vertical-stack-in-card
mode: horizontal
cards:
  - type: custom:mushroom-template-card
    primary: UDM Pro
    icon: mdi:router-network
    icon_color: blue
    layout: horizontal
  - square: false
    columns: 4
    type: grid
    cards:
      - type: custom:vertical-stack-in-card
        horizontal: true
        cards:
          - type: custom:mushroom-template-card
            secondary: >-
              Uptime - {{ relative_time
              (as_datetime(states('sensor.unifi_network_uptime'))) }}
            entity: sensor.unifi_network_uptime
            layout: vertical
            icon: mdi:clock-time-nine-outline
            icon_color: |-
              {% if "{{states('sensor.unifi_network_uptime') >20 }}" %}
                green
              {% else %}
                amber
              {% endif %}
      - type: custom:vertical-stack-in-card
        horizontal: true
        cards:
          - type: custom:mushroom-template-card
            secondary: >-
              Storage -  {{states('sensor.unifi_network_storage_utilization')
              }}%
            entity: sensor.unifi_network_storage_utilization
            layout: vertical
            icon: mdi:database
            icon_color: >-
              {% if "{{ states('sensor.unifi_network_storage_utilization') | int
              > 98 | int }}" %}
                red
              {% else %}
                green
              {% endif %}
            icon_color: >
              {% if states('sensor.unifi_network_uptime')|int(0) > 20 %}
                green
              {% else %}
                amber
              {% endif %}
            icon_color: >
              {% if states('sensor.unifi_network_storage_utilization')|int(0) > 98 %}
                red
              {% else %}
                green
              {% endif %}

Thanks Tom, it’s all working. Please can you explain the changes?

  1. {% and %} tell Jinja that this is the start ({%) and end (%}) of a non-result-returning line of code.

    {{ and }} tell Jinja that this is the start ({{) and end (}}) of a result-returning line of code.

    You put {{ }} inside {% %}. That’s not valid. It’s one or the other for the entire line.

  2. Wrapping items in quotes changes them from a function/object to a literal string of characters. For example, if you had an apple in code, it would have properties. Like taste, smell, color, etc. When you access the apple, you access it as an object. So to get the taste, you’d say apple.taste. Now when you wrap the word apple in quotes. You literally change it from the object apple, to a 5 letter word "apple". This just makes a string, which has none of those extra properties. You also did that in the wrong place a few times.

1 Like

Thanks both. I’ve just noticed that the network uptime isn’t working - it’s my mistake as I didn’t include the relative_time in the logic. I have amended to the following, but it still isn’t working?

icon_color: >-
              {% if
              (relative_time(as_datetime(states('sensor.unifi_network_uptime'))))|int(0)
              > 2 %}
                green
              {% else %}
                amber
              {% endif %}

relative_time returns a string with units, you can’t compare time like that.

              {% if (now() - states('sensor.unifi_network_uptime') | as_datetime).days > 2 %}
                green
              {% else %}
                amber
              {% endif %}

Thanks Petro, I wasn’t aware - that’s all working now. Really appreciate your help.