Default, global oh-context

Is there any possibility, hack, idea how make some default oh-context for entire OH site ? So we could get some global functions, constants and variables that we can reuse in custom widgets. What do You think oof this?

I can’t really think of any way of achieving this with what’s currently available.

It might be worth an issue on the UI repository to start the discussion, but my guess would be it would get some pushback from the maintainers for two reasons. First, one of the primary concerns from the very beginning of OH3 has been to optimize the loading of pages and widgets by carefully limiting amount of data passed between the server and browser and the automatic injection of potentially unused variables etc. would go against that philosophy. Second, as part of their task to ensure the stability of a UI that already has a lot of places users can modify and add to it, the maintainers are, justifiably, cautious about any proposal that includes a possibility of system-wide breakage.

That said, I don’t think it’s a complete non-starter, and a good discussion about it may go somewhere interesting.

I made a custom widget, containing only oh-context. But when starting any new widget with this widget as parent of anything else, caused a lot of problems, the wired one was, all parameters were empty after widget:contextWidget, I could not figure way around this 


This doesn’t surprise me. As far as I know, it is not supported to have child components to a custom widget called by widget:WidgetName.

At the moment I can’t think of anyway to do what you’re trying to do.

Well, it works! First, create your context widget which contains your variables and functions:

uid: widget_context2
tags:
  - demo
props:
  parameters:
    - default: This is a text from widget_context2
      label: Some label
      name: someVar
      required: false
      type: TEXT
  parameterGroups: []
timestamp: Jan 10, 2026, 2:33:32 AM
component: oh-context
config:
  functions:
    duplicate: =(a) => (a+a)
  variables:
    testvar: This variable is in widget_context2
slots: {}

Then, call this widget from another widget. Here you need to ensure that you “pass on” the original props to a new prop, so you can access them from the derived widget:

uid: widget_context_test2
tags:
  - development
props:
  parameters:
    - default: This is a text from widget_context_test2
      label: Test
      name: test
      required: false
      type: TEXT
timestamp: Jan 10, 2026, 2:36:38 AM
component: widget:widget_context2
config:
  context: =props
  anyOther: New prop from widget_context_test2
slots:
  default:
    - component: oh-label-card
      config:
        title: =props.context.test+fn.duplicate(' !')
        footer: =props.anyOther+' // '+vars.testvar
        label: ='PROPS='+JSON.stringify(props)+', VARS='+JSON.stringify(vars)

your result is something like this:

A few comments:

  1. the name ‘context’ was just chosen by me, you could also name this any other way, say banana. In such case, you would access the props by changing title to =props.banana.test 
.

  2. anyOther is treated like a new prop of the widget_context_test2 widget (just like context). For both, the value can be specified just like it was a variable.

  3. In the result you can see the JSON objects props and vars (both predefined by the system. In vars, you can see the variable that was initiated in the widget_context2 (original), so you could access it in here as well.

  4. You can access functions in widget_context_test2 that are defined in widget_context2 which I demonstrated with duplicate().

1 Like

Fascinating! That’s a really nice demo. Out of curiosity, what version of OH are you currently using?

I am working now on 5.1 (Docker image) but this worked for some versions before already.