]> git.basschouten.com Git - openhab-addons.git/commitdiff
[jrubyscripting] document how to use Ruby in a transformation (#13286)
authorCody Cutrer <cody@cutrer.us>
Mon, 22 Aug 2022 13:46:18 +0000 (07:46 -0600)
committerGitHub <noreply@github.com>
Mon, 22 Aug 2022 13:46:18 +0000 (15:46 +0200)
using the core ScriptTransformationService. See
https://github.com/openhab/openhab-addons/pull/13255 for prior
discussion on this.

Signed-off-by: Cody Cutrer <cody@cutrer.us>
bundles/org.openhab.automation.jrubyscripting/README.md

index 6cdaaa104e4cb69bfbd63d4c6128a8ea479f7acf..7c1df5d6f4d09c4fc2f8790e072879c9513477c6 100644 (file)
@@ -75,3 +75,37 @@ JRuby can [import Java classes](https://github.com/jruby/jruby/wiki/CallingJavaF
 Depending on the openHAB logging configuration, you may need to prefix logger names with `org.openhab.automation` for them to show up in the log file (or you modify the logging configuration).
 
 **Note**: Installing the [JRuby Scripting Library](https://boc-tothefuture.github.io/openhab-jruby/installation/) will provide enhanced capabilities with simpler rule syntax.
+
+## Transformations
+
+This add-on also provides the necessary infrastructure to use Ruby for writing [transformations](https://www.openhab.org/docs/configuration/transformations.html).
+Once the addon is installed, you can create a Ruby file in the `$OPENHAB_CONF/transform` directory, with the extension `.script`.
+It's important that the extension is `.script` so that the core `SCRIPT` transform service will recognize it.
+When referencing the file, you need to specify the `SCRIPT` transform, with `rb` as the script type: `SCRIPT(rb:mytransform.script):%s`.
+You can also specify additional variables to be set in the script using a URI-like query syntax: `SCRIPT(rb:mytransform.script?a=1b=c):%s` in order to share a single script with slightly different parameters for different items.
+
+**Note**: Due to an [issue](https://github.com/jruby/jruby/issues/5876) in the current version of JRuby, you will need to begin your script with `input ||= nil` (and `a ||= nil` etc. for additional query variables) so that JRuby will recognize the variables as variables--rather than method calls--when it's parsing the script.
+Otherwise you will get errors like `(NameError) undefined local variable or method 'input' for main:Object`.
+
+### Example Transformation
+
+#### compass.script
+
+```ruby
+input ||= nil
+DIRECTIONS = %w[N NE E SE S SW W NW N].freeze
+
+if input.nil? || input == "NULL" || input == "UNDEF"
+  "-"
+else
+  cardinal = DIRECTIONS[(input.to_f / 45).round]
+  "#{cardinal} (#{input.to_i}°)"
+end
+```
+
+#### weather.items
+```Xtend
+Number:Angle Exterior_WindDirection "Wind Direction [SCRIPT(rb:compass.script):%s]" <wind>
+```
+
+Given a state of `82 °`, this will produce a formatted state of `E (82°)`.