I have a rule which is triggered by one event (A), then check if the other (B) is on, do something.
This works as long B is on before A.
Today it was the other way around and the rule did not trigger.
How can this be fixed?
For those wanting to see my rule:
rule "Allow hot water system heating for 2h after SOC reached 100 AND Irrigation tank is full"
when
Item Hub_BorePump_Demand changed to OFF
then
if ((spp_SOC100RanOnce.state == ON) && (Shed_HWS_RanOnce.state == OFF) && (Shed_HWS_Allowed.state == ON)) {
if (Shed_RC41_HotWaterSystem.state != ON) {
Shed_RC41_HotWaterSystem.sendCommand(ON)
logInfo("Shed_HWS1.rule", "Shed_RC41_HotWaterSystem = ON")
Shed_HWS_RanOnce.postUpdate(ON)
// create timer and associated action in 2h
timerShed_RC41_HotWaterSystem = createTimer(now.plusMinutes(120)) [|
Shed_RC41_HotWaterSystem.sendCommand(OFF)
logInfo("Shed_HWS2.rule", "Shed_RC41_HotWaterSystem = OFF")
timerShed_RC41_HotWaterSystem = null
]
}
}
end
So in this case the battery wasn’t full yet, but the Pump had finished.
Not sure I understand your use case, but if I understand your question, you rule is triggered by event_A and then checks some conditions, among which a status_B and if all is as it should be, the rule does something. Which leads to the behavior you observed: only a status change in A will trigger the rule, and B, better be ready right then and there or nothing will happen.
Would it work for your use case if you added some of your conditions as additional rule triggers? In other words, to have a rule trigger for all conditions?
rule " great name of a rule"
when
Item A received command or
Item B received command or
Item C received command
then
if (A.state == condition1 && B.state == condition2 && C.state == condition3) {
//do something
}
end
This way, every time one of the items that are a condition for your action received any command, the rule triggers and checks the state of all conditions; if everything is as it should be, it would do something.
From the above, it does not seem that way, but be careful when you use group events as a trigger (the rule will fire once for each member of the group when the group.state changes), in this case, I would suggest to use reentrant locks (there are good threats on this). This consideration would also apply to deal with other events being fired while the rue still runs. Depending on your use case, it may be tricky to deal with it, on the other hand, it may be such an exceedingly rare occasions that two or more of the triggering state changes happen at the same millisecond that you may not need to worry about it.