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