OK,
I did some tests, and it seems sometimes is working, sometimes not.
The issue seems this part:
p.lastUpdate.isAfter(now.minusMillis(5500))
when it works:
2019-01-31 21:13:04.424 [INFO ] [pse.smarthome.model.script.variables] - state=ON: [Cate_Phone_Home_Unifi_online (Type=SwitchItem, State=ON, Label=Cate, Category=null, Groups=[gPeople, gPresence, gPhones]), Andrea_Phone_Home_Unifi_online (Type=SwitchItem, State=ON, Label=Andrea, Category=null, Groups=[gPeople, gPresence, gPhones])]
2019-01-31 21:13:04.427 [INFO ] [pse.smarthome.model.script.variables] - LastUpdate: [Cate_Phone_Home_Unifi_online (Type=SwitchItem, State=ON, Label=Cate, Category=null, Groups=[gPeople, gPresence, gPhones]), Andrea_Phone_Home_Unifi_online (Type=SwitchItem, State=ON, Label=Andrea, Category=null, Groups=[gPeople, gPresence, gPhones])]
2019-01-31 21:13:04.433 [INFO ] [pse.smarthome.model.script.variables] - isAfter: [Andrea_Phone_Home_Unifi_online (Type=SwitchItem, State=ON, Label=Andrea, Category=null, Groups=[gPeople, gPresence, gPhones])]
2019-01-31 21:13:04.448 [INFO ] [smarthome.model.script.check message] - message is: Benvenuto a casa, Andrea
when it’s not working (added also the map part):
2019-01-31 21:23:49.229 [INFO ] [pse.smarthome.model.script.variables] - state=ON: [Cate, Andrea]
2019-01-31 21:23:49.245 [INFO ] [pse.smarthome.model.script.variables] - LastUpdate: [Cate, Andrea]
2019-01-31 21:23:49.248 [INFO ] [pse.smarthome.model.script.variables] - isAfter: []
2019-01-31 21:23:49.256 [ERROR] [pse.smarthome.model.script.came home] - Unexpected number of phones: 0
Consider I’m using Andrea_Phone for test, Cate_Phone is always ON
Here the rule now (there was also a TTS sendCommand mistake):
rule "Someone came home"
when
Member of gPhones changed to ON
then
if(cameHomeTimer !== null) {
return; // do nothing if there is already a Timer
}
cameHomeTimer = createTimer(now.plusSeconds(5), [ |
val gPhones_state = gPhones.members.filter[ p | p.state == ON ].map[ name.replace("_Phone_Home_Unifi_online", "") ]
val gPhones_lastupdate = gPhones.members.filter[ p | p.lastUpdate !== null ].map[ name.replace("_Phone_Home_Unifi_online", "") ]
val gPhones_lastupdateisafter = gPhones.members.filter[ p | p.lastUpdate.isAfter(now.minusMillis(5500)) ].map[ name.replace("_Phone_Home_Unifi_online", "") ]
val cameHome = gPhones.members.filter[ p | p.state == ON && p.lastUpdate !== null && p.lastUpdate.isAfter(now.minusMillis(5500)) ].map[ name.replace("_Phone_Home_Unifi_online", "") ]
logInfo("variables", "state=ON: " + gPhones_state)
logInfo("variables", "LastUpdate: " + gPhones_lastupdate)
logInfo("variables", "isAfter: " + gPhones_lastupdateisafter)
var message = "Benvenuti a casa, "
switch(cameHome.size) {
case 0: {
logError("came home", "Unexpected number of phones: " + cameHome.size)
return;
}
case cameHome.size == 1 && cameHome.get(0) == "Andrea": message = "Benvenuto a casa, Andrea"
case cameHome.size == 1 && cameHome.get(0) == "Cate": message = "Benvenuta a casa, Caterina"
case cameHome.size == 1 && cameHome.get(0) == "Sofia": message = "Benvenuta a casa, Sofia"
case cameHome.size == 1 && cameHome.get(0) == "Edo": message = "Benvenuto a casa, Edoardo"
default: {
message = message + cameHome.members.reduce[ list, name | list + ", " + name ]
val ind = message.lastIndexOf(",")
message = new StringBuilder(str).replace(ind, ind+1," e").toString();
}
}
logInfo("check message", "message is: " + message)
Echo_Living_Room_TTS.sendCommand('<speak><break time="5s"/>' + message + '</speak>')
cameHomeTimer = null
])
end