Template help - give colors to the certain result of comparison status1 to status2

Hi, can someone help please with the following template.

I’m trying to give colors to the certain result of comparison status1 to status2.
If status1 is below status2 with a value between 0 and -250, a color must show. If it is between -250 and -500, another color.

But also the way around: if status1 is ABOVE the value of status2, up to and including +250, it must also be given a color, just as it is between +250 and +500.

I just don’t know how to compare this to status2 in this template?! What I have is:
but of course isn’t working.

     {% set status1 = states('sensor.value') %}
     {% set status2 = states('sensor.value_ath') %}

     {% if status1 > 0 and -250 > status2 %}
      lightorange
     {% elif status1 > -250 and -500 > status2 %}
      orange
     {% elif status1 > 0 and 250 > status2 %}
      lightgreen
     {% elif status1 > 250 and 500 > status2 %}
      lightgreen
     {% else %}
      white
     {% endif %}

any help is appreciated.

Something like this?


          {% set status1 = 0 %}
          {% set status2 = 100 %}
          {% if status1 < status2 %}
            {% if status1 <= 0 and status1 >= -250 %}
              lightorange
            {% elif status1 < -250 and status1 >= -500 %}
              orange
            {% else %}
              blue?
            {% endif %}
          {% elif status1 > status2 %}
            {% if status1 > 0 and status1 <= 250 %}
              lightgreen
            {% elif status1 > 250 and status1 <= 500 %}
              green
            {% else %}
              black?
            {% endif %}
          {% else %}
            white
          {% endif %}

Place your sensors in the variables

Make sure to include an int or float filter on each of your status variable definitions so you get mathematically accurate comparisons.

1 Like

Hm it feels like I’m almost there, this is my testing so far.

is the template correct when I say:

first check: is status1 greater than status2? yes? then the value is checked, is it greater than 250 (ORANGE) or 500 (RED)?
second check: is status2 smaller then status2? yes? then the value is checked, is it smaller than -250 (GREY) or -500 (BLUE)?

{% set status1 = states('sensor.value') | float %}
{% set status2 = states('sensor.value_ath') | float %}

{% if status1 > status2 < 250 %}
orange
{% elif status1 > status2 < 500 %}
red
{% elif status1 < status2 > -250 %}
grey
{% elif status1 < status2 > -500 %}
blue

{% else %}
white
{% endif %}

but is it still not 100% correct because the result (compare status1 with status2) = -48 at this moment.
it gives me grey, but when I change -250 to -600 for instance, it also gives me grey ?!

thanks for thinking along, but it feels a bit complicated and longer than necessary.

It is difficult to help when we don’t have a “truth table” to compare against or even any idea what the possible value ranges are for these statuses.


What do you mean by “with a value” and “it is between”? Are you referring to status1, status2 or something else?

What do you mean by “the result”? How is a comparison (>, <, <=, >=) returning anything but true or false? If you are performing other calculations you need to show/describe them.

something comes to mind as I write a response to you.

I’ve got 2 sensors which reports a certain EUR value (currency), so for instance status1 is 300 EUR. Status2 is the all-time-high of status1 so if status1 changes to 500 EUR, status2 will change too, to the same value.

So what I just thought; a 250 or 500 EUR difference above status2 is not possible. I’m sorry for the confusing.
But I still want to know the color when status1 is below status2.

So if status1’s value has a difference with status2, between 0 and -250 EUR, I want to see a specific color. The same for the difference between -500 and -750 EUR.

status1 has a value at this moment of: 5733 EUR
status2: 5794 EUR

the difference is -61 EUR

so when I try a template like this I would expect to see green but I see blue in the template tester?!
because it is closer to -250 (in the between 0 and -250 range) than between -250 and -500

{% set status1 = states('sensor.value') | float %}
{% set status2 = states('sensor.value_ath') | float %}

{% if status1 < status2 < -250 %}
green
{% elif status1 < status2 > -500 %}
blue
{% else %}
red
{% endif%}

Then you’re doing it completely wrong.

{% set status = ((states('sensor.sensor1') 
| float) - (states('sensor.sensor2') | float)) %}

Do the IFs

1 Like

Testing again and it looks like this one works
I created a new sensor based on the outcome of sensors: status1 - status2. Like this

sensor:
      - name: valuedif
        unique_id: valuedif
        icon: mdi:currency-eur
        unit_of_measurement: "€"
        state: >
         {{ ( states('sensor.value')|float(0) - states('sensor.value_ath')|float(0)) | round(2) }}
 

And then put the new valuedif sensor like this:

{% set status = states('sensor.valuedif') | int %}
 {% if status > -100 %}
  blue
 {% elif -100 > status > -250 %}
  orange
 {% elif -250 > status > -500 %}
  red
{% else %}
 grey
{% endif %}

Dont ask me why because I don’t understand the < and > here

That template will output grey for a difference of exactly -100 or -250.

In English, the statements are:

  • If the status is greater than -100 (e.g. -50)
  • Elseif the status is between -100 and -250 (but not including either)
  • Elseif the status is between -250 and -500 (but not including either)

Suggest you make the second and third statements:

 {% elif -100 >= status > -250 %}

and

 {% elif -250 >= status > -500 %}

which will output orange for exactly -100 and red for exactly -250.

1 Like