i want to set the Item vFreigabe to ON while the current from a meassurement is above 100 mA. Therefore i want to use a while-loop.
rule "Freigabe"
when
Item Schaltaktor_2_Switch changed from OFF to ON
then
var Aktor_Current = Schaltaktor_2_Current.state as Number
Thread::sleep(5000)
while (Aktor_Current > 100){
Thread::sleep(5000)
sendCommand(vFreigabe, ON)
}
sendCommand(vFreigabe, OFF)
end
Ouch! Don’t do it that way, and have a potentially never ending loop in a rule (locking out other rules)
Do it the event driven way. When the current measurement changes then do the comparison and set your other item.
When the current doesn’t change, there is nothing to do of course.
If it is supposed to be controlled by that switch, then use the switch state in an if statement within your rule.
Luckily you provided enough detail to provide the proper solution which rossko57 outlines. The rule would look something like:
Rules DSL:
rule "Freigabe"
when
Item Schaltaktor_2_Current changed
then
if(Schaltaktor_2_Switch.state != ON) return;
var newState = OFF
if(Schaltaktor_2_Current.state > 100) newState = ON
if(vFreigabe.state != newState) vFreigabe.sendCommand(newState)
end
JSR223 Python using the Helper Libraries:
from core.rules import rule
from core.triggers import when
from core.utils import sendCommandCheckFirst
@rule("Freigabe", description="Sets vFreigabe to ON while Schaltaktor_2_Current > 100")
@when("Item Schaltaktor_2_Current changed")
def freigabe(event):
if items["Schaltaktor_2_Switch"] != ON: return
sendCommandCheckFirst("vFreigabe", ON if items["Schaltaktor_2_Current"] > DecimalType(100) else OFF)
I want to check if a pump is running on idle (not enough water in the tank, about 80mA) or on load (~150mA). While the pump is running it might happen that the waterlevel gets too low (and the pump has to stop). I want to use the information if the waterlevel is high enough in more than one rule, so i want to builld a variable with this information in it.
So if the current is above 100mA after 5 seconds (that means vFreigabe is ON) the rule may start, if it falls below 100mA (vFreigabe changes to OFF) while running, the rules have to stop.
I probably should use a level sensor but i want to try it that way first.
The above Rule will set vFreigabe to ON whenever the Current > 100. If you need to wait for five seconds of over 100 than you should use a Timer. Schedule it for five seconds from now and then check inside the Timer’s lambda if it is still over 100. If the Current falls below 100 cancel the Timer (if it’s still running) and turn off vFreigabe.