Running the same script by many rules

Hello,

Suppose I have a script in OH4, when I configure two different rules to run the same script and I choose “similar” triggers which normally trigger rules at the same time, I get this error:

[ERROR] [e.automation.internal.RuleEngineImpl] - Failed to execute rule ‘heatpump-zone' with status 'RUNNING'

Does it mean that it is not possible to run two instances of the same script in parallel? Is there any workaround?

More details of the use case:
I have 6 heatpump zones in my house, I developed a generic script which implements logic of zone controlling. Zones are defined as equipments with unified model (items for temperature, damper etc). When the script is triggered, it identifies which zone should be handled by finding a corresponding equipment group, eg:

var group = items.getItem(triggeringItem.getGroupNames().get(0));

Then the scrip finds necessary items within found group:

var damper = group.members.find(x => x.tags.indexOf("Control") >= 0);
var setpoint = group.members.find(x => x.tags.indexOf("Setpoint") >= 0).numericState;
var temp = group.members.find(x => x.tags.indexOf("Measurement") >= 0).numericState;
var mode = group.members.find(x => x.tags.indexOf("HeatpumpMode") >= 0).state;

As you can see, the logic is unified and it is not tied to any specific damper. An advantage of this approach is to have the logic implemented in a single script which is triggered by several rules. If you change the script (bugfix for ex), it changes for all rules / zones.

We need more details. What rules language, it looks like a weird mix of JS Scripting and Rules DSL. There is no such thing as triggeringItem in JS Scripting, that’s items[event.itemName].

For JS Scripting indeed, this is not allowed. But you’ll get a “Multithreaded not allowed for JS” exception, not the error you show.

This seems a bit like an XY problem though.

Why not implement it in just one rule then? What advantage to you get using multiple rules that do the same thing when you can have the logic implemented in a single script in a single rule?

A better alternative for JS Scripting if there is a compelling reason not to implement one rule for all dampers and when there is a chance that the rule will be called from more than one other rule at the same time would be to put the code into a personal node module (i.e. a library). Details for that can be found at JavaScript Scripting - Automation | openHAB.

And if all that your called script does is set some variables then it’s not actually doing what you need. Those variables do not make it back to the rule that called the script. So calling this script doesn’t do anything at all.

why couldn’t you just have one rule, with multiple triggers?

In fact, if you structured your groups properly, you could even just do it with one trigger using member-of-group trigger.

Thank you for your hints @rlkoshak and @JimT. I’m experienced software dev, but that was not very obvious for a strange reason that I could use only one rule :see_no_evil:. Using only one rule with multiple triggers I was able to set up 6 dampers/zones to use single script.