Oh-label-cell Condition based label and color

Widgets get their item data from the api, but there’s no initial parsing. Every state is a string, regardless of the type of the item. If you want a numerical comparison in your ternary statement, then you need to first parse that string into a number.

color: '=(Number.parseInt(item[Solar_FeedIn].state) < 3) ? "green" : "red"'

Thanks Justin, that unfortunately doesn’t display thew colors. Am I missing something else?

I have this in my Overview page:

component: oh-label-cell
config:
  expandable: false
  title: Power usage of house.
  icon: oh:energy
  item: Power_Watts
  footer: Live readings
  trendItem: Power_Watts
  stateStyle:
    color: "=Number.parseFloat(items.Power_Watts.state) >= 500 ? 'red' : 'green'"
 

It works for me.

1 Like

Thank you! that makes the text change color, but I was trying to color the entire cell. Definitely closer :smiley:

Is there any documentation on the expression function? Id love to be able check if the value is negative, then the Energy Export should be 0 and red, but if its positive, green!

Wouldn’t it be if it is less than zero then it is negative?

1 Like

This changes the background colour of the cell:

component: oh-cell
config:
  on: =Number.parseFloat(items.Power_Watts.state) >= 500
  color: "=Number.parseFloat(items.Power_Watts.state) >= 500 ? 'red' : 'green'"
  action: url
  actionModal: widget:Webpage display widget
  actionModalConfig:
    title: TV Guide
    URL: https://www.yourtv.com.au/guide/nownext/
    height: "600"
  header: TV Guide
  title: TV Guide
  icon: f7:tv
  actionUrl: https://www.freeview.com.au/tv-guide
slots: null

NOTE: If you have a trend line item in the cell then the colour will not change.

1 Like

OK Cool, sorted that.

Is there any real life examples of this Label/Label - expression? I’d like to change the label depending on the item state (if its greater than 0, Consuming, if its less than 0, feeding in to the grid)

Documentation is hard to find. The expressions in widgets are parsed using jsep but should pretty much follow javascript expressions. If you are having trouble with a particular expression, you can use the developer sidebar’s expression tester to work through the issues.
image

Use the same expression as your highlight color, but replace your "green" and "red" strings with the label text that you want (check out the example in the image above with arbitrary text in the ternary results).

1 Like

Works great for the footer status and also changing to green when its a positive value, but it fails changing to red when the ItemValue goes under 0

Am I missing something?

component: oh-label-cell
config:
  on: =Number.parseFloat(items.Energy_FromGrid.state) >= 0
  color: "=Number.parseFloat(items.Energy_FromGrid.state) <= 0? 'red' : 'green'"
  header: Energy Status
  footer: "=Number.parseFloat(items.Energy_FromGrid.state) <= 0? 'From Grid' : 'To
    Grid'"
  item: Energy_FromGrid
slots: null

There is something about your items.Energy_FromGrid.state that is not what you are expecting.

The expression will always evaluate to false (meaning you get green and To Grid results) if the value inside parseFloat cannot be parsed to a number. The parser starts at the beginning of the string and collects everything that is a number or numerical symbol and stops when it gets to a space or non-numerical character.

Using the developer sidebar, you can see that the expression works as expected if you replace the items call with a string that looks like the state of an energy type item and can be parsed:
image

On the other hand, if the value inside the parseFloat is null or nonsense, you will always get the false results:
imageimage

You can use the api explorer to see what the state of the item looks like to the widget editor or just temporarily put =items.Energy_FromGrid.state into one of the text display configs such as Header to see what you are actually working with.

2 Likes

Ah! I’m thinking perhaps its got UoM and thats why it’s failing? I added that item into the title and I see this when its positive

And this when its negative

Not sure, but that looks OK to me? Removing the UoM from the item definition doesnt change anything though

Developer side bar showing me this when positive and when its negative, it just adds in the -. All looks OK

What do you see in the developer side bar for

=items.Energy_FromGrid.state

Example:
image

You should just have a number.

And when I set the value to -5, I see -5 kW

And if you put this into the developer thing:

=Number.parseFloat(items.Energy_FromGrid.state) <= 0? 'red' : 'green'

What do you get?

Example:
image

image

Green

What about:

=Number.parseFloat(items.Energy_FromGrid.state) >= 0? 'red' : 'green'

Red

Then it should work.

Its showing red though when its sending to the grid, so no :rofl: and when I set it to negative -5, From the grid, its showing no color

Must be a bug?

component: oh-label-cell
config:
  on: =Number.parseFloat(items.Energy_FromGrid.state) >= 0
  color: "=Number.parseFloat(items.Energy_FromGrid.state) >= 0? 'red' : 'green'"
  header: Energy Status
  footer: "=Number.parseFloat(items.Energy_FromGrid.state) <= 0? 'From Grid' : 'To
    Grid'"
  item: Energy_FromGrid
  title: =items.Energy_FromGrid.state
slots: null

component: oh-label-cell
config:
  on: =Number.parseFloat(items.Power_Watts.state) >= 0
  color: "=Number.parseFloat(items.Power_Watts.state) >= 0? 'red' : 'green'"
  header: Energy Status
  footer: "=Number.parseFloat(items.Power_Watts.state) <= 0? 'From Grid' : 'To Grid'"
  item: Energy_FromGrid
  title: =items.Power_Watts.state
slots: null

Gives me this:
image

component: oh-label-cell
config:
  on: =Number.parseFloat(items.Power_Watts.state) >= 0
  color: "=Number.parseFloat(items.Power_Watts.state) <= 0? 'red' : 'green'"
  header: Energy Status
  footer: "=Number.parseFloat(items.Power_Watts.state) <= 0? 'From Grid' : 'To Grid'"
  item: Energy_FromGrid
  title: =items.Power_Watts.state
slots: null

Gives me this:
image

The only difference is I changed the > on the color: line of code from > to < just to test it .