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.
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).
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:
On the other hand, if the value inside the parseFloat is null or nonsense, you will always get the false results:
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.
I’m confused, set your Power_Watts though to -500 and it should go red.
Is there a way to check two seperate items in the one color statement? I can populate and Item Energy_ToGrid (when its negative) and populate another item Energy_FromGrid (when its positive)
Nope. Because the UoM come after the numerical part of the string, the parser collects the numerals and parses those then just ignores the remaining characters.
OK, that’s good. I agree the state output looks like it is correct.
It is showing you no color in this instance because you also have the on property set to an expression. The on property when true sets the card to whatever color you have used in the color property. But when on is false then the card will always just be whatever the default color is based on the app theme settings (dark/light/auto). So if you only ever want the card to be red or green then you don’t need an expression in on, you just want it to be:
on: true
It looks like the question has become, “What is the output of Number.parseFloat when that energy item’s state is negative?” Using the developer sidebar, the state looks correct but the full expression fails. That leaves this piece in between. When you just put
=Number.parseFloat(items.Energy_FromGrid.state)
into the expression tester what do you get for positive and negative values of the state?
Sorry I missed this earlier, but when you said that the card fails to turn color when the state was less than 0 I thought you meant it stayed the same color, so I was looking in the wrong place. The entire problem is with your on property. The code you posted:
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
Is working exactly as it should. Let’s work through it:
When items.Energy_FromGrid.state is positive:
on evaluates to true
color evaluates to "green"
Final result: the card will show a background color and that color will be green
When items.Energy_FromGrid.state is negative:
on evaluates to false
color evaluates to "red"
Final result: the card will not be highlighted with a background color so it doesn’t matter what the color property is set to; it’s just ignored.
Nowhere. If you always want the card to be highlighted with a background color then you don’t want an expression that is going to change, you just want on to be always true. You only want an expression in on if there is some situation where you do not want the card to render with a different background color. Then you want on to have an expression that can be either true or false. As I understand it your card should only ever be green or red, so there’s no reason for on to ever change; just leave it at true.
The final code:
component: oh-label-cell
config:
on: true
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