Calling rule from ui, parameter handover possible?

All of my Jython rules have similar configuration:

triggers = []
triggers.append(ItemStateChangeTrigger("MailDrop", "OPEN", "CLOSED", "DROPSTART").trigger)
triggers.append(ItemStateChangeTrigger("MailDrop", "CLOSED", "OPEN", "DROPFINISHED").trigger)
  

@rule("Letter Drop Rule", description="This Rule monitors dropiing letters into the bin")
class iCloudRule(object):
    def __init__(self):
        self.triggers = triggers
.......        
      
    def execute(self, module, inputs):
        act = str(inputs["module"]).split("_")[0]
        if act == "DROPSTART":
            self.dropStart()
        elif act == "DROPFINISHED":
            self.dropFinished()
  1. I define an array of triggers with trigger names (e.g. DROPSTART, DROPFINISHED)
  2. In the execute the I find out what the trigger is and the stuff what needs to be done

My question is:

Is it possible to pass in some parameters when the rule is called from within UI (e.g. oh-button with rule action)?

The reason is that most of my rules run on channel triggers and I do not want to create items for buttons in order to trigger rules from within UI.

Why not, they are free? All interaction with UI is via Items so I think you’re not gong to find a way.

1 Like

Thanks for the feedback

Anyway calling a file-based rule from UI has another problem:
each time the file is saved the rule gets another UID. The UI references rule UID’s. each time you save a changed file you lose the connection to the rule inside the UI.

I will implement the following solution:

1. Define a proxy item

String MyRuleTrigger "My Rule Trigger Proxy"

2. Set this proxy item to textual commands whatever in UI

3. Evaluate these commands inside the rule

triggers = []
triggers.append(ItemStateUpdateTrigger("MyRuleTrigger", None, "UI-TRIG").trigger)


@rule("test rule", description="test calling from ui")   
class rsManagerRule(object):    
    def __init__(self):   
        self.triggers = triggers

    def execute(self, module, inputs):    
        act = str(inputs["module"]).split("_")[0]
        # value of MyRuleTrigger causing the trigger event
        value = inputs[inputs["module"]+".state"]

Getting the value of MyRuleTrigger from the rule inputs binds also the event with the value

It’s not clear which rule you are talking about that was created in the UI. Are you calling a rule created in the UI from your Python rule? Or are you calling your Python rule from a rule created in the UI?

If the former then yes, it’s possible to pass data to the rule you’ve called. I think this is supported by the Helper Libraries (look at core.rules).

If not it’ll look something like the following JavaScript

// Run another rule
var FrameworkUtil = Java.type("org.osgi.framework.FrameworkUtil");
var _bundle = FrameworkUtil.getBundle(scriptExtension.class);
var bundle_context = _bundle.getBundleContext()
var classname = "org.openhab.core.automation.RuleManager"
var RuleManager_Ref = bundle_context.getServiceReference(classname);
var RuleManager = bundle_context.getService(RuleManager_Ref);
RuleManager.runNow("tocall");
var map = new java.util.HashMap();
map.put("test_data", "Passed data to called function!")
RuleManager.runNow("tocall", true, map); // second argument is whether to consider the conditions, third is a Map<String, Object>

map will be available as context in the called rule.

if the latter, then no it’s not possible. There is no place to in the UI to specify the data to pass in the forms. Though you can use the same code as above in a Script Action defined in the UI.

1 Like

It is the latter.

But I think the solution with a command to a string-type item is better for me. I could also pack a JSON string into the command (no clue how big that can be?).

component: oh-image-card
config:
  action: options
  actionRule: 148b2a2f-0f81-48fd-ab30-6c0bd96e21ee
  actionItem: MyRuleTrigger
  actionOptions: '{"StudyRoomRoSh":[{"ORIN":180,"FUSU":53,"FUSD":53,"AUTO":0,"WDCO":"studyWindow"},{"UP":[["CET","deconz:switch:00212E00C488:04cf8cdf3c77c3a2010012:buttonevent","6001",""]],"DN":[["CET","deconz:switch:00212E00C488:04cf8cdf3c77c3a2010012:buttonevent","5001",""]],"45DEG":[["CET","deconz:switch:00212E00C488:04cf8cdf3c77c3a2010012:buttonevent","3002",""]],"90DEG":[["CET","deconz:switch:00212E00C488:04cf8cdf3c77c3a2010012:buttonevent","4002",""]],"OP":[["CET","deconz:switch:00212E00C488:04cf8cdf3c77c3a2010012:buttonevent","6002",""]],"CL":[["CET","deconz:switch:00212E00C488:04cf8cdf3c77c3a2010012:buttonevent","5002",""]],"PO":[["ISCT","StudyRoomRoSh","",""]]}]}'
slots: null

I think the max String length in Java is something north of 2 GB.