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