OH3 Widgets - initialize variables

Hello,
I am quiet new to openhab and currently try to create a custom widget which uses variables.

Is it possible to initialize custom variables?

In the code below I would like the variables to be initialized as follows: vars.weekDay = “monday” and vars.command = “0”. How can I achive this?

uid: TimerList_PopUp_Add
tags: []
props:
  parameters:
    - context: item
      description: Item that can be used to add a Timer
      label: Add Timer
      name: add_Timer
      required: true
      type: TEXT
    - context: item
      description: String item which holds all configured timers
      label: Timer List
      name: timerListItem
      required: true
      type: TEXT
    - context: item
      description: Item that should get controlled by timer
      label: Item controlled by Timer
      name: controlledItem
      required: true
      type: TEXT
  parameterGroups: []
timestamp: Apr 10, 2021, 4:39:49 PM
component: f7-card-content
config:
  padding: false
  class:
    - margin-vertical
    - display-flex
    - flex-direction-column
    - flex-justify-content-flex-start
    - flex-align-items-center
slots:
  default:
    - component: oh-list
      config:
        id: weekday
      slots:
        default:
          - component: oh-list-item
            config:
              id: weekday
              radio: true
              title: Montag
              action: variable
              actionVariable: weekDay
              actionVariableValue: monday
              defaultChecked: true
              checked: = vars.weekDay == "monday"
          - component: oh-list-item
            config:
              id: weekday
              radio: true
              title: Dienstag
              action: variable
              actionVariable: weekDay
              actionVariableValue: thuesday
              checked: = vars.weekDay == "thuesday"
          - component: oh-list-item
            config:
              radio: true
              title: Mittwoch
              action: variable
              actionVariable: weekDay
              actionVariableValue: wednesday
              checked: = vars.weekDay == "wednesday"
          - component: oh-list-item
            config:
              radio: true
              title: Donnerstag
              action: variable
              actionVariable: weekDay
              actionVariableValue: thursday
              checked: = vars.weekDay == "thursday"
          - component: oh-list-item
            config:
              radio: true
              title: Freitag
              action: variable
              actionVariable: weekDay
              actionVariableValue: friday
              checked: = vars.weekDay == "friday"
          - component: oh-list-item
            config:
              radio: true
              title: Samstag
              action: variable
              actionVariable: weekDay
              actionVariableValue: saturday
              checked: = vars.weekDay == "saturday"
          - component: oh-list-item
            config:
              radio: true
              title: Sonntag
              action: variable
              actionVariable: weekDay
              actionVariableValue: sunday
              checked: = vars.weekDay == "sunday"
          - component: f7-col
            slots:
              default:
                - component: f7-row
                  slots:
                    default:
                      - component: Label
                        config:
                          text: Stunde
                      - component: oh-stepper
                        config:
                          large: true
                          variable: hour
                          id: hour
                          raised: true
                          init: true
                          value: 0
                          name: minute
                          autorepeat: true
                          autorepeatDynamic: true
                          min: 0
                          max: 23
                          manualInputMode: true
                      - component: f7-row
                        slots:
                          default:
                            - component: oh-stepper
                              config:
                                large: true
                                variable: minute
                                id: minute
                                raised: true
                                autorepeatDynamic: true
                                init: true
                                value: 0
                                name: minute
                                autorepeat: true
                                min: 0
                                max: 59
                                manualInputMode: true
                - component: oh-slider
                  config:
                    min: 0
                    max: 100
                    label: true
                    scale: true
                    variable: command
                    unit: "%"
                    default: =50
    - component: oh-label-item
      config:
        title: "Timer:"
        after: = props.timerListItem + "@+" + props.controlledItem + "," + vars.command + "," + vars.weekDay + "," + vars.hour + ":" + vars.minute
    - component: oh-button
      config:
        popupClose: true
        round: true
        fill: true
        large: true
        text: ="Speichern"
        action: command
        actionItem: = props.add_Timer
        actionCommand: = props.timerListItem + "@+" + props.controlledItem + "," + vars.command + "," + vars.weekDay + "," + vars.hour + ":" + vars.minute
        class:
          - margin-horizontal

regards
Jochen

According to the docs the properties follow the same format and have the same options as Configuration Descriptions used by the bindings. Looking at the table shows

default: The default value of the configuration parameter (optional). If multiple is true you can define a list of default values, separating them by a

Hello Rich,
thank you for your reply and your great work on openHab.

Unfortunatly I am unable to figure out how to use the information you gave me.
At the moment in my custom widget the variables occure only in the context of and . So It is uncleare to me where the default value could be added.

I have tried the following things without success:

  1. adding a section “vars” like the section props to the code
vars:
  parameters:
    - name: weekDay
      type: ="text"
      default: ="thursday"

It gets removed automatically.

  1. adding "default to one of the places where the variable gets used
  2. add a Propierty of type string to the props section

Can variables be declared as a parameter in the props section of the widget configuration?

Can you please give me a more elaborated advice?

thank you

best
Jochen

All I really know on this topic is what’s in the docs. Beyond that I think there was an issue recently opened on this topic.

It appears that defaultValue for widget parameters might be supported soon:

I’ve been trying to get some variables happening too and this question is the same one I came up with.

I haven’t got a complete answer, well not one that solves my problem, but I found one way how to initialise a variable.

 - component: oh-knob
     config:
       variable: thisVariable
       min: 0
       max: 100
       stepSize: 1
       disabled: false

If you throw it into a defintition like this, you’ll find vars.thisVariable is available within the scope of the widget.

While I could reference the variable elsewhere in the widget, I wasn’t able to get close to what I need to do.

It was too late at night and I was lost in so many levels of convolution that I can’t make much more sense of it, but at this point this method will get you into the game.

–X–X–X–X–X–X–X–X

I wanted to pass a number representing total seconds to a popover widget, and split that number into hours, minutes and seconds and manipulate them with knobs to ultimately return a result of total seconds. I’ve got the split happening on a popover widget and it sets the three knobs, but handing it off to variables for recombination to be returned is still evading me.

Does anyone have an idea on how to do this?

1 Like