OH3 normal rules trigger but script based rules do not trigger at all


I tried to solve this by reading through the other trigger topics but I can’t find an answer that works for me.
I’m new to OH3 and I probably make a dumb mistake.

The following rule should be triggered everry second but it does not do anything.

rule "TestTime"
    Time cron "* * * ? * *"
    logInfo("___","rule Test Time")

I don’t see the MQTT message (note the item works fine manually) and I also don’t see any log entry. I only see a “Rule TestTime has been updated” log entry when I save the rule.

If I create a rule with the simple rule engine it works fine and it is triggered every second and I also see the item related log entries and the MQTT messages.

configuration: {}
  - id: "1"
    label: Test Timer
      cronExpression: "* * * ? *  *"
    type: timer.GenericCronTrigger
conditions: []
  - inputs: {}
    id: "3"
      itemName: Generic_MQTT_Thing_BashTest
      command: ON
    type: core.ItemCommandAction

Both rules have a green “IDLE” sign next to it.
But why is the second “simple” rule working as expected and the first script based rule is not working at all?

I found in another discussion that Java could be a problem but the installed version sould be fine:
openjdk version “11.0.17” 2022-10-18
OpenJDK Runtime Environment (build 11.0.17+8-post-Ubuntu-1ubuntu220.04)
OpenJDK 64-Bit Server VM (build 11.0.17+8-post-Ubuntu-1ubuntu220.04, mixed mode, sharing)

What do I do wrong?

I would be grateful for any hints…

here is more technical info
version: 3.4.0
buildString: Release Build
locale: default
configFolder: /etc/openhab
userdataFolder: /var/lib/openhab
logFolder: /var/log/openhab
javaVersion: 11.0.17
javaVendor: Ubuntu
osName: Linux
osVersion: 5.15.0-56-generic
osArchitecture: amd64
availableProcessors: 2
freeMemory: 27495736
totalMemory: 205520896
startLevel: 100

  • mqtt
  • ntp
  • openweathermap

I couldn’t say why one works and another doesn’t but I do know that Quartz is picky. Using a Quartz cron expression builder as well as the builder built into MainUI I get * * * * * ? * for every second. The primary difference is the field where “?” appears and that there is one extra field, though I think that last one is generally optional.

As an aside, if you are looking to do more than just test stuff that’s one thing. But OH is an event based system. There is almost always a better way than polling.

thank you very much for your suggestion, but it didn’t change the situation and the script based rule still isn’t triggered.

I came up with this simple time based rule for tests since my other rule that should be triggered by openweathermap also wasn’t triggered…do you have a suggestion for an event triggered rule that should work? (maybe based on an generic MQTT device) I would appreciate any ideas that would help me. Maybe also a hint where error messages should be. There are none in the normal log.

The fact that openHAB is so hard to use (with no error message for me to work with) is a bit sad. I am preparing a school IoT project with esp32 development boards using MQTT to exchange sensor values. I thought it would make everything more interesting if I included a smart home platform. Just to show how easy it is to incorporate these actuators and sensors into a standard smart home platform and derive intelligent decisions based on additional information, such as external weather data …

I’m actually a bit surprised that the MQTT stuff is all solvable and understandable (even in openHAB after some learning) and it seems that I fail to use openHAB rules - which I thought would be the easy part of this project.

That’s two separate issues. I couldn’t tell you why the OWM rule didn’t trigger. You don’t say what the trigger of this rule is. I would guess it would be based on an Item changed event. If the rule didn’t trigger, it’s almost certainly because the Item didn’t change.

For testing it’s usually best to run the rules manually from MainUI or add a Switch Item to trigger it so you have more control over when it runs.

All rules are triggered by some event. Any rule defined to trigger based on an event that actually occurs should work.

All errors get logged to openhab.log.

It’s hard to say where things are going wrong. You’ve not posted any logs from openhab.log to indicate whether or not the file based rule was even loaded.

I didn’t post this log because there isn’t any error in the log. So my guess was, that this is not helpful. But maybe you can see more that I can:

2023-01-15 04:43:19.773 [INFO ] [.core.internal.i18n.I18nProviderImpl] - Time zone set to 'Europe/Berlin'.
2023-01-15 04:43:19.951 [INFO ] [.core.internal.i18n.I18nProviderImpl] - Location set to xxxx,xxxx'.
2023-01-15 04:43:19.952 [INFO ] [.core.internal.i18n.I18nProviderImpl] - Locale set to 'de'.
2023-01-15 04:43:19.953 [INFO ] [.core.internal.i18n.I18nProviderImpl] - Measurement system set to 'SI'.
2023-01-15 04:43:45.004 [INFO ] [.core.model.lsp.internal.ModelServer] - Started Language Server Protocol (LSP) service on port 5007
2023-01-15 04:43:48.330 [INFO ] [ab.ui.habpanel.internal.HABPanelTile] - Started HABPanel at /habpanel
2023-01-15 04:43:49.866 [INFO ] [.transport.mqtt.MqttBrokerConnection] - Starting MQTT broker connection to 'localhost' with clientid Maler
2023-01-15 04:43:50.804 [INFO ] [e.automation.internal.RuleEngineImpl] - Rule engine started.

I thought a time based event is the simplest possible event, since I was not able to make the rules work based on other events. I thought that I made an error in writing the rule. But it seems that at least the cron based rule seems to be formulated correctly, but still isn’t working.
I’ll try to write a rule based on the MQTT generic device since this is the device I currently understand best.

I now enforced the execution of the time based rule by “run now” and this error message occured in the openhab.log
Is this helpful?

2023-01-15 05:35:09.223 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'TestTime' failed:  ___ rule  ___ "TestTime"
    Time cron "* * * * *  ___ ? *"
    logInfo("___","rule Test Time")
   1. The method or field rule is undefined; line 1, column 0, length 4
   2. The method or field when is undefined; line 2, column 16, length 4
   3. The method or field cron is undefined; line 3, column 30, length 4
   4. The method or field then is undefined; line 4, column 51, length 4
   5. The method or field end is undefined; line 7, column 141, length 3
   6. This expression is not allowed in this context, since it doesn't cause any side effects.; line 1, column 5, length 10
   7. This expression is not allowed in this context, since it doesn't cause any side effects.; line 3, column 25, length 4
   8. This expression is not allowed in this context, since it doesn't cause any side effects.; line 3, column 35, length 15

since everything seems to be undefined…

I used this script language option for creating the rule:

  • Rule DSL v1

Is this not the correct one?

I noticed that in the log three underlines are included at several arbitrary places e.g.:
"___ rule ___ "

Is this normal?

I can see that the file was never loaded. You should get a “Model loaded …” for every config file loaded from a text config file, including rules.

So the question is, what’s the name is the file that rule is in? Where is that file? What are the permissions of the file?

It just further points to either the file being in the wrong folder or had the wrong extension. The original rule is using Rules DSL syntax so it must be in a file that ends in “.rules” and is placed in the /etc/openHABian/rules folder.

Or you’ve put a whole file based rule definition as the action to a UI rule. In the UI, all that you put into the script action is the stuff between “then” and “end”. The rest of the rule is defined through the UI.

I’m starting to think this is what you’ve done. Get rid of everything up to and including “then” in your script action and remove the “end” as well. The trigger, name, and description are all defined through the UI.

It might help to review/review the rules pages in the Getting Started Tutorial.

Thank you very much for your time and help. I think I understand now. At least I was able to find a solution that works for my demonstration project.

As a short feedback to the developers:
It is a bit misleading for a new user that one can create a “rule DSL” based script in the UI that gives a green “Idle” feedback, when in fact it isn’t working at all because the syntax is only working for file based rules. It is additionally a bit difficult to understand that problem when the only error message related to that problem is generated into the openhab log only after one forces the execution by using the “run now” button.

So, again, thank you very much for helping me to solve this problem!