OH3 oh-button toggle or set/unset variable using action: variable

Hi All

Another request for help…

I am using an oh-button to set a variable which is used in a widget to determine visibility of a component. However I need to9 be able to “toggle” the variable TRUE/FALSE or TRUE/unset etc.

I have managed to work around this for testing by adding a 2nd “hidden” button to change the variable and this successfully toggle my visibility as I wanted.

My current YAML:

    - component: oh-button
      config:
        tooltip-trigger: hover
        tooltip: '=!(props.tooltipEnable) ? false : "Show Faulted Zones (If any)"'
        text: READY
        textColor: white
        action: variable
        actionVariable: Faulted
        actionVariableValue: TRUE
        style:
          position: absolute
          top: 14.5%
          left: 30%
          color: white
 #??? Hidden button - just shows an X

    - component: oh-button
      config:
        tooltip-trigger: hover
        tooltip: '=!(props.tooltipEnable) ? false : "Show Faulted Zones (If any)"'
        text: X  
        textColor: white
        action: variable
        actionVariable: Faulted
        actionVariableValue: FALSE
        style:
          position: absolute
          top: 14.5%
          left: 25%
          color: white  

My visibility condition is:

visible: =(vars.Faulted) == true

Toggle Visibility

Any ideas of how to achieve this with a SINGLE button? So some method to TOGGLE the variable

EDIT: I am aware I could use a “Proxy Item” to achieve this, though I would prefer to have a “self contained” option if at all possible.

Thanks again.
Mark

I think one of your challenges will be is that when you refresh the page the variables get wiped out. If this variable’s value needs to persist beyond that reload you’ll have to use an Item. Beyond that I can’t help unfortunately. I only respond to keep you from spinning your wheels only to discover you need the Item anyway.

Hi Rich

Thank for the response. I didn’t think of that, but it is actually what I would prefer.

I have done a version with ITEMS, and it works well, however the visibility is obviously set to what the last state was - which is not a train smash, but not what I would prefer.

I could expire the proxy items - however the issue then is after how long etc.

My preferred version would be with the variable - which I also had working with the “hidden” switch. I like the clean slate start up when accessing the widget and did nto have any issues during my testing with the variable being cleared.

So still looking for a plan to toggle the variable somehow.

The actionVariableValue can be an expression and it can reference the variable itself so your answer could be as simple as

        actionVariableValue: =!(vars.Faulted)

The only hiccup here is that the initial value of the variable is null so your expression will have to intelligently handle that and depending on what you want the first press of the button to do you your expression should be able to start off with that value. So in the end you probably want something such as:

        actionVariableValue: '=(vars.Faulted == "null") ? true : !(vars.Faulted)'
2 Likes

Thank you so much… Works like a charm. Going to revert my item based version back to the variable version.

Wouldn’t it be easier to use single/double boolean inversion to achieve the same result?

(!null) == true
(!!null) == false
actionVariableValue: '= !(vars.Faulted)'