Skip to content

Commit a1fc363

Browse files
authored
[jrubyscripting] Minor updates, add short examples at the top of README.md (openhab#18030)
Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
1 parent 4e88f48 commit a1fc363

File tree

1 file changed

+71
-42
lines changed
  • bundles/org.openhab.automation.jrubyscripting

1 file changed

+71
-42
lines changed

bundles/org.openhab.automation.jrubyscripting/README.md

+71-42
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,55 @@
33

44
# JRuby Scripting
55

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+
910
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).
1011

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+
next if 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+
1155
- [Why Ruby?](#why-ruby)
1256
- [Installation](#installation)
1357
- [Configuration](#configuration)
@@ -66,14 +110,12 @@ If you're new to Ruby, you may want to check out [Ruby Basics](https://openhab.g
66110
- [Calling Java From JRuby](#calling-java-from-jruby)
67111
- [Full Documentation](#full-documentation)
68112

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-
71113
## Why Ruby?
72114

73115
- Ruby is designed for programmers' productivity with the idea that programming should be fun for programmers.
74116
- 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.
77119
- Ruby is really good at letting one express intent and create a DSL to make that expression easier.
78120

79121
### Design points
@@ -88,35 +130,17 @@ Additional [example rules are available](https://openhab.github.io/openhab-jruby
88130
- Designed and tested using [Test-Driven Development](https://en.wikipedia.org/wiki/Test-driven_development) with [RSpec](https://rspec.info/)
89131
- Extensible.
90132
- 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/).
92134

93135
## Installation
94136

95-
### Prerequisites
96-
97-
1. openHAB 3.4+
98-
1. The JRuby Scripting Language Addon
99-
100137
### From the User Interface
101138

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).
107140

108141
### Using Files
109142

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:
115-
116-
```ini
117-
org.openhab.automation.jrubyscripting:gems=openhab-scripting=~>5.0
118-
org.openhab.automation.jrubyscripting:require=openhab/dsl
119-
```
143+
- Edit `<OPENHAB_CONF>/services/addons.cfg` and ensure that `jrubyscripting` is included in an uncommented `automation=` list of automations to install.
120144

121145
## Configuration
122146

@@ -128,16 +152,16 @@ This allows the use of [items](https://openhab.github.io/openhab-jruby/main/Open
128152
This functionality can be disabled for users who prefer to manage their own gems and `require`s via the add-on configuration options.
129153
Simply change the `gems` and `require` configuration settings.
130154

131-
| Parameter | Description |
132-
| --------------------- | -------------------------------------------------------------------------------------------------------- |
133-
| `gem_home` | The path to store Ruby Gems. <br/><br/>Default: `$OPENHAB_CONF/automation/ruby/.gem/RUBY_ENGINE_VERSION` |
134-
| `gems` | A list of gems to install. <br/><br/>Default: `openhab-scripting=~>5.0` |
135-
| `check_update` | Check for updated version of `gems` on start up or settings change. <br/><br/>Default: `true` |
136-
| `require` | List of scripts to be required automatically. <br/><br/>Default: `openhab/dsl` |
137-
| `rubylib` | Search path for user libraries. <br/><br/>Default: `$OPENHAB_CONF/automation/ruby/lib` |
138-
| `dependency_tracking` | Enable dependency tracking. <br/><br/>Default: `true` |
139-
| `local_context` | See notes below. <br/><br/>Default: `singlethread` |
140-
| `local_variables` | See notes below. <br/><br/>Default: `transient` |
155+
| Parameter | Description |
156+
| --------------------- | ---------------------------------------------------------------------------------------------------------- |
157+
| `gem_home` | The path to store Ruby Gems. <br/><br/>Default: `$OPENHAB_CONF/automation/ruby/.gem/RUBY_ENGINE_VERSION` |
158+
| `gems` | A list of gems to install. <br/><br/>Default: `openhab-scripting=~>5.0` |
159+
| `check_update` | Check for updated version of `gems` on start up or settings change. <br/><br/>Default: `true` |
160+
| `require` | List of scripts to be required automatically. <br/><br/>Default: `openhab/dsl` |
161+
| `rubylib` | Search path for user libraries. <br/><br/>Default: `$OPENHAB_CONF/automation/ruby/lib` |
162+
| `dependency_tracking` | Enable dependency tracking. <br/><br/>Default: `true` |
163+
| `local_context` | See notes below. <br/><br/>Default: `singlethread` |
164+
| `local_variables` | See notes below. <br/><br/>Default: `transient` |
141165

142166
When using file-based configuration, these parameters must be prefixed with `org.openhab.automation.jrubyscripting:`, for example:
143167

@@ -766,8 +790,9 @@ To log a message on `INFO` log level:
766790
logger.info("The current time is #{Time.now}")
767791
```
768792

769-
The default logger name for UI rules is `org.openhab.automation.jrubyscripting.script`.
770-
For file-based rules, it's based on the rule's ID, such as `org.openhab.automation.jrubyscripting.rule.myrule.rb:15`.
793+
The main logger prefix is `org.openhab.automation.jrubyscripting`.
794+
The default logger name for UI rules includes the rule ID: `org.openhab.automation.jrubyscripting.script.<RULE_ID>`.
795+
The logger name for file-based rules includes the rule's filename and the rule ID: `org.openhab.automation.jrubyscripting.<filename>.rule.<RULE_ID>`.
771796

772797
To use a custom logger name:
773798

@@ -776,7 +801,7 @@ logger = OpenHAB::Log.logger("org.openhab.custom")
776801
```
777802

778803
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.
780805

781806
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).
782807
Supported logging functions include:
@@ -815,7 +840,7 @@ end
815840
```
816841

817842
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).
819844

820845
```ruby
821846
rule "simple timer" do
@@ -1065,6 +1090,10 @@ end
10651090
start_of_day = ZonedDateTime.now.with(LocalTime::MIDNIGHT)
10661091
# or
10671092
start_of_day = LocalTime::MIDNIGHT.to_zoned_date_time
1093+
# or
1094+
start_of_day = LocalDate.now.to_zoned_date_time
1095+
# or using Ruby Date
1096+
start_of_day = Date.today.to_zoned_date_time
10681097

10691098
# Comparing ZonedDateTime against LocalTime with `<`
10701099
max = Solar_Power.maximum_since(24.hours.ago)

0 commit comments

Comments
 (0)