[Custom Widget] Button react to bit of item

Hello everyone,
is it possible, to use a number-item which is bit-coded, to change the fill-property of an oh-button?

In my case, i want to use bit 2 of the value from the item to fill my button or not.

In a rule of mine, this code is already working:
val btand = (wBedRoom_Day_Num.state as Number).intValue.bitwiseAnd(bitdec.intValue)

And here is my guess for the fill-property of one button:

                            - component: oh-button
                              config:
                                text: MO
                                outline: true
                                fill: "=(0 < (items[wBedRoom_Day_Num].state as Number).intValue.bitwiseAnd(2)) ? true : false"
                                action: command
                                actionItem: =props.day
                                actionCommand: 1
                                style:
                                  width: 14%
                                  position: relative

Remember, MainUI Widgets and Rules are written in completely different languages (usually) and operating in completely different environments. MainUI Widgets are JavaScript running in your browser. Rules may be JavaScript, among other language choices, but they are running on your OH server. Furthermore, most of the stuff the Rules interact with are Java Objects and Java doesn’t even exist on the UI.

So there should be no expectation that you can take a line of code from a rule and have it work in the UI unfortunately.

JavaScript doesn’t have an as operation. Number in your rule is referring to the Java Number which doesn’t even exist in the UI. JavaScript does have a Number class but it’s used to parse stuff and doesn’t hold a value itself so it doesn’t have an intValue. Unlike in Rules DSL, when calling a function that doesn’t have arguments, the ( ) is required so if intValue did exists, you’d have to call it using .intValue().

What you do have in the UI widget:

  • items[wBedRoom_Day_Num].state is a string representation of the state of the Item, it’s not a number and it might have extra stuff like units of measurement
  • JavaScript does have bitwise operators: https://www.w3schools.com/js/js_bitwise.asp. However, widget expressions do not support the whole of JavaScript. It mentions it supports all the mathematical operations so I would guess they are supported. See Building Pages - Components & Widgets | openHAB

So, if you parse the string to an integer and then use & that might work.

=(0 < Number.parseInt(items["wBedRoom_Day_Num"].state) & 2)

The ? true : false part is redundant. The comparison will return true or false.

2 Likes

As always you helped me a lot!!
I had a few months off and not doing anything in openHAB but i finally got it working thanks to your help!

                        - component: oh-button
                          config:
                            text: MO
                            outline: true
                            fill: =( 0 < (Number.parseInt(items["wBedRoom_Day_Num"].state) & 2 ) )
                            action: command
                            actionItem: =props.day
                            actionCommand: 2
                            style:
                              width: 14%
                              position: relative

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.