I would like to count if/how many windows are open at the moment.
This should be the base for a reminder when I leave the house and there is still an open window.
Unfortunately the rule doesn’t count reliable
I often get an alarm even when all windows are closed.
// RULE
var Number counterFensteregoffen = 0
// Erdgeschoss
rule "Fenster EG offen Counter ++"
when
Item eg_bd_k_fenster changed to OPEN or
Item eg_wzost_k_fenster changed to OPEN or
Item eg_az_k_fenster changed to OPEN or
Item eg_ku_k_fenster changed to OPEN or
Item eg_ku_k_tuer changed to OPEN
then
logInfo("RULE", "--> Fenster EG offen Counter +")
counterFensteregoffen = counterFensteregoffen + 1
postUpdate(vFensterOffenEG, ON)
end
rule "Fenster EG offen Counter --"
when
Item eg_bd_k_fenster changed to CLOSED or
Item eg_wzost_k_fenster changed to CLOSED or
Item eg_az_k_fenster changed to CLOSED or
Item eg_ku_k_fenster changed to CLOSED or
Item eg_ku_k_tuer changed to CLOSED
then
logInfo("RULE", "--> Fenster EG offen Counter -")
counterFensteregoffen = counterFensteregoffen - 1
if (counterFensteregoffen == 0) {
logInfo("RULE", "--> Fenster EG offen Counter = 0 & vFensterOffenEG = OFF")
postUpdate(vFensterOffenEG, OFF)
}
end
and make your windows member of that group. Then try something like:
rule "fenster counter"
when
Item Fenster_EG received update
then
val Number counterFensteregoffen = Fenster_EG.members.filter[ NumberItem i | i.state==OPEN ].size
if ((counterFensteregoffen == 0) && (vFensterOffenEG.state != OFF)) vFensterOffenEG.postUpdate(OFF)
if ((counterFensteregoffen > 0) && (vFensterOffenEG.state != ON)) vFensterOffenEG.postUpdate(ON)
end
Try to create the group like in my example above, hopefully that helps. If not, please post some more detailed (error) logging and your item configurations
I believe that something like Group:SUM will add up the items.
I am on my phone so I have limited access to docs but It is explained in the items docs.
in sitemap, you should get always the correct information.
If you explicitly want to count them in a rule…
rule "count openwindows"
when
Member of MyWindows changed
then
val Number openWindows = MyWindows.members.filter[w|w.state == OPEN].size
val Number unknownWindows = MyWindows.members.filter[w|w.state == NULL].size
val Number closedWindows = MyWindows.members.filter[w|w.state == CLOSED].size
logInfo("window","state for Windows: open: {} closed: {} unknown: {}",openWindows,closedWindows,unknownWindows)
end
One more thing … how can I use “openWindows” in another rule?
rule "Pushover Alarm Fenster EG offen NEU"
when
Item vAusserHaus changed to ON
then
if(openWindows>=1) {
logInfo("RULE", "--> P2 außer Haus und Fenster EG offen / sende Pushover")
sendPushoverMessage(pushoverBuilder("ACHTUNG: Fenster im EG sind noch offen").withPriority(1).withSound("tugboat"))
}
end
I think he means he wants to use the calculated value in another rule.
One way is to create a ‘global variable’ by defining it outside of any rules, at the beginning of the file. That will be available to any rule within the same rules file.
You’ll want to define it as a var not a val, as you’ll want to update its value sometimes.
General idea;
var myGlobal = 0
rule "set it"
when
Item something changed
then
myGlobal = some calculation
end
rule "use it"
when
...
var sometihngelse = myGlobal + 10
...
end
If you need to share it across different rules files, the simplest thing is to use an Item, postUpdate it and read .state
It counts correctly but doesn’t send a message.
According to the log the variable “vAusserHaus” works …
var Number openWindowsEG = 0
var Number unknownWindowsEG = 0
var Number closedWindowsEG = 0
rule "count openwindows"
when
Member of Gfenstereg changed
then
openWindowsEG = Gfenstereg.members.filter[w|w.state == OPEN].size
unknownWindowsEG = Gfenstereg.members.filter[w|w.state == NULL].size
closedWindowsEG = Gfenstereg.members.filter[w|w.state == CLOSED].size
logInfo("window","Fenster EG Status: open: {} closed: {} unknown: {}",openWindowsEG,closedWindowsEG,unknownWindowsEG)
end
rule "openwindows Alarm bei Verlassen des Hauses"
when
Item vAusserHaus changed to ON
then
if (closedWindowsEG>=1) {
sendPushoverMessage(pushoverBuilder("ACHTUNG: Fenster im EG sind noch offen").withPriority(1).withSound("tugboat"))
}
end
Perhaps its the pushover that isn’t working. Add logInfo to debug your rule.
rule "openwindows Alarm bei Verlassen des Hauses"
when
Item vAusserHaus changed to ON
then
logInfo("testing", "triggered with " + closedWindowsEG.toString)
if (closedWindowsEG>=1) {
logInfo("testing", "entered IF")
sendPushoverMessage(pushoverBuilder("ACHTUNG: Fenster im EG sind noch offen").withPriority(1).withSound("tugboat"))
logInfo("testing", "tried pushover")
}
end