Closed marcinczeczko closed 4 months ago
Are you sure your Item state doesn't have a unit incorporated? (e.g. % for battery)
items.<itemName>.state
and items['<itemName>'].state
should be the same, at least the syntax is supposed to be equivalent.
Those always return strings, no matter what the actual item type is, because that's how the REST API behaves.
Try something like:
icon: '=(Number.parseInt(items.Test.state > 50)) ? "if:mdi:battery-100" : "if:mdi:battery-alert"'
Comment: Number type Items can hold both integer and float states. So a string version of the state (that you are usually dealing with in widgets) will be "5" or "5.0". Those are mathematically equal of course, but "5" does not equal "5.0" - two different strings. You need to be careful to parse the string into a number if you want to do maths comparisons on it.
In case you need a number, please use the new .numericState property. (It will be available with openHAB 4.2.0 in two weeks.)
Closing now as this should solve your problem.
The problem
I was about to configure Default Widget for the Number item representing battery level of the sensor.
I wanted to dynamically assign icon depending on the item state. So, if used following expression that gets the item via
items[props.item]
in the "Icon" field I'm always getting the "else" condition value:I investigated a little bit and found out that, if I use expression with the Item name directly, then apparently the state value is integer and condition is evaluated as expected
Expected behavior
items[props.item].state
should behave same way asitems.<ItemName>.state
and return same type (integer for Number items)Steps to reproduce
Number Test
openhab:send Test <Value>
Your environment
Browser console
Browser network traffic
Additional information