Hello World logInfo example rule error

Hi Gang. I’m new to openhab, but have been an embedded C programmer for eons. I was able to pretty quickly configure an initial set of bindings and things, and get some relevant items and displayed on a page, but am really struggling to get my first rule working. I’ve spent many hours reading the documentation, searching this forum, and Googling the interwebs, but am not finding what I need to get through the starting gate. I’ve tried to dumb down a really simple test rule to spit out a message to the log file, but am getting an error message. Not understanding which rock to look under.
Platform information:

tom-e-toes@openhabian:~$ cat /var/lib/openhab/etc/version.properties
openHAB Distribution Version Information
----------------------------------------
build-no        : Release Build
online-repo     : https://openhab.jfrog.io/openhab/libs-release

Repository        Version
----------------------------------------
openhab-distro  : 3.2.0
openhab-core    : 3.2.0
openhab-addons  : 3.2.0
karaf           : 4.3.4

tom-e-toes@openhabian:~$ hostnamectl | grep -v ID
   Static hostname: openhabian
         Icon name: computer-desktop
           Chassis: desktop
  Operating System: Ubuntu 20.04.3 LTS
            Kernel: Linux 5.11.0-44-generic
      Architecture: x86-64

tom-e-toes@openhabian:~$ java -version
openjdk version "11.0.13" 2021-10-19 LTS
OpenJDK Runtime Environment Zulu11.52+13-CA (build 11.0.13+8-LTS)
OpenJDK Server VM Zulu11.52+13-CA (build 11.0.13+8-LTS, mixed mode)

My rule looks like this:

The rule defines a one line DSL script:

logInfo(“test”, “message”)

Resulting in the following YAML:

configuration: {}
triggers:
  - id: "1"
    configuration:
      cronExpression: 0 * * * * ? *
    type: timer.GenericCronTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: logInfo(“test”, “message”)
    type: script.ScriptAction

I can see that the rule is running once per minute, as intended, since I see the following error in the log file, but I’m not understanding what I’m missing. Increasing the log level to DEBUG doesn’t yield any additional information:

[ERROR] [.internal.handler.ScriptActionHandler] - Script execution of rule with UID 'dbace84532' failed: logInfo(“test”, “message”)

Could some kind soul give me a nudge in the right direction so I can figure out where to look to find the answer?
Thanks a bunch.

Everything looks right on a quick look. It’s not complaining that logInfo doesn’t exist, it just says failed which is unhelpful.

What do you get if you use one of the other languages? Blockly would be a good one to try.

Since you are a programmer and just getting started, it might be worth while using JS Scripting. It needs to be installed but is looking like it might become the default at some point.

Then you would choose “ECMAScript 11” from the list of languages. Logging would be either:

console.log("message");

or

var logger = log("test");
logger.info("message");

What I mainly want to see is if this is a UI rule problem or specific to Rules DSL.

Another thing you can try is to see if it has a problem with a text file based rule. Put the file test.rules in $OH_CONF/rules with the contents:

rule "Test"
when
    Time cron 0 * * * * ? *
then
    logInfo("test", "message")
end

Thanks Rich. Running a Blockly script worked as expected. A JS Script also worked as expected, although it did generate the following warning (once) in the log file:

[WARN ] [.internal.OpenhabGraalJSScriptEngine] - Failed to retrieve script script dependency listener from engine bindings. Script dependency tracking will be disabled.

I also placed a text based rules file (unique rule name) in the the $OH_CONF/rules folder, as suggest, and restarted the OH service to pick up the change, but am not seeing any output in the log file. No errors either. Am I missing some add-on needed for the DSL scripts?

That’s not an unusual warning and is benign and can be ignored for now.

OH will see the file and load it automatically, no need to restart anything. You should see a line something along the lines “loaded model nameOfFile.rules” as soon as you save the file. There is a file system watcher.

Assuming you put the file in the right folder, which I have no reason to doubt, there does seem to be something off with Rules DSL. I’m not sure what it could be though. There is nothing to configure or install to make it work. Rules DSL should just be there.

If you log into the karaf console (from the machine running openHAB run openhab-cli console if it’s installed or use ssh -p 8101 openhab@localhost using "habopen as the password. Then run list | grep -i rule. Are all the bundles shown as “Active”?

If not or as a last resort you can try to Clear the Cache and see if that makes a difference.

This looks like the ‘wrong kind of quotemarks’. It’s especially easy to do with copy-paste.

"use the symmetrical type"
“not the bookends type”
1 Like

Thanks rossko57. That was exactly the problem with my original rule. I had copied the original logInfo() function from a different topic in the forum, bookend quotes and all, and just modified the text. Typing it in from scratch resolved the problem. No error now, and the expected text is showing up in the log file.

1 Like

Hi Rich. Thanks for the tips. My original problem has now been resolved (see below), but as long as I’m at it I’d like to discover why the file based rule isn’t running.
These are the active rules bundles:

openhab> list | grep -i rule
160 x Active x  80 x 3.2.0                 x openHAB Core :: Bundles :: Automation Script RuleSupport
197 x Active x  80 x 3.2.0                 x openHAB Core :: Bundles :: Model Rules
198 x Active x  80 x 3.2.0                 x openHAB Core :: Bundles :: Model Rule IDE
199 x Active x  80 x 3.2.0                 x openHAB Core :: Bundles :: Model Rules Runtime

Is it just a file permission issue? I copied the file over from a different machine, but needed to ‘sudo’ to wedge it in there (probably a sign that I wasn’t doing something correctly)

tom-e-toes@openhabian:/etc/openhab/rules$ ls -l $OPENHAB_CONF/rules/
total 8
-rw-rw-r-- 1 openhab openhab 223 Dec 20 00:21 readme.txt
-rwxr--r-- 1 root    root    107 Jan 12 17:15 ruleTest.rules

I was unable to Clear the Cache - the service doesn’t stop when I issue the openhab-cli stop command. Do I need to be logged into the machine as ‘openhab’ user? (I’m using ssh via putty as a different user to access the OH machine)

Look in your openhab.log shortly after editing, if the rules file is found it will tell you.
Your folder location looks sensible, and the pre-existing readme.txt is encouraging in that respect - have you read it see if it actually relates to rules?

No but you do have to be root to control services. Use sudo to run openhab-cli.

The .rules fine is owned by root but had world read peissions so that should be ok. I think openhab-cli had a fix permissions option (again run it as root) which will change the ownership and make sure it’s readable, but based on what I’m seeing that shouldn’t be the problem.

Now that $OH_CONF on an installed to Linux machine is /etc/openhab so you were already in the rules folder when you read the ls.

This is a mystery. Any time that. rules duke is touched OH should try to load it. But I do remember vaguely that oh for smart and won’t reload it unless the contents change so maybe moving the file to a different name that doesn’t end in .rules and then moving it back while watching the logs would be the best way to force a reload.

Success! OH wanted the owner of the file to be ‘openhab’, and the group to be ‘openhab’ Once I changed those sudo chown openhab:openhab $OPENHAB_CONF/rules/ruleTest.rules, OH automagically picked up the file, as expected.

tom-e-toes@openhabian:~$ ls -l $OPENHAB_CONF/rules/
total 8
-rw-rw-r-- 1 openhab openhab 223 Dec 20 00:21 readme.txt
-rwxr--r-- 1 openhab openhab 110 Jan 13 10:47 ruleTest.rules

The only other issue I had was with a minor syntax issue in the script (missing quotes around the function arguments). The log following log entry was very helpful in that regard:

[WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'ruleTest.rules' has errors, therefore ignoring it: [3,15]: mismatched input '0' expecting RULE_STRING

So this was the final file-based rules script:

rule "Test"
when
    Time cron "0 * * * * ? *"
then
    logInfo("test","file defined rule message")
end

Thank you both for walking me through this. Looking forward to finding my sea legs and being able to contribute back to the forum.
PS. rossko57, I appreciate your Dalek icon - nice touch :slight_smile: