openhab / openhab-js

openHAB JavaScript Library for JavaScript Scripting Automation
https://www.openhab.org/addons/automation/jsscripting/
Eclipse Public License 2.0
38 stars 31 forks source link

Support passed in values for manually called JSRules #333

Closed rkoshak closed 4 weeks ago

rkoshak commented 1 month ago

I've had a few users who want to use my rule templates but who also want to use .js files and JSRule for all their other rules. This means that they have a rule that gets called from a UI rule and when called data is passed to the called rule.

I UI rules one can simply use the passed in variables by name. Clearly that won't work with JSRule.

I've looked at how the event Object is created: https://github.com/openhab/openhab-js/blob/523d874a192d97444f502c271e3ed32b8bc45c63/src/rules/rules.js#L358 and it's not clear to me what the last line of that function is doing. What's "module" and does that contain the additional passed in data?

If not, would it be acceptable to do one of the following?

  1. if there is no rule trigger (i.e. it was manually triggered), stuff data with anything extra that's in input
  2. no matter how the rule is triggered, add anything extra in input to data
  3. add input itself to data as raw

With option 1 and 2, if the calling rule passes a variable foo the callee rule would access that via event.foo.

With option 3, if the calling rule passed a variable foo the callee rule would access that via event.raw.foo (or something more appropriate if raw is the wrong name).

Or maybe module already does option 3? I assume this is the result from https://www.openhab.org/javadoc/latest/org/openhab/core/automation/rule#getModules() and the docs for Module talk about Inputs but as far as I can tell this is not what I'm looking for.

Before I go too far down the rabbit hole researching and prepare a PR I want to make sure it would be accepted in the first place. I'm inclined to use approach 3 as the least likely to cause problems now and in the long run.

Your Environment

openhab-js 5.0.0 is my current environment.

florian-h05 commented 1 month ago

Let's go for solution 3! BTW I have just tested this, it works this way :+1: Please don't forget to update the README.