Change icon color on Condition

I am new and I need help …

I created “input.boolean.xxx_arm” switches for each house sensor, eg “binary_sensor.xxx”, and then created a virtual sensor “binary_sensor.xxx_arm” which is activated like this:

template:
  - binary_sensor:
      - name: "xxx arm"
        state: >
          {{ is_state('binary_sensor.xxx,'on') 
             and is_state('input_boolean.xxx_arm','on')}}

I wish in Lovelace:
if binary_sensor.xxx OFF, then
Yellow color, if input.boolean.xxx_arm ON
Gray color, if input.boolean.xxx_arm OFF
instead
if binary_sensor.xxx ON, then
Red color, if input.boolean.xxx_arm ON
Blue color, if input.boolean.xxx_arm OFF

Immagine

How can I do?

2 Likes

Hi and welcome!

I’m not aware of any possibility to do that without helpers like custom:button-card or card-mod.

I’ve already installed both and tried many times, but couldn’t find any examples that would work for me, do you have any suggestions?

Example with Javascript to change color:


type: custom:button-card
entity: binary_sensor.xxxxxxxxx
icon: mdi:gesture-tap-button
styles:
  icon:
    - color: |
        [[[
          if (entity.state == 'on') return 'red'; 
          return 'yellow'
        ]]]

example with state definition:


type: custom:button-card
entity: binary_sensor.xxxxxxxxx
icon: mdi:gesture-tap-button
state:
  - value: 'on'
    icon: mdi:alarm-light
    color: red

  - value: 'off'
    icon: mdi:alarm-light-off
    color: yellow

   

There’s a big thread named Lovelace Button Card, where you will find examples for nearly everything.

1 Like

these are the examples that I have already found but they do not lead me to understand anything!
I need the condition to change the icon color of binary_sensor.xxx! that is, where should the input.boolean.xxx_arm go?
Forgive me, I’m new to this system.

try customUI - GitHub - Mariusthvdb/custom-ui: Adapted Custom-ui for HA 110+ / HA 2021.6

examples - custom-ui/EXAMPLES.md at 053380a7f1628ea5efe8b385e7552ec92fd35b21 · Mariusthvdb/custom-ui · GitHub

Then I don’t understand your description. Is only the binary sensor „xxx“ supposed to change colours? If so, then:


styles:
  icon:
    - color: |
        [[[
          var e = states['binary_sensor.xxx'].state;
          var x = states['input_boolean.xxx_arm'].state;
          if (e == 'on' && x == 'on') return 'red'; 
          if (e == 'on' && x == 'off') return 'blue'; 
          if (e == 'off' && x == 'on') return 'yellow'; 
          if (e == 'off' && x == 'off') return 'grey'; 
          return 'pink';
        ]]]

„e“ and „x“ are variables to clear the following if/else conditions.


          return 'pink';

is a shortform for else and a kind of fallback, if no condition is met.

Forgive me, the logic of what you wrote is clear and it is what I need but, I have not the faintest idea where to put it.
I am from the Vera system, it was enough to look for a plugin or a Luup code to do actions.
Definitely HA is more complete, I connected my google home, did automation with zwave devices but, HA is not for beginners like me!
Whenever I look for something in the community, it seems to assume that everyone is an expert … I download integrations, I create directories, I copy files, I modify the now very long configuration.yaml that sometimes gives errors, then I look for other solutions that suggest others integrations with installations in an old and modern way with examples that lead to other links … I’m definitely inside a washing machine.
I end up copying and pasting nonsense codes to randomly test … it’s humiliating

My poor logic would like me to insert your code in the button, change the names and everything works … instead I’ll have to add who knows what to get to a solution, believe me, it’s frustrating.

But on the other hand: I think, every system, that allows its users to manipulate the given standards and do their own special stuff, request knowledge about how the „engine runs“. :slight_smile: But: It will become easier.
How was this implemented in the Vera system?

I really hope so

It has been a few years since the installation of my Vera system and I don’t remember well, however, each sensor has properties such as parameters and settings with fields such as icon type, color, association etc; Each sensor has by default the switch “ARMED/DISARMED”, to allow the creation of automations on condition. Unfortunately, the Vera System was very old and had been standing still for over 10 years.
vera

Thanks for your interest, I will try a few more attempts but, I would appreciate so much more help on my problem, thanks

Sounds convenient! But: Now you want all the color conditions, and this request a little bit more effort… Here is an example, how you can built your card:


type: custom:button-card
entity: binary_sensor.xxx
triggers_update: all
styles:
  icon:
    - color: |
        [[[
          var e = entity.state;
          var x = states['input_boolean.xxx_arm'].state;
          if (e == 'on' && x == 'on') return 'red'; 
          if (e == 'on' && x == 'off') return 'blue'; 
          if (e == 'off' && x == 'on') return 'yellow'; 
          if (e == 'off' && x == 'off') return 'grey'; 
          else return 'lightgrey';
        ]]]

4 Likes

Great, it works perfectly, now I know what to work on and thank you.
I will do this to all sensors and, if I may venture, in my opinion, each sensor should have by default a switch that arms or disarms it because the condition would make the automation easier for amateurs. Thank you again.

I went ahead and now I configure the floor plan :yum:, so I tried to use your code with “paper-buttons-row” but it doesn’t seem to be compatible, moreover to the touch, it should change the state of the “input_boolean” but it doesn’t happen … where am i wrong?

type: picture-elements
image: /local/piantina/casa.png
elements:
  - type: custom:paper-buttons-row
    action: call-service 
    service: input_boolean.toggle
    service_data: {}
    target:
      entity_id: input_boolean.sensorecucinaarm
    buttons:
      - entity: binary_sensor.sensore_cucina
        layout: icon
        state_icons:
          'on': mdi:motion-sensor
          'off': mdi:leak
        styles:
        button:
        color: |
          [[[
            var e = entity.state;
            var x = states['input_boolean.sensorecucinaarm'].state;
            if (e == 'on' && x == 'on') return 'red'; 
            if (e == 'on' && x == 'off') return 'white'; 
            if (e == 'off' && x == 'on') return 'yellow'; 
            if (e == 'off' && x == 'off') return 'grey'; 
            else return 'lightgrey';
           ]]]
    style:
      top: 18%
      left: 35%

I have to do this for about 12 sensors … is this the right way or is there a more “practical” method?

I don’t use that, but I think, that a) the indentation is wrong and must be


     styles:
        button:
          color: |
              …

and b) that the code must be Jinja (as in Developer Tools → Templates):


            {% set e = states('binary_sensor.sensore_cucina') %}
            {% set x = states('input_boolean.sensorecucinaarm') %}
            {% if (e == 'on' and x == 'on') %} red
            {%  elif (e == 'on' and x == 'off') %} white
            {% elif (e == 'off' and x == 'on') %} yellow
            {% elif (e == 'off' and x == 'off') %} grey
            {% else %}  lightgrey
            {% endif %}  
           

1 Like

Thanks to your help, I went ahead with my project, and made a card with all the sensors that I arm and disarm.
Now I would like to add the “secondary_info: last-changed” function but I would like it to refer to the sensor ‘binary_sensor.senso_soggiorno’, what should I do?

type: entities
entities:
  - entity: input_boolean.sensoresoggiornoarm
    name: Soggiorno
    icon: mdi:walk
    secondary_info: last-changed
    card_mod:
      style: |
        :host { 
          --paper-item-icon-color:
              {% set e = states('binary_sensor.sensore_soggiorno') %}
              {% set x = states('input_boolean.sensoresoggiornoarm') %}
              {% if (e == 'on' and x == 'on') %} red
              {% elif (e == 'on' and x == 'off') %} orange
              {% elif (e == 'off' and x == 'on') %} blue
              {% elif (e == 'off' and x == 'off') %} gray
              {% else %}  lightgrey
              {% endif %}
        }  

I’m only aware of a method by using card_mod:


type: entities
show_header_toggle: false
state_color: true
entities:
  - entity: light.flur
    name: Flurlicht
    secondary_info: true
    card_mod:
      style:
        hui-generic-entity-row:
          $: |
            .info .secondary {color: red;}
            .info .secondary::before {
               content: "Garderobe changed at: {{ as_timestamp(states.light.garderobe.last_changed)|timestamp_custom('%d.%m.%y %H:%M', true) }}";
               color: green;
               }

Hi Guys,

I’ve a picture-elements card where I’m trying to change the icon color based on a motion sensor last trigger time.

Example: If {{ relative_time(states.binary_sensor.front_motion.last_changed) }} is less than 5 minutes I want the icon color to show Red or else Green.

With the following I’m able to change the color based on the state of this sensor, could any one please help me how can I do the same with last changed time.

        icon_color: |-
          {% if is_state("binary_sensor.front_motion", "on") %}
          red
          {% else %}
          green
          {% endif %}
1 Like

{% set ls = states.binary_sensor.front_motion.last_changed %}
{{ now() - ls < timedelta(minutes=5) }}

1 Like

Thanks much @pedolsky :slight_smile:

Following is my working code

        icon_color: |-
          {% set ls = (states.binary_sensor.front_motion.last_changed) %}
          {% if ( now() - ls < timedelta(minutes=5) ) %}
          red
          {% else %}
          green
          {% endif %}

This is what i have been searching for! I do have a question for you tho. I am trying to create a garbage collection card on my dashboard, and I would like to have 1 card with 1 icon or picture displaying. when its recycle day the icon will switch from grey to blue and the following week when its yard waste it will switch from grey to green. that seems the most simple but im not sure how to attach 2 entities to 1 icon. which leaves me to believe it might be easier to get images of a blue trash can and a green trash can and display which ever boolean is currently on.