[main UI] How to use "Context" when running Script via Action Button

I’ve been searching in the documentation how to use the “Context” property when

  • Configuring a List Item in mainUI
  • As Action running a Script

The description says:

Object representing the optional context to pass. Edit in YAML or provide a JSON object, e.g. { "param1": "value1", "param2": { "subkey1": "testing", "subkey2": 123 } }.

So what would i write down in a *.script to access e.g. the value of param1?

I tried to guess but nothing did work form me

Im wondering if it is possible to use it similar like arguments passed to a function-call so i can execute one and the same script but the script executes dependent to the context

E.g. a script

sendmqttcommand.script

// Retrieve Broker Instance
val mqttActions = getActions("mqtt","mqtt:broker:mybroker")

var command = 0

// Set value dependent to context
switch (context) {
   case "onoff":
    command = 2704
   case "input":
    command = 2704
   case "volup":
    command = 1168
   case "voldown":
    command = 3216
   case "up":
    command = 752
   case "down":
    command = 2800
    case "right":
    command = 3280
   case "left":
    command = 720
   case "enter":
    command = 2672
   case "back":
    command = 25321
    
}

// Send Command
mqttActions.publishMQTT("my/topic","{\"value\":" + command + ",\"bits\":12}", true)

Yes, this is exactly the kind of use case for this.

Under this example param1 should be a variable available in the main scope of the rule.

For example, if you have a widget with the following configurations:

- component: oh-link
  config:
    action: rule
    actionRule: myRuleUid
    actionRuleContext:
      itemName: =loop.item.name
      newValue: ON

Then in rule you can access those two variables directly:

items.getItem(itemName).sendCommand(newValue)

Are you using DSL for your scripts? It looks like you might be. I don’t believe this works with DSL; just the other script languages.

2 Likes

You wouldn’t.

Unfortunately the term “Script” has many meanings in OH depending on the context (see the links below). In this case though, the script being referenced means “any rule that doesn’t have any triggers or conditions and tagged with the ‘script’ tag”. However, even that is a bit misleading because, as the values for the action indicate, any rule can be called from a widget with that action.

What you are trying to use is a Rules DSL Script which is kind of like the appendix in the human anatomy. It might have some obscure uses still today but overall it’s not useful and a vestige of the openHAB of yesteryear. In Rules DSL you can write some Rules DSL code in .script files in the $OH_CONF/scripts which can be called using the callScript Action. You can’t pass arguments to these. You can’t get a result from these. And they cannot be called directly from MainUI as an action. They really are not useful and almost no one uses them (one of the best uses for scripts has been superseded by Scenes now).

Don’t guess, check the docs. Rules - Introduction | openHAB and Rules | openHAB both of which should come up at the top in a search of the docs for “script”.

In this context, a Script is just a rule without any triggers or conditions. But, like I said above, the action on the widget is not limited to calling just those rules, it can call any rule (note, a Scene is a rule too which makes a really nice feature).

Yes, that’s what it’s for. But how you access it depends on the rules language chosen for the rule that gets called.

It’s not clear but your sendmqttcommand looks like it might be Rules DSL. I’m guessing that either it’s not available or if it is you’d just use param1, maybe context.param1.

Assuming Nashorn JS you’d access “param1” using context.param1 I think IIRC.

In GraalVM you’d access it with just param1.

1 Like

This was something i did already understand and was working for me
I fully agree that this is a awasome feature

Thats the info i was looking for but i could not find anything specific about it
So my current guess is that its not possible using RulesDSL, which you guessed right → Im using for these (my very first) scripts

So i think i have to write multiple scripts (one for each value) and call these individually (what works for me right now)

This is something i have no idea about :sweat_smile:

Thanks @rlkoshak and @JustinG for helping me out!