OH3 Widget vars default value

The same solution works as part of any expression, so:

groupItem: =(vars.selectedPartition || "Partition1") + '_Bypass_Group'

works just fine. But it’s true that if you have a lot of variable use in your widget it can get tedious and difficult to track.

It never hurts to ask! There quite a few widgets that could be greatly simplified if this were possible. On the other hand, I don’t know if there’s some technical reason why this hasn’t been done up to this point.

There is one other option possible, but it’s a workaround that really only works if you plan on having only one of these widgets on a single page. I said initially that variables in widgets couldn’t have default values, but variables can also be defined on pages so that widgets have access to variables with a more “global” scope and can communicate with each other. These page level variables can have default values set. So if you’re only going to have one widget using the variable, then you can just also define the variable at the page level and give it a default value there.

I believe the same general properties definitions that work for rule templates also work for widgets. In rule templates, you can define a default value using defaultValue: Partition1. I’d be surprised if that doesn’t work but I’m not in a position to test that out at the moment. I’ll try to remember to come back when I get a chance.

I’ve never made this work within widgets:

uid: demo:default_vars
props:
  parameterGroups: []
  parameters:
    - name: prop1
      label: Prop 1
      type: TEXT
      description: A text prop
      defaultValue: This is a default value
tags: []
component: f7-card
config:
  title: =(props.prop1 || 'No Default Value Set')

image

:frowning: Oh well. I wonder if it could be added. It’s super handy in rule templates.

If I rember correct, it was changed from defaultValue to default.

1 Like

Just for widgets? I think it’s still defaultValue for rule templates. I’m pretty sure I tested that out with my latest rule template I posted a few days ago. Maybe I didn’t.

Right! I forgot about that. That’s true. However it still only applies to properties. There’s still no way that I’ve seen to define variables in the widget config.

As soon as I save the widget mine gets saved as:

uid: demo:default_vars
tags: []
props:
  parameters:
    - default: This is a default value
      description: A text prop
      label: Prop 1
      name: prop1
      required: false
      type: TEXT
  parameterGroups: []
timestamp: Aug 22, 2022, 5:38:10 PM
component: f7-card
config:
  title: =(props.prop1 || 'No Default Value Set')

So changes the defaultValue to default and the output is correct:

But… does not work for vars.

Thanks Justin I will try that.

I will try and log a GitHUB issue to see if it can be extended to vars as well. Hopefully it will get some traction as that would be WAY beyond my abilities.

I only saw it for widgets.

Logged as [webui]Extend props. default value to vars. · Issue #1471 · openhab/openhab-webui · GitHub

1 Like

There is a way if you add this code to the pages code, no way inside the widget code so it has to be the page you insert the widget into.

config:
  defineVars:
    MyVarNameHere: 0

Thanks Matt.

Gave it a try with:

config:
  label: Overview-TEST
  layoutType: responsive
  sidebar: true
  defineVars:
    selectedPartition: Partition2

and

component: oh-cell
config:
  defineVars:
    selectedPartition: Partition2

On my Overview TEST page.
But the variable does not seem to propagate.

Matt’s suggestion is the same thing I mentioned as a possible workaround up above. You don’t define the variable in the widget (widget’s just don’t have the defineVars config property which is the whole crux of this problem). You just define the variable at the top of the page and then reference the variable as you normally would in the widget with the vars object. Because the variable has been defined on the page, the widget references this “global” value instead of using a value local to the widget.

Your second example way you tried is not correct as you are trying to modify the configs of the Oh-cell and not the global pages configs. The first example I do not know why it does not work when I can only see a small snippet of the code. I am using this as a work around and it works for me despite having used the same variable in multiple widgets of the same inserted on the one page.

This is not documented and I found it by searching the forum posts here:

Using vars in custom widgets: when are vars updated? - Add-ons / UIs - openHAB Community

Thanks Justin and Matt. After your messages above I thought I knew why - was overwriting in the widget. But i still cant make the suggestion work.

I am probably doing something really dumb…

Have now set up a test as follows:

Page:

config:
  label: VARS Test
  layoutType: responsive
  sidebar: true
  defineVars:
    selectedPartition: Partition2
blocks:
  - component: oh-block
    config: {}
    slots:
      default:
        - component: oh-grid-cells
          config: {}
          slots:
            default:
              - component: oh-cell
                config:
                  action: popup
                  actionModal: widget:vars_test
masonry: []
grid: null
canvas: null

Widget:

uid: vars_test
tags: []
props:
  parameterGroups: []
timestamp: Aug 24, 2022, 7:35:14 AM
component: f7-page
config: {}
slots:
  default:
    - component: f7-list
      config: {}
      slots:
        default:
          - component: Label
            config:
              text: =(vars.selectedPartition || "Partition1")

But still get:
image

Thanks Guys for any advise

So I have been trying to work with this work around.

It appears however that the variable is only propagated one level sadly.

So if I reference the variable in an oh-cell on a page, the variable is available. However if I try and reference the variable from a widget inside the oh-cell, the variable is no longer available.

So I cannot apply the work around in my use case as my widget needs to be called from an oh-cell otherwise the formatting is all lost and it just does not work anymore.

My results:

Page code:

config:
  label: VARS Test
  layoutType: responsive
  sidebar: true
  defineVars:
    selectedPartition: Partition4
blocks:
  - component: oh-block
    config: {}
    slots:
      default:
        - component: oh-grid-cells
          config: {}
          slots:
            default:
              - component: widget:vars_test
                config: {}
              - component: oh-cell
                config:
                  action: popup
                  actionModal: widget:vars_test
                  title: "selectedPartition: Partition4"
                  header: "defineVars:"
                  footer: =vars.selectedPartition
masonry: []
grid: null
canvas: null

Widget Code:

uid: vars_test
tags: []
props:
  parameterGroups: []
timestamp: Aug 24, 2022, 1:46:34 PM
component: f7-page
config: {}
slots:
  default:
    - component: f7-list
      config: {}
      slots:
        default:
          - component: Label
            config:
              text: =(vars.selectedPartition || "Partition1")
          - component: oh-button
            config:
              text: = "Partition1"
              fill: true
              textColor: white
              raised: false
              action: variable
              actionVariable: selectedPartition
              actionVariableValue: ="Partition1"
          - component: oh-button
            config:
              text: = "Partition2"
              fill: true
              textColor: white
              raised: false
              action: variable
              actionVariable: selectedPartition
              actionVariableValue: ="Partition2"
          - component: oh-button
            config:
              text: = "Partition3"
              fill: true
              textColor: white
              raised: false
              action: variable
              actionVariable: selectedPartition
              actionVariableValue: ="Partition3"
          - component: oh-button
            config:
              text: = "Partition4"
              fill: true
              textColor: white
              raised: false
              action: variable
              actionVariable: selectedPartition
              actionVariableValue: ="Partition4"

That has not been my experience, although I may be doing the multiple levels differently to how you are, but mine are working in the 3rd level as per your picture. The widget I use and works, is in the marketplace called camera mp4 history if you wish to look and play with its code. I define the variable in the widget and only use defaultValue to set the value, not create the variables. I use multiple widgets all on the same page and they do not share a GLOBAL variable, but unique separate variables that all work as intended in each widget. The defaultValue appears to just set all variables in the page with the given name. If you are not defining the variable in the widget, then this may be the reason it works for me and not for you.

I am not an expert and don’t know if this is the wrong way, but it works for me and hope it can help you. Hopefully someone with some time to donate can: 1. document this. 2. Request a way to do it at the widget level with a github issue as clearly multiple people are wanting this.

Hi Matt

Thanks for the feedback.

I have taken a look at your code. But I cannot see where you are calling the wiget from a page and assigning the variable? Am I missing something?

I have submitted a gitHub request. Would be nice to get a plus 1 from you (and others) if you have the time.

I am also happy to try and document the workaround if I can get it to work. I am clearly missing some thing though.

Hi Matt
Please could you explain what you mean here? How do you define that variable in the widget?