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

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?

2 Likes

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

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.

1 Like

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

https://openhab-scripters.github.io/openhab-helper-libraries/Guides/Logging.html#logaction

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

https://openhab-scripters.github.io/openhab-helper-libraries/Guides/Logging.html#logaction

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

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

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!
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!

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

What do you think: Will this work with an operator as well?

No. To support something like that you need to use a condition (i.e. the But only if…).

Of course, the answer to your questions could also have been discovered if you had tried to run it.

As has always been the case, you can’t do math or comparisons in your rule triggers.

1 Like