Are there differences in textual rule design between OH1 and OH2? I found a few websites which all referred to OH1 and I got the impression, the syntax differed slightly.
I neither could find any useful (text or video) tutorials regarding ruling (Habmin-GUI or textual) nor an overview regarding the syntax and commands.
Any tips are appreciated. I’d prefer German websites, videos or tutorials, English works just as fine.
My main problem:
I need some rules that say e.g.:
IFtime is after 5 amANDtime is before 10 amANDrollershuter sleeping room receives command UPTHENsend command UP to all other rollershutters
IFfrontdoor opens_ ANDany.window is openTHENturn hallway light to red
IFfire detecetedANDnobody is home THEN _open all rollershutters ANDswitch on all lightsELSE _open all rollershutters ANDswitch on all lightsANDsend notification “fire alert”
I’ve tried both, text and habmin gui and failed so far.
What I found out is that multiple WHEN-conditions linked with AND won’t work.
The approach you need to take is that e.g. WHEN an item changes state THEN if this and this etc. So you have one trigger and when that triggers you check for other conditions to be met.
So in my first example the WHEN-part is the rollershutter and i need to check the time in the IF-part.
How do I check for a given time of day in the IF-part?
I have to admit i’m fairly new to rule-scripting.
Only a few minor changes. You no longer need to import anything from org.joda and org.openhab. See the The Migration Tutorial for a list of everything that is different.
Rule triggers are event based, not state based. No two events will occur at exactly the same time so and is not supported in triggers.
What you want is to have a triggering event in the when and if/else in the body of the rule to test for state.
One thing you might find useful in a lot of these sorts of situations is to separated it the calibration of state into their own rules and store the state in an item. See the [Time of Day design pattern] ([Deprecated] Design Pattern: Time Of Day) for a good illustration of this using time.
rule "Rollershutter sleeping room"
when
Item rollershutter_sleeping received command UP
then
if ((now.getHourofDay > 5) && (now.getHourofDay < 10)) {
rollershutter_sleeping.sendCommand(UP)
}
end
rule "Alert if Windows are open"
when
Item Frondoor changed to OPEN
then
if(gWindows.state == OPEN) {
hallwaylight.sendCommand(RED)
}
end
rule "Fire-Detection"
when
Item Fire-Alert received command
then
rollershutters.sendCommand(UP)
lights.sendCommand(ON)
if (Presence.state == ON) {
sendNotification
}
end
This code snippets are written out of brain and are not functional. They should give you only an idea how rules are built.