openHAB 3 UI - widget logical condition defined from string

Hello,
i created a widget which makes based on an item state badges visible or not.

Currently this is working:
State for which the badge shall be shown can be defined via
badgecond1 = “ON”

    - description: "example: Fernseher"
      label: Badge Text 1
      name: badgetxt1
      required: false
      type: TEXT
    - context: item
      description: "example: Switch-item, TV state"
      label: Badge Item 1
      name: badgeitem1
      required: false
      type: TEXT
    - description: "example: ON"
      label: Badge Condition 1
      name: badgecond1
      required: false
      type: TEXT
    - description: "example: green"
      label: Badge Color 1
      name: badgecol1
      required: false
      type: TEXT
...
    - component: f7-badge
      config:
        visible: =items[props.badgeitem1].state === props.badgecond1
        textColor: black
        color: =props.badgecol1
      slots:
        default:
          - component: Label
            config:
              text: =props.badgetxt1

What i would like to do:
is to define also the logical condition into badgecond1 for examplme like this:
badgecond1 = “!== 50”

this means, that the badge visibility would need a condition like this:
(which is not working because i dont know how to do this)

    - component: f7-badge
      config:
        visible: ='=(items[props.badgeitem1].state ' + props.badgecond1 +')'
        textColor: black
        color: =props.badgecol1
      slots:
        default:
          - component: Label
            config:
              text: =props.badgetxt1

This way i would be able to have a very flexible widget, covering all possible logical operations for visibility

Thanks in advance

This is not going to work, please try

=(!badgecond1==50)

What should be the result here ??? Does not make sense IMHO.

Maybe i forgot to say, that badgecond1 is defined via the Properties User-Interface.
This should only show, that the user would put this string as input to the field:
“!== 50”

Here i tried to show what i want to achieve.

For example i want to generate this command line
(which is a working command)

  • visible: =items[props.badgeitem1].state !== 50

And the user has set the last part of the line via property, typed in as string

  • "!== 50"

So if the user sets the input string next time to for example

  • ">== 50"

automatically the command line

  • visible: =items[props.badgeitem1].state >== 50

shall be generated

The user shall have the chance to not only define the value, but also the logical operator.

Ok, now it is clearer.

Please try the following then :

visible: '=items[props.badgeitem1].state + props.badgecond1'

Thanks for this suggestion but i fear this does not work.
It seems that visibility is always true if im using your proposed command.

I tried

visible: '=items[props.badgeitem1].state + props.badgecond1'
visible: "=items[props.badgeitem1].state + props.badgecond1"

for the badgecond1, configured via UI i tried

=== "ON"
=== 'ON'
=== ON

And im pretty sure, that items[props.badgeitem1].state = “OFF”
But my badge is always visible, what means that the visible-condition returns “true” somehow?

I don’t think you can accomplish this goal with a simple string the way you are attempting. The YAML parser doesn’t run an eval() on any string variables. So yes, your visible parameter always evaluates to true because it equals the string "!=50".

It is possible to achieve this, but you would have to build a much more complex system that includes separate operator and value properties. Then the visible parameter could select the appropriate comparison and apply it to the value. It would get pretty long depending on how complex you needed it to be but would look something like:

visible: '=(props.operator == "=") ? (items[props.badgeitem1].state == props.conditionValue) : ((props.operator == "!=") ? (items[props.badgeitem1].state != props.conditionValue) : true)'

Hello JustinG,
i understood!

No problem for me → Would just give additional oppotunities but the stuff i already have is basically OK :wink: