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

**URL:** <https://community.openhab.org/t/wiki-getting-started-with-oh3-rewriting-the-tutorial-9-make-everything-work-together-rules-advanced/101046>\
**Category:** Documentation\
**Tags:** tutorial, documentation, openhab3\
**Created:** [June 20, 2020, 2:13pm UTC](https://community.openhab.org/t/wiki-getting-started-with-oh3-rewriting-the-tutorial-9-make-everything-work-together-rules-advanced/101046 "2020-06-20T14:13:59Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ysc](https://community.openhab.org/letter_avatar_proxy/v4/letter/y/e480ec/32.png) [@ysc](https://community.openhab.org/u/ysc)\
**Post date:** [June 20, 2020, 2:13pm UTC](https://community.openhab.org/t/wiki-getting-started-with-oh3-rewriting-the-tutorial-9-make-everything-work-together-rules-advanced/101046/1 "2020-06-20T14:13:59Z")

</div>

This has now been merged into the main docs (should be available sometime in the next day or so after the next build). Thanks for all comments and edits! I’m closing this thread now.

rlkoshak

* * *

_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](https://community.openhab.org/t/washing-machine-state-machine/15587) updated for OH3!_

Prerequisites:

- 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](https://www.openhab.org/addons/bindings/zwave/thing.html?thingTypeUID=fibaro_fgwp102_03_002)
- 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:

 ![image](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/6/d/6d015c5fab02aab66c1b63b265e37b8cfd9ee14b.png)

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.

 ![image](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/c/6/c6c35fbeff260a40e352e82c519133e3b537ebc7.png)

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:

 ![image](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/0/b/0b93a072a610cd9854e26a03bc3eec4b532b2b3b.png)

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:

 ![image](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/7/e/7eba28e306181f7e1ac5c0fb1af9a30324bd76db.png)

According to the [documentation of the hue binding](https://www.openhab.org/addons/bindings/hue/#channels), 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.

 ![image](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/e/6/e627503e865600b0ca59adc75b92dfdb7087fae3.png)

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.

 ![image](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/1/d/1da7383c9255ee2eaf213be29f3ece29d72411e2.png)

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.

 ![image](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/8/b/8b28e8de0373b1aec4f8091d043a5865da372963.png)

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.

 ![image](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/f/4/f480bc16fe2e9874d8ecf1c5fc102cf70c56d1c6.png)

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

Type the following code:

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

```

 ![image](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/d/a/da1d1e75924e58d1d57610f84d8e25909512a12b.png)

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!

 ![image](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/b/5/b5d1047f58f3b7cb055fc88a4eb4d1e850b52c97.png)

Previous → [Your first rule](https://community.openhab.org/t/wiki-getting-started-with-oh3-rewriting-the-tutorial-8-rules/109360)

---

<div class="post-metadata">

**Author:** ![ysc](https://community.openhab.org/letter_avatar_proxy/v4/letter/y/e480ec/32.png) [@ysc](https://community.openhab.org/u/ysc)\
**Post date:** [August 1, 2020, 6:14am UTC](https://community.openhab.org/t/wiki-getting-started-with-oh3-rewriting-the-tutorial-9-make-everything-work-together-rules-advanced/101046/2 "2020-08-01T06:14:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [October 16, 2020, 2:46pm UTC](https://community.openhab.org/t/wiki-getting-started-with-oh3-rewriting-the-tutorial-9-make-everything-work-together-rules-advanced/101046/3 "2020-10-16T14:46:53Z")

</div>



---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [October 16, 2020, 4:32pm UTC](https://community.openhab.org/t/wiki-getting-started-with-oh3-rewriting-the-tutorial-9-make-everything-work-together-rules-advanced/101046/4 "2020-10-16T16:32:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![FunMiles](https://community.openhab.org/user_avatar/community.openhab.org/funmiles/32/37386_2.png) [@FunMiles](https://community.openhab.org/u/FunMiles)\
**Post date:** [October 22, 2020, 7:33pm UTC](https://community.openhab.org/t/wiki-getting-started-with-oh3-rewriting-the-tutorial-9-make-everything-work-together-rules-advanced/101046/5 "2020-10-22T19:33:11Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [October 22, 2020, 8:58pm UTC](https://community.openhab.org/t/wiki-getting-started-with-oh3-rewriting-the-tutorial-9-make-everything-work-together-rules-advanced/101046/6 "2020-10-22T20:58:29Z")

</div>

> [@FunMiles](#):
>
> 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.

I use the following code:

```javascript
    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.

> [@FunMiles](#):
>
> `setTimeout(...)` is not found. How do we create a delay?

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

```
java.lang.Thread.sleep(1000);

```

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.

---

<div class="post-metadata">

**Author:** ![5iver](https://community.openhab.org/user_avatar/community.openhab.org/5iver/32/21201_2.png) [@5iver](https://community.openhab.org/u/5iver)\
**Post date:** [October 26, 2020, 7:28am UTC](https://community.openhab.org/t/wiki-getting-started-with-oh3-rewriting-the-tutorial-9-make-everything-work-together-rules-advanced/101046/7 "2020-10-26T07:28:42Z")

</div>

> [@FunMiles](#):
>
> How does one print something to the events log?

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

> **[Logging — openHAB Helper Libraries documentation](https://openhab-scripters.github.io/openhab-helper-libraries/Guides/Logging.html#logaction)**

> [@FunMiles](#):
>
> What’s the best way to make rules for the voice system?

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

> **[Logging — openHAB Helper Libraries documentation](https://openhab-scripters.github.io/openhab-helper-libraries/Guides/Logging.html#logaction)**

---

<div class="post-metadata">

**Author:** ![matt1](https://community.openhab.org/user_avatar/community.openhab.org/matt1/32/19930_2.png) [@matt1](https://community.openhab.org/u/matt1)\
**Post date:** [November 13, 2020, 12:34pm UTC](https://community.openhab.org/t/wiki-getting-started-with-oh3-rewriting-the-tutorial-9-make-everything-work-together-rules-advanced/101046/8 "2020-11-13T12:34:26Z")

</div>

Next article here

> [@Building Pages in the OH3 UI: documentation draft (1/3)](https://community.openhab.org/t/wiki-building-pages-in-the-oh3-ui-documentation-draft-1-3/104382):
>
> UPDATE: The most current information on this topic is now available at: [User Interface Design Overview | openHAB](https://openhab.org/docs/ui/). The following is kept for archival purposes only! TOC: I. Pages basics (this topic) Concepts: UI components, pages & widgets Sitemaps Types of pages Page editors, creating a page & adding widgets [II. Editing pages & widgets: general information](https://community.openhab.org/t/wiki-buiding-pages-in-the-oh3-ui-documentation-draft-2-3/104392) Configuring widgets The system & standard libraries, default widgets (standalone, list items & cells), adding widgets from the model W…

---

<div class="post-metadata">

**Author:** ![FunMiles](https://community.openhab.org/user_avatar/community.openhab.org/funmiles/32/37386_2.png) [@FunMiles](https://community.openhab.org/u/FunMiles)\
**Post date:** [November 14, 2020, 3:13pm UTC](https://community.openhab.org/t/wiki-getting-started-with-oh3-rewriting-the-tutorial-9-make-everything-work-together-rules-advanced/101046/9 "2020-11-14T15:13:19Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [November 14, 2020, 3:25pm UTC](https://community.openhab.org/t/wiki-getting-started-with-oh3-rewriting-the-tutorial-9-make-everything-work-together-rules-advanced/101046/10 "2020-11-14T15:25:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Olymp](https://community.openhab.org/user_avatar/community.openhab.org/olymp/32/5553_2.png) [@Olymp](https://community.openhab.org/u/Olymp)\
**Post date:** [November 17, 2020, 6:00am UTC](https://community.openhab.org/t/wiki-getting-started-with-oh3-rewriting-the-tutorial-9-make-everything-work-together-rules-advanced/101046/11 "2020-11-17T06:00:41Z")

</div>

> [@rlkoshak](#):
>
> Joda DateTime is related with ZonedDateTime

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

```

script:

```
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?  
Thanks!  
UPD:

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

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

```

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [November 17, 2020, 3:06pm UTC](https://community.openhab.org/t/wiki-getting-started-with-oh3-rewriting-the-tutorial-9-make-everything-work-together-rules-advanced/101046/12 "2020-11-17T15:06:52Z")

</div>

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());

```

---

<div class="post-metadata">

**Author:** ![Olymp](https://community.openhab.org/user_avatar/community.openhab.org/olymp/32/5553_2.png) [@Olymp](https://community.openhab.org/u/Olymp)\
**Post date:** [November 18, 2020, 7:01am UTC](https://community.openhab.org/t/wiki-getting-started-with-oh3-rewriting-the-tutorial-9-make-everything-work-together-rules-advanced/101046/13 "2020-11-18T07:01:19Z")

</div>

> [@rlkoshak](#):
>
> So it’s probably better to use:

It works, thanks!  
Offtopic:  
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!

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [November 18, 2020, 3:00pm UTC](https://community.openhab.org/t/wiki-getting-started-with-oh3-rewriting-the-tutorial-9-make-everything-work-together-rules-advanced/101046/14 "2020-11-18T15:00:26Z")

</div>

> [@Olymp](#):
>
> are there any of your rules rewritten in Java for OH3?

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 [GitHub - rkoshak/openhab-rules-tools: Library functions, classes, and examples to reuse in the development of new Rules.](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.

---

<div class="post-metadata">

**Author:** ![querdenker2k](https://community.openhab.org/user_avatar/community.openhab.org/querdenker2k/32/17523_2.png) [@querdenker2k](https://community.openhab.org/u/querdenker2k)\
**Post date:** [December 11, 2020, 8:25pm UTC](https://community.openhab.org/t/wiki-getting-started-with-oh3-rewriting-the-tutorial-9-make-everything-work-together-rules-advanced/101046/16 "2020-12-11T20:25:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [December 11, 2020, 11:17pm UTC](https://community.openhab.org/t/wiki-getting-started-with-oh3-rewriting-the-tutorial-9-make-everything-work-together-rules-advanced/101046/17 "2020-12-11T23:17:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kovacsi2899](https://community.openhab.org/user_avatar/community.openhab.org/kovacsi2899/32/80943_2.png) [@kovacsi2899](https://community.openhab.org/u/kovacsi2899)\
**Post date:** [December 13, 2020, 5:31am UTC](https://community.openhab.org/t/wiki-getting-started-with-oh3-rewriting-the-tutorial-9-make-everything-work-together-rules-advanced/101046/18 "2020-12-13T05:31:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mstormi](https://community.openhab.org/user_avatar/community.openhab.org/mstormi/32/55857_2.png) [@mstormi](https://community.openhab.org/u/mstormi)\
**Post date:** [December 13, 2020, 11:56am UTC](https://community.openhab.org/t/wiki-getting-started-with-oh3-rewriting-the-tutorial-9-make-everything-work-together-rules-advanced/101046/19 "2020-12-13T11:56:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kovacsi2899](https://community.openhab.org/user_avatar/community.openhab.org/kovacsi2899/32/80943_2.png) [@kovacsi2899](https://community.openhab.org/u/kovacsi2899)\
**Post date:** [December 14, 2020, 5:19am UTC](https://community.openhab.org/t/wiki-getting-started-with-oh3-rewriting-the-tutorial-9-make-everything-work-together-rules-advanced/101046/20 "2020-12-14T05:19:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mstormi](https://community.openhab.org/user_avatar/community.openhab.org/mstormi/32/55857_2.png) [@mstormi](https://community.openhab.org/u/mstormi)\
**Post date:** [December 14, 2020, 9:47am UTC](https://community.openhab.org/t/wiki-getting-started-with-oh3-rewriting-the-tutorial-9-make-everything-work-together-rules-advanced/101046/21 "2020-12-14T09:47:51Z")

</div>

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

[Next page](https://community.openhab.org/t/wiki-getting-started-with-oh3-rewriting-the-tutorial-9-make-everything-work-together-rules-advanced/101046.md?page=2)
