You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: bundles/org.openhab.automation.jrubyscripting/README.md
+71-42
Original file line number
Diff line number
Diff line change
@@ -3,11 +3,55 @@
3
3
4
4
# JRuby Scripting
5
5
6
-
This add-on provides [JRuby](https://www.jruby.org/) scripting language for automation rules.
7
-
Also included is [openhab-scripting](https://openhab.github.io/openhab-jruby/), a fairly high-level Ruby gem to support automation in openHAB.
8
-
It provides native Ruby access to common openHAB functionality within rules including items, things, actions, logging and more.
6
+
This add-on provides Ruby scripting language for automation rules.
7
+
It includes the [openhab-scripting](https://openhab.github.io/openhab-jruby/) helper library, a comprehensive Ruby gem designed to enhance automation in openHAB.
8
+
This library offers a streamlined syntax for writing file-based and UI-based rules, making it easier and more intuitive than Rules DSL, while delivering the full features of the Ruby language.
9
+
9
10
If you're new to Ruby, you may want to check out [Ruby Basics](https://openhab.github.io/openhab-jruby/main/file.ruby-basics.html).
10
11
12
+
Example file-based rules:
13
+
14
+
```ruby
15
+
rule "Turn on light when sensor changed to open"do
16
+
changed Door_Sensor# a Contact item
17
+
run do |event|
18
+
if event.open?
19
+
Cupboard_Light.on for:3.minutes # Automatically turn it off after 3 minutes
20
+
else
21
+
Cupboard_Light.off # This will automatically cancel the timer set above
22
+
end
23
+
end
24
+
end
25
+
```
26
+
27
+
```ruby
28
+
rule "Door open reminder"do
29
+
changed Doors.members, to:OPEN
30
+
run do |event|
31
+
# Create a timer using the triggering item as the timer id
32
+
# If a timer with the given id already exists, it will be rescheduled
33
+
after 5.minutes, id: event.item do |timer|
34
+
nextif timer.cancelled? || event.item.closed?
35
+
36
+
Voice.say "The #{event.item} is open"
37
+
38
+
timer.reschedule # Use the original duration by default
39
+
end
40
+
end
41
+
end
42
+
```
43
+
44
+
Example UI-based rules:
45
+
46
+
```ruby
47
+
only_every(2.minutes) do# apply rate-limiting
48
+
Audio.play_sound("doorbell.mp3")
49
+
Notification.send("Someone pressed the doorbell")
50
+
end
51
+
```
52
+
53
+
Additional [example rules are available](https://openhab.github.io/openhab-jruby/main/file.examples.html), as well as examples of [conversions from Rules DSL, JavaScript, and Python rules](https://openhab.github.io/openhab-jruby/main/file.conversions.html).
54
+
11
55
-[Why Ruby?](#why-ruby)
12
56
-[Installation](#installation)
13
57
-[Configuration](#configuration)
@@ -66,14 +110,12 @@ If you're new to Ruby, you may want to check out [Ruby Basics](https://openhab.g
66
110
-[Calling Java From JRuby](#calling-java-from-jruby)
67
111
-[Full Documentation](#full-documentation)
68
112
69
-
Additional [example rules are available](https://openhab.github.io/openhab-jruby/main/file.examples.html), as well as examples of [conversions from DSL and Python rules](https://openhab.github.io/openhab-jruby/main/file.conversions.html).
70
-
71
113
## Why Ruby?
72
114
73
115
- Ruby is designed for programmers' productivity with the idea that programming should be fun for programmers.
74
116
- Ruby emphasizes the necessity for software to be understood by humans first and computers second.
75
-
- Ruby makes writing automation enjoyable without having to fight with compilers and interpreters.
76
-
- Rich ecosystem of tools, including things like Rubocop to help developers write clean code and RSpec to test the libraries.
117
+
- Ruby makes writing automation enjoyable with its readable syntax and a rich collection of useful methods in its built-in classes.
118
+
- Rich ecosystem of tools and libraries, including things like Rubocop to help developers write clean code and RSpec to test the libraries.
77
119
- Ruby is really good at letting one express intent and create a DSL to make that expression easier.
78
120
79
121
### Design points
@@ -88,35 +130,17 @@ Additional [example rules are available](https://openhab.github.io/openhab-jruby
88
130
- Designed and tested using [Test-Driven Development](https://en.wikipedia.org/wiki/Test-driven_development) with [RSpec](https://rspec.info/)
89
131
- Extensible.
90
132
- Anyone should be able to customize and add/remove core language features
91
-
- Easy access to the Ruby ecosystem in rules through Ruby gems.
133
+
- Easy access to the Ruby ecosystem in rules through [Ruby Gems](https://rubygems.org/).
92
134
93
135
## Installation
94
136
95
-
### Prerequisites
96
-
97
-
1. openHAB 3.4+
98
-
1. The JRuby Scripting Language Addon
99
-
100
137
### From the User Interface
101
138
102
-
1. Go to `Settings -> Add-ons -> Automation` and install the jrubyscripting automation addon following the [openHAB instructions](https://www.openhab.org/docs/configuration/addons.html).
103
-
In openHAB 4.0+ the defaults are set so the next step can be skipped.
104
-
1. Go to `Settings -> Add-on Settings -> JRuby Scripting`:
105
-
-**Ruby Gems**: `openhab-scripting=~>5.0`
106
-
-**Require Scripts**: `openhab/dsl` (not required, but recommended)
139
+
- Go to `Settings -> Add-ons -> Automation` and install the jrubyscripting automation addon following the [openHAB instructions](https://www.openhab.org/docs/configuration/addons.html).
107
140
108
141
### Using Files
109
142
110
-
1. Edit `<OPENHAB_CONF>/services/addons.cfg` and ensure that `jrubyscripting` is included in an uncommented `automation=` list of automations to install.
111
-
In openHAB 4.0+ the defaults are set so the next step can be skipped.
112
-
1. Configure JRuby openHAB services
113
-
114
-
Create a file called `jruby.cfg` in `<OPENHAB_CONF>/services/` with the following content:
- Edit `<OPENHAB_CONF>/services/addons.cfg` and ensure that `jrubyscripting` is included in an uncommented `automation=` list of automations to install.
120
144
121
145
## Configuration
122
146
@@ -128,16 +152,16 @@ This allows the use of [items](https://openhab.github.io/openhab-jruby/main/Open
128
152
This functionality can be disabled for users who prefer to manage their own gems and `require`s via the add-on configuration options.
129
153
Simply change the `gems` and `require` configuration settings.
Please be aware that messages might not appear in the logs if the logger name does not start with `org.openhab`.
779
-
This behaviour is due to [log4j2](https://logging.apache.org/log4j/2.x/) requiring definition for each logger prefix.
804
+
This behavior is due to [log4j2](https://logging.apache.org/log4j/2.x/) requiring definition for each logger prefix.
780
805
781
806
The [logger](https://openhab.github.io/openhab-jruby/main/OpenHAB/Logger.html) is similar to a standard [Ruby Logger](https://docs.ruby-lang.org/en/master/Logger.html).
782
807
Supported logging functions include:
@@ -815,7 +840,7 @@ end
815
840
```
816
841
817
842
Alternatively a timer can be used in either a file-based rule or in a UI based rule using [after](https://openhab.github.io/openhab-jruby/main/OpenHAB/DSL.html#after-class_method).
818
-
After takes a [Duration](#durations), e.g. `10.minutes` instead of using [ZonedDateTime](https://openhab.github.io/openhab-jruby/main/OpenHAB/CoreExt/Java/ZonedDateTime.html).
843
+
After takes a [Duration](#durations) relative to `now`, e.g. `10.minutes`, or an absolute time with [ZonedDateTime](https://openhab.github.io/openhab-jruby/main/OpenHAB/CoreExt/Java/ZonedDateTime.html) or [Time](https://openhab.github.io/openhab-jruby/main/Time.html).
0 commit comments