Change Colour on a label given for a given Status

Hello, I am trying to do something super simple for all but have been continuously failing.
I want to change the colour of the “ON/OFF”. Attached you will see the YAML. Please send me the full command to be able to control the colouring based on the text. Example: When its ON to make the “ON” text red. When its OFF to make no change and keep it as it is.

YAML:
component: oh-label-item
config:
action: analyzer
actionAnalyzerCoordSystem: time
actionAnalyzerItems:
- Boiler_Pump_Running
icon: iconify:mdi:pump
item: Boiler_Pump_Running
title: “Bolier Pump:”
slots: null

Welcome to openHAB!

See this thread for an example:

Hello Justin! Thans for taking the time! I actually have seen this comments before I created this thread and the reason was because I was not able to adapt to what the comment said. I am not a developer nor know any coding (I understand very basic stuff but have managed to do some stuff on openhab). Given the YAML i posted above, how would you change it in order to make it work?
Many thanks!

  - component: oh-list-card
            config:
              mediaList: false
              accordionList: false
            slots:
              default:
                - component: oh-label-item
                  config:
                    action: analyzer
                    actionAnalyzerCoordSystem: time
                    actionAnalyzerItems:
                      - Boiler_Pump_Running
                    icon: iconify:mdi:pump
                    item: Boiler_Pump_Running
                    title: "Bolier Pump:"
                    slots:
                      after:
                        - component: Label
                          config:
                            text: =items.Boiler_Pump_Running.state
                            style:
                              color: red```

What you have just posted is correct except for two small changes:

  1. In yaml the level of the indentations is part of the syntax. If you have too many or too few indentations on a line then your syntax is incorrect. In this case you have the label item’s slots property lined up wrong. It should be on the same level as the config property, not on the same level as the other keys under the config.

If you fix just that you should see that the whole things works, except that you will have a little black dash after the red text. That dash is there because the label item is still trying to show an automatic label which you don’t want.

  1. The first thing the post I linked to says is “using the more generic oh-list-item” so you have to change your oh-label-item component to an oh-list-item. That will also get rid of the black dash.

You are a star! I made the changes and it is working now BUT, Now the On/Off is red in both cases. I would like to colour only when its ON ofr example.
After that i think iam all set!

For this you want to use an expression in the value for the color:

If you just want it to be red when the boiler state is on then you can use a shorthand expression such as:

color: =(items.Boiler_Pump_Running.state == "ON") && "red"

The && is a logical AND, and the way that expression is processed the parser looks at the first part (items.Boiler_Pump_Running.state == "ON") which is a boolean test so it can only either equal true or false. In order for an AND expression to be true the first part must always be true, so if this first part is false the parses just stops and returns false which will cancel color property and you’ll get the default text color. If the first part is true, then the parser looks at the second part which is just “red” (and any non-zero, defined value is also true). The trick here is that the parser returns the second value of the AND so the final result, if the boiler state is ON, is “red” which is the value you want for the color.

2 Likes

Thank you again for the info! I think I am getting there. Now my issue is that the when I put the slots: part the state chnages from ON/OFF to CLOSED/OPEN. The colour coding is correct but now I see values are different.

component: oh-list-item
config:
  action: analyzer
  actionAnalyzerCoordSystem: time
  actionAnalyzerItems:
    - Boiler_Pump_Running
  icon: iconify:mdi:pump
  item: Boiler_Pump_Running
  title: "Bolier Pump:"
slots:
  after:
    - component: Label
      config:
        text: =items.Boiler_Pump_Running.state
        style:
          color: =(items.Boiler_Pump_Running.state == "CLOSED") && "red"

Note that if I change the “CLOSED” to “ON” then the color coding does not work.
Any ideas?

Also bear in mind that the item Boiler_Pump_Running has Metadata description as per below:

You have to use displayState instead of state when you want to access the mapped label.
You could also use @'Boiler_Pump_Running' which is a shortcut for ‘item.stateDescription and if not available item.state’

1 Like

Perfect it worked! Thank you all for your help!