I want to create a script that I can call from various rules, to avoid duplicating code.
I’ve found this article, explaining what to do. I’m not sure if the information in there is still relevant (given OH2).
I started with a small POC, just to see if I got the basics right (including passing 2 parameters from a rule to a script).
I create a file called “sendsms.script” and I’ve put it in the scripts-folder.
The content is this:
val org.eclipse.xtext.xbase.lib.Functions$Function2 sendsms = [ org.openhab.core.library.items.StringItem sms_text, org.openhab.core.library.items.StringItem sms_recipient | logInfo ("script", "The sendsms script is called") logInfo ("script", "First parameter =" + sms_text) logInfo ("script", "Second parameter =" + sms_recipient) ]
My openhab.log shows:
2017-05-30 16:03:22.991 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'sendsms.script' 2017-05-30 16:03:23.183 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model 'sendsms.script', using it anyway: The value of the local variable sms is not used Function2 is a raw type. References to generic type Function2<P1, P2, Result> should be parameterized
I guess the validation issue is ok, since I am not doing anything besides a logInfo.
I’m not sure if I understand the second part (Function2 is a raw type).
In a rule, I try to call the script using:
sendsms.apply("some text", "some recipient")
However, when I execute the rule, I get:
2017-05-30 16:05:59.124 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'test': An error occured during the script execution: The name 'sendsms' cannot be resolved to an item or type.
Is there some more documentation or other help, allowing me to execute a script from a rule with 2 parameters?