I’m trying to create an expression in YAML to create a dynamic label for a gauge in the overview page of main UI. My problem is that I need to compare something with a Unit of Measurement (Watts) on it. The typecasting you can find in the forums does not work here. The version below does something, but is behaving unexpected. For example:
1 Watt -> Done
2 Watt -> Done
5 Watt -> Not done
50 Watt -> Not Done
150 Watt -> Done <-- ?!
=items.Waschmaschine_Watt.state < “3 W”?“Done”:“Not done”
I’ve not found any guidance to this in the docs. Any idea?
I think I’m having the same problem.
Trying to set colors based on Illuminance values which are in Lux or lx
Both Illuminance and Illuminance Threshold items are Number:Illuminance types.
'=(items.Illuminance.state > items.IlluminanceThreshold.state) ? “red”: “green” ’
'=(items.Illuminance.state | lx > items.IlluminanceThreshold.state | lx) ? “red”: “green” ’
'=(items.Illuminance.state | " lx" > items.IlluminanceThreshold.state | " lx") ? “red”: “green” ’
none of these work.
Did you ever find a solution?
I don’t think there is any UoM support here.
You must process the states yourself. Split the value and units, either trust the unit is what you hoped for, or process that and scale the value.
So how do I do this?
I can’t seem to compare a Number:Illuminance value with a number or another Number:Illuminance
This doesn’t work
color: '=(items.Kitchen_Endboard_MotionLightSensor_Illuminance.state < 10) ? "red" : "blue"'
gives you the state with units
You have to hope it is in the units that you expected.
you can examine the unit, and if not the one you wanted, do your own conversion maths.
There is no equivalent in the UI to sort of features you get in system rules like
(MyItem.state as QuantityType).toUnit("kW")
ok. Thanks. This worked.
But why is Illuminance the only one I have needed to treat like this? There is no problem with temperatures for example.
I have no idea how one widget of yours compares to another. Most likely you are not trying to do nonsense maths in other cases.
if ( 52 buckets < 10 )
is not possible to process. 10 what, lorry loads or teaspoonful?
No. It’s exactly the same.
This works with a Number:Temperature item
'=(items.Temperature.state > "20") ? "red" : "blue" '
This does not work with a Number:Illuminance item
'=(items.Illuminance.state > "20") ? "red" : "blue" '
And, if (Illuminance < IlluminanceThreshold) knows that it’s lorry loads (lx) because both items are Number:Illuminance items
Check that very closely. What does ‘not work’ mean?
There’s a longer discussion here
I came here because I didn’t want to mess up the other thread. My problem however is quite similar.
Formerly I had defined the color with:
color: '=(items.AqaraMQ_THPS2_TEMP.state < 0.0) ? "white" : null'
which worked as inteded.
Now I have added the Item to the semantic model making it a Number:Temperature Item with the semantic property Measurement->Temperature.
Suddenly, the state gives me the Temperature including units, which is also shown in the widget snippet test in the …
But it doesn’t, the code has no idea at all about quantities and units. These are just strings. “0.00001 lx” > “5 lx” is true, because it has more characters.
Actually, the first suggestion of using split hasn’t worked. I think there’s a bug in here. It’s acting very strange. A simple expression of
if A < B then color green. As I incrrease and decrese the A value, I get green randomly.
You intend to do a numerical comparison, yes? Remember that Item states arrive here as strings, not numbers. Splitting them results in strings of digits, not real numbers. Parse the strings to get numbers.
“19 °C” > “20 °C” False, hurrah!
“21 °C > 20 °C” True, hurrah!
“19.5 °C” > “20 °C” True, errk
nearly works with formatted strings.
I have a fully functional home heating system UI built with loads of conditional colors and other things like icon sizes etc all reacting to Number:Temperature items with no problems.
I can not get even simple maths or comparisons to happen with a Number:Illuminance item
Please go read the other thread. This is key
The widespread technique to deal with these units is to strip everything after the first space before parsing into a float:
I plan to add some utils functions in the expression scope to deal with this and other cases.
My heating system works just fine with numbers that are a mixture (and changing) between whole numbers, .0, and .00 numbers.
I’m not trying to be difficult. I only say what I see
Well, there’s the fix, as recommended by the guy who wrote the UI. What will you do now?
That works, thank you.
But it still doesn’t explain why I need to do this with Illuminance numbers and not temperature numbers. It reads like it shouldn’t work with temperatures either, but like I said, my heating system UI changes through blue, yellow, orange, pink, red as the temp rises and it works perfectly.
I seem to be able to get numbers from the Developer Sidebar by losing the split bit, but it doesn’t work in my UI expressions
Why, what are you doing with it? What does ‘not work’ mean happens instead? Tip, now it is parsed to a number don’t try comparing to some other string like “20”.