OH3 Widget vars default value

Hi All

I use props: parameters in a widget to pre-populate some of the expressions

I have a props set as follows which I use as vars.selectedPartition.:

props:
  parameters:
    - default: Partition1
      description: Default (Fallback) Partition
      label: Default (Fallback) Partition
      name: selectedPartition
      required: false
      type: TEXT

The default value however does not seem to be available via vars., but only under props.

So the following works once the initial value is set (via a button selection, not via the default):

    - component: f7-list
      config:
        style:
          position: absolute
          left: 10
          top: 55%
          width: 50%
      slots:
        default:
          - component: Label
            config:
              text: =vars.selectedPartition

Is there a way to set the default value for a variable?

Thanks
Mark

EDIT: Default values for widget property parameters by ghys · Pull Request #1049 · openhab/openhab-webui · GitHub

Not sure if the above means that this is only available for props. (and it looks like defaultValue has been changed to default (on save it changes to default)

To my knowledge you cannot currently use default values for vars in widgets. The simplest solution is just to use a basic OR syntax in your expressions: (vars.someVariable || default value). When the variable is undefined at the street of the widget use the OR will return the default value and once the variable has been set by the widget it will return the variable value.

1 Like

Thanks Justin, that works for the following:

    - component: f7-list
      config:
        style:
          position: absolute
          left: 10
          top: 55%
      slots:
        default:
          - component: Label
            config:
              text: =(vars.selectedPartition || "Partition1")

But I am trying to get rid of these annoying messages, so might be a bit more complex:

15:24:36.979 [WARN ] [se.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: undefined
15:24:36.979 [WARN ] [se.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: undefined_Armed
15:24:36.979 [WARN ] [se.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: undefined_ReadyToArm
15:24:36.979 [WARN ] [se.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: undefined_ErrorBeepTripleBeep
15:24:36.979 [WARN ] [se.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: undefined_EntryGuard
15:24:36.979 [WARN ] [se.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: undefined_ChimeModeOn
15:24:36.979 [WARN ] [se.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: undefined_Exit1
15:24:36.979 [WARN ] [se.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: undefined_Bypass_Group
15:24:36.979 [WARN ] [se.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: undefined_Cancel
15:24:36.979 [WARN ] [se.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: Partition5_Armed
15:24:36.979 [WARN ] [se.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: Partition6_Armed
15:24:36.979 [WARN ] [se.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: Partition7_Armed
15:24:36.979 [WARN ] [se.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: Partition8_Armed
15:24:36.979 [WARN ] [se.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: Partition2_Cancel
15:24:36.979 [WARN ] [se.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: Partition1_Cancel
15:24:36.979 [WARN ] [se.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: Partition4_Cancel

My use case is:

    - component: f7-list
      config:
        visible: =(vars.Bypass) == true
        style:
          position: absolute
          left: 20%
          top: 16%
          width: 60%
          --f7-list-border-color: transparent
          opacity: 80%
        class:
          - padding-bottom
          - big-toggles
      slots:
        default:
          - component: Label
            config:
              text: Zones Bypassed
              style:
                padding-left: 20px
                font-weight: 500
                line-height: 20px
                backgroundColor: rgb(191, 189, 189)
                font-size: 75%
          - component: oh-repeater
            config:
              fragment: true
              for: item
              sourceType: itemsInGroup
              groupItem: =(vars.selectedPartition) + '_Bypass_Group'
            slots:
              default:
                - component: oh-toggle-item
                  config:
                    title: =loop.item.label
                    item: =loop.item.name
                    color: '=(items[loop.item.name].state == "ON") ? "green" : "gray"'
                    style:
                      backgroundColor: rgb(235, 232, 232)
                      font-size: 70%
                      --f7-toggle-width: 35px
                      --f7-toggle-height: 10px
                      --f7-list-item-min-height: 14px
                      --f7-list-item-padding-vertical: 1px
                      --f7-list-item-border-color: transparent

And my widget has a whole lot of similar expressions that rely on the selectPartition being set before starting.

Maybe worth logging a request to see if Default values for widget property parameters by ghys · Pull Request #1049 · openhab/openhab-webui · GitHub could be expended to variables?

Unless you has another genius idea?
Thanks

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"