[Partly solved] Rules are not triggered as expected

I installed yesterdays nightly (#413) manually using a script (not apt -get!)
When starting the OpenHAB service initially my rule which is based on an Item state change(network item) is triggered as expected. The rule which is based on a cron-statement and System started is NOT performed after the start (IMHO which it should be), however the cron-events of the rule are performed.

If I do stop and then restart the service (either by using those commands or by rebooting) both type of events are not triggered ( Item state change AND System started).

A Trigger with an AND is not possible, because both events must occur at the same femto second. In the when clause only a or is usable.

Thomas

To elaborate on @Dibbler42ā€™s answer, a rule is triggered be an event. Any further logic you need to do needs to be performed within the rule in an if statement. An an event is discreet and has no duration. For how long after the System started should the Item state changed be considered enough to trigger the rule?

In this case the event is ā€œSystem startedā€. In the rule you would have:

if(item.state == "something") {
    // do stuff
}

Because no two events will ever happen at the exact same time, if you truly need to do something in case of both of these events, you need to do some fancy book keeping.

  • create a timer for both Items
  • create a rule for each trigger on the events
  • check whether either of the timers is active in the rule and if it is you know that the two events occurred close together in time.

val Timer startedTimer = null
val Timer itemTimer = null

rule "System started, check for Item changed"
when
    System started
then
    if(itemTimer != null) {
        // the Item changed state close to the system starting logic goes here
    }
    else {
        startedTimer = createTimer(now.plusSeconds(1), [|    startedTimer = null ])
    }
end

rule "MyItem changed state"
when
    Item MyItem changed
then
    if(startedTimer != null){
        // MyItem changed state close to System started logic goes here
    }
    else {
        itemTimer = createTimer(now.plusSeconds(1), [| itemTimer = null])
    }
end

However, WHY do you want this? There might be a more appropriate way to achieve what you are after.

1 Like

Stupid me, I used the ā€œandā€ in the sentence, however the trigger is naturally set up with ā€œorā€. Both rules where working before this update.
My question is, why are those rules not working anymore as expected?

Thanks for the help!

It is always easiest for others to reproduce your problem if you can show it on the demo setup. The demo comes with a demo.rules file, which contains cron as well as startup rules. Imho they are all working. So what needs to be changed about them to make your problem appear?

I made a clean install of the todays nigthly (#417 / clean as in deleting the whole directory that contained openhab2) and started with the demo setup. When adding the following rule to the running OpenHAB:

import org.openhab.core.library.types.*
val String Tankstelle1_ID=ā€œxxxxxā€
val String API_Key=ā€œxxxxā€
var String Status
var String TankstellenMarke
var String TankstellenStrasse
var String TankstellenOrt
rule ā€œTankstellendatenā€
when
Time cron ā€œ50 3 0 * * ?ā€ or System started
then
logInfo (ā€œTankstellendatenā€, ā€œStartā€)
var String URL= ā€œhttps://creativecommons.tankerkoenig.de/json/detail.php?id=ā€ + Tankstelle1_ID + ā€œ&apikey=ā€ + API_Key
//logInfo (ā€œTankstellendatenā€, ā€œURL = {}ā€, URL)
var String json = sendHttpGetRequest(URL)
//logInfo(ā€œTankstellendatenā€,ā€œJSON-String = {}ā€, json)
if (json!=null) {
var String OK = transform(ā€œJSONPATHā€, ā€œ$.okā€, json)
//logInfo(ā€œTankstellendatenā€, ā€œOK = {}ā€, OK)
if (OK==ā€œtrueā€) {
TankstellenMarke = transform(ā€œJSONPATHā€, ā€œ$.station.brandā€, json)
TankstellenStrasse = transform(ā€œJSONPATHā€, ā€œ$.station.streetā€, json)
TankstellenOrt = transform(ā€œJSONPATHā€, ā€œ$.station.placeā€, json)
Name_1.postUpdate(TankstellenMarke + " " +TankstellenStrasse + " " + TankstellenOrt)
}
else {
var String HttpError = transform(ā€œJSONPATHā€, ā€œ$.messageā€, json)
logInfo(ā€œTankstellendatenā€, ā€œHttpError = {}ā€, HttpError)
}
}
else {
logInfo(ā€œTankstellendatenā€, ā€œEmpty Return from HTTPRequestā€)
}
end

The rule was triggered immidiatly in this demo setup (verified by log-entry and output to Item Name_1).
After that I changed the setup to standard using this addons.cfg file:

package = standard
binding = sonos, network, intertechno, exec, http
ui = basic, classic, paper
persistence = rrd4j, mysql
transformation = map, jsonpath, exec, regex
misc = myopenhab

After starting OpenHAB in this setup the above rule was NOT performed immidiatly, only when changing the cron-statement to the actual time it was performed.

Didnā€™t have time to do the check on for the Item-Changed rule. That has to wait until tomorrow.

Checked for the Rule which is triggered on using ā€œItem Juergen_WLAN changed to ONā€. Used the Demo setup, with all of my Items, Sitmap and rules removed initially. When adding the above triggered rule, it is triggered. However, if changing to the standard setup it is NOT triggered.

Using a ā€œlog:set DEBUG org.eclipse.smarthome.modelā€ and saving a change to the rule with ā€œcron xxx or System startedā€ I get those log-entries:

19:55:25.946 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model ā€˜price.rulesā€™
19:55:26.209 [DEBUG] [e.internal.engine.RuleTriggerManager] - Removed scheduled cron job ā€˜price.rules#Tankstellendaten#50 3 0 * * ?ā€™
19:55:26.223 [DEBUG] [e.internal.engine.RuleTriggerManager] - Scheduled rule Tankstellendaten with cron expression 50 3 0 * * ?
19:55:31.225 [DEBUG] [ntime.internal.engine.RuleEngineImpl] - Executing startup rule ā€˜Tankstellendatenā€™

The last entry tells it is executing the startup rule, however the rule is not performed (expected LogInfo isnā€™t logged).

ā€¦and now Iā€™m completly lost!
I left the system untouched since the test yesterday evening and looked at the log this morning.
Now I see the ā€œItem changedā€ triggered rule being done. Starting with the Error " script interpreter couldnā€™t be obtain", I thought that one was resolved.

Iā€™m still having this problem!:rage:
I hate those problems that seem to come and go. Iā€™ve been writing enough code to know that is a stupid description, but thatā€™s what it looks like to me.

All I can figure out is that only those rules do get their starting event when the event is scheduled by a cron-statement.
It doesnā€™t matter if the cron is paired with another statement (paired by ā€œorā€!) or it is alone, the cron works.
All others like ā€œSystem startedā€ or any ā€œItem Changedā€ do not. The one raised by ā€œSystem startedā€ reports itself ā€œstartedā€ when using:

ā€œlog:set DEBUG org.eclipse.smarthome.modelā€

However the code is NOT performed!
Actualy Iā€™m testing it with a ā€œTest-Ruleā€ like this:

rule ā€œTesterā€
when
Time cron ā€œ0 2,7,12,17,22,27,32,37,42,47,52,57 * * * ?ā€ or Item TestSomething changed to ON
then
logInfo(ā€œTesterā€,ā€œTestSomesting = {}ā€, TestSomething)
end

The rule does run every 5 minutes as planned, however a try to run it by switching the swich ā€œTestSomethingā€ isnā€™t working! The log looks like :slight_smile:

21:42:00.011 [DEBUG] [ntime.internal.engine.ExecuteRuleJob] - Executing scheduled rule ā€˜Testerā€™
21:42:00.028 [INFO ] [clipse.smarthome.model.script.Tester] - TestSomesting = TestSomething (Type=SwitchItem, State=OFF, Label=Test, Category=null, Groups=[EG_Wohnzimmer])

If anybody who is working on a nigthly #413 or later has the rules working correctly, could (s)he post the bundle:list output. Maybe I still have something stupid in my setup although I did a complete reinstall.
Thanks!

Found the problem ( at least I think so).
I had some entries of an apt -get installation remains on the system. After removing that the Item changed rules are again.
The remaining problem is the rule which should react on system started. It had been that such a rule triggered whenever the rule-file got an update. This isnā€™t working!
Since I get the above mentioned log-entry that the rules supposed to be started, I believe that this is not an expected behavior.
The rule itself has no errors, the Cron trigger in the same rule works!
[edit]
The problem came back after a day??
Iā€™ll make a complete reinstall of the OS after my vacation. Will report back!

I today fixed a major bug that caused rules to not execute correctly with a kind of random behavior. I would hope that many of those problem reports are solved with it - the fix is part of the latest build #469 already. So please be so kind and test this!

For me #464 AND the correct usage of Linked mode together with an .items file solved the problems.
Nevertheless I will test it as soon as Iā€™m back home at the end of the week.
Thanks for the effort.

Installed #469. No problems observed regarding my rules.