[wiki] Getting Started with OH3: rewriting the tutorial - 9. Make everything work together: rules (advanced)

Tags: #<Tag:0x00007fc901630b88> #<Tag:0x00007fc901630ac0> #<Tag:0x00007fc9016309f8>

This Wiki topic’s goal is to serve as a working document, in order to provide an up-to-date tutorial for OH3 aimed at new users and novices. Everyone is welcome and encouraged to edit it, when we’re satisfied it will make its way to the official docs. You may reply for the specific purpose of improving the article are allowed, but please, this is NOT a support thread: if you’re stuck while reading this and are simply seeking support please open another thread. Thanks!

Note: this is more of a showcase than a tutorial, but I thought it would be a nice way to wrap up the tutorial with a real example of what you can achieve with openHAB and try to give some inspiration to the reader. It’s one of examples I come up with when I’m asked about what openHAB can do. Credit where it’s due, it’s only a version of Washing Machine State Machine updated for OH3!


  • a regular washing machine without connected capabilities
  • a connected wall plug that’s able to report the power consumption - in this example, we’ll use a Z-Wave FGWP102 Metered Wall Plug Switch
  • a connected lightbulb - in this example, a Philips Hue one

Scenario: you want to “smarten” your washing machine and make it so that you’re notified when it has finished its cycle, for instance by flashing a light bulb in another room.

This is what your model could look like:

We have Equipment representing the washing machine, the wall plug it’s attached to (as a sub-Equipment), and a Measurement point for reporting the power consumption.

This item is linked to the channel of the metered wall plug so that it’s updated in real-time.

We first need to figure out when we can consider the washing machine is done, based on the evolution of that metric - openHAB’s UI Analyzer can help us do that.

Click on Analyze to open it and try to find a previous typical laundry cycle. Open the controls by clicking on the button in the bottom toolbar and change settings as needed, as well as the chart period with the controls on the top-right corner, until you get a good view like this:

What we can conclude from it, is that while the washing machine is running, the power consumption never gets under 2 W; when it finally does, that would mean that the cycle is finished. We therefore want to detect when the reported power changes from over 2 watts to below 2 watts and react to it.

Close the Analyzer, and take a note of the name of the item (here FGWP102WallPlug_Power).

Back to the Model, let’s examine the “Kitchen Table Light” equipment in the kitchen:

According to the documentation of the hue binding, the light bulb defines an “alert” channel, which will make the light bulb flash when we send certain commands (SELECT, LSELECT or NONE). we have therefore added a Point item and linked it to that channel. Pushing the default widget for it opens a list of command options provided by the binding; we can try selecting “Long Alert”: the light will flash for a couple of seconds - that’s what we want.

Let’s create a rule to make these 2 items work together. Click on Rules on the sidebar.

Create a new rule using the “+” button. Change the name and ID.

Save (with Ctrl-S on the Save button in the title bar), then open the Rule again.

We want to trigger the rule when the state of FGWP102WallPlug_Power changes: click on Add Trigger, select “when an item state changes” and pick the item.

Here, we cannot use the “Previous State” and “State” of the trigger because it requires exact states and we don’t actually know what they’ll be, so we have to make the rule run whenever the state changes and try to implement the logic somewhere else.

We’ll do it in a script action.Click Add Action and choose “execute a given script”.

You’re given the choice of the language you want to use for your script, ECMAScript (i.e. JavaScript) or the openHAB domain-specific language uses for rules (Rule DSL). Let’s choose ECMAScript.

Click on the Edit script button, this will open a code editor.

Type the following code:

var threshold = new QuantityType("2 W")
if (newState < threshold  && oldState >= threshold) {
  events.sendCommand('HueKitchen_Alert', 'LSELECT');

Here are some explanations:

  • The trigger will define some variables for you: oldState and newState you can use to get the previous and current states;
  • By checking the logs you noticed that since the item has a Unit of Measurement, the states will not actually be numbers but instead strings in the format 23.6 W. We therefore need to create a QuantityType using the same general units to perform the comparison. So we create a new threshold variable which is of type W.
  • Once we have a value we can compare to the two states, we can send use the events.sendCommand method made available to the script to send the LSELECT command to the HueKitchen_Alert item - but only when the previous parsed state was >=2 and the current one is < 2.

Close the code editor, and save the rule. You should now see the lights flashing when the power drawn from the washing machine drops below 2 W from above that threshold!

Previous -> Your first rule


This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.

I’ve stared playing with Rules in the new editor. This is much better than the old PaperUI interface by far. I’m also converting my Rules over to JavaScript as I want my examples to be based on something that the users will have by default without needing to install other stuff.

I’ve updated the doc to create a QuantityType to compare to rather than parsing the W out of the toString of the states. Over all that should provide more flexibility to users as they can pick and choose compatible units for comparison. It also looks more deliberate than a work around.

I do have two quick questions though.

  1. Where did executeCommandLine go? I’ve tried to import org.openhab.core.model.script.actions.Exec as well as org.openhab.core.io.net.exec.ExecUtil and in both cases I get an error that the classes can’t be found. I can find those classes at those paths in GitHub though so I’m left wondering if something else is up.

  2. Is there a way to store a variable from one run to another using just the UI? For example, a common use case is to set a Timer and when the rule runs again check to see if the Timer already exists reschedule it. But it doesn’t seem that there is any place to put a variable like that. Am I missing something?

An insight you might have on these are greatly appreciated.

1 Like

I have also a need for that item 2.
I’m having trouble with Ecmascript 5. I can write many things in version 6, but they are not working here.

  • How does one print something to the events log? I found l found the code from another post, retrieving a logger from Java, but I am not seeing the output.

  • setTimeout(...) is not found. How do we create a delay?

What’s the best way to make rules for the voice system?


I use the following code:

    var logger = Java.type("org.slf4j.LoggerFactory").getLogger("org.openhab.model.script.Rules.Experiments");
    logger.error("This is an error log");
    logger.warn("This is a warning log");
    logger.info("This is an info log");
    logger.debug("This is a debug log");
    logger.trace("This is a trace log");

Pay attention to the path provided in getLogger. You will want to keep everything but the last field (“Experiments”). This will use the same parent logger as regular rules so you can control the log just as we always have in the logger config file.

If you use the JavaScript Helper Libraries there are helper functions around this. I also think there is a more OH native way to get the logger.

Keep in mind that there is full integration between JavaScript and the underlying Java. So you can just use


However, based on some reading I did awhile back, doing so is considered quite controversial in Nashorn with some saying it should never be done and others who think it’s just fine.

1 Like

Have you tried the helper libraries? Here is an example of using LogAction in JS using actions.js…


Here are some examples of core actions, including ‘say’…


Next article here

The two links you provided are too the same page.
When I was talking about voice rules, I meant the ones receiving commands from voice recognition. I have a bunch of rules to turn on scenes and actions that I wouldn’t want to go without. Can I take my old rules and just put them in the rules directory? Will they all work?

Yes, that should mostly work. There are a couple minor breaking changes, the biggest of which is Joda DateTime is related with ZonedDateTime and there are some minor differences between the two.

If you’ve already got the voice to text set up you have an Item that received the text, right? So trigger the rule on that Item updating and continue as usual.

I can’t update the value of an item of type datetime, error:

2020-11-17 13:56:53.092 [ERROR] [e.automation.internal.RuleEngineImpl] - Failed to execute rule 'last_update': Fail to execute action: 2


var ZonedDateTime  = Java.type("java.time.ZonedDateTime");
events.sendCommand(event.itemName + "_Update", ZonedDateTime.now())  //.toString()

I reviewed the entire forum with the OH3 tag, I could not find a single example of how to do this. Please tell me what am I doing wrong?

  - id: "1"
      groupName: gLastUpdate
    type: core.GroupStateUpdateTrigger
conditions: []
  - inputs: {}
    id: "2"
      type: application/javascript
      script: >-
        var ZonedDateTime  = Java.type("java.time.ZonedDateTime");

        events.sendCommand(event.itemName + "_Update", ZonedDateTime.now())  //.toString()
    type: script.ScriptAction

events.sendCommand requires two strings as arguments.

events.sendCommand(event.itemName + "_Update", ZonedDateTime.now().toString());

But there is a further complication as I think ZonedDateTime does not provide an ISO 8601 formatted string by default. So it’s probably better to use:

events.sendCommand(event.itemName+"_Update", new DateTimeType().toString());

It works, thanks!
Looking at your jiton rules, I rewrote mine on a jiton,
are there any of your rules rewritten in Java for OH3?
In them you can always find the answer to most of the questions.
Thanks for doing this!

No because Java isn’t a supported language.

I have started rewriting some in EMCAScript (also known as JavaScript). You can find some of them at https://github.com/rkoshak/openhab-rules-tools. So far I have time_utils, time_mgr, debounce, and ephem_tod. I’ve a few others written but I’m not only focusing on rules right now so it’s slow going.

As @rlkoshak already asked, how is it possible via ui to share variables or functions over different rules/scripts?

It can’t be done. You can preserve variables between runs of a single rule by saving it to this in JavaScript. I don’t know it it’s possible to do that in Rules DSL. I’ve no idea for any of the other languages.

Each rule gets it’s own context so they don’t share variables.

There is a ScriptExtensions feature that I’ve yet to figure out how to use that is supposed to be usable for sharing variables between different rules.

If you have constants, you can define them in automation/lib/javascript/configuration.js (or put constants and reusable functions in autoamtion/lib/javascript/personal and load them into your rule.

Does it mean that NGRE rule engine is working? It was part of that extension.

NGRE is the default in OH3. Of course it is working (unless you refer to specific conditions you didn’t explain, but for these open a new thread. This one isn’t for discussion).

1 Like

Ok. Then can I install Phyton in the current version? If yes I try and in case no succes I open separate topic to that.

No. This will require an OH3 addon which isn’t available yet.
Only post here if on topic please (the topic in the title, not yours.)