Hi Folks,
I’ve the following situation:
An ESP32 detects BLE key trackers and publishes their MAC-adresses and RSSI via MQTT in JSON-String every 10 seconds like
{
"ac:23:3f:26:46:1c":-72,
"ff:ff:30:03:a9:93":-84,
"ff:ff:af:50:14:8f":-83
}
To figure out if a certain key tracker is present, a rule simple checks if the json-string contains its MAC-address and switches a personal presence proxy Item ON. Else OFF.
Occasionally, ESP32 misses to detect one or other tracker.
To avoid switching the proxy OFF an ON again, a timer should forces the rule waiting for 2min before switch the proxy OFF. But if the MAC-address appears again the timer should be canceled.
Two identical rules for two different trackers with this logic looks like this:
var Timer presenceTimerPerson1 = null
var Timer presenceTimerPerson2 = null
rule "Person1PresenceProxy from presenceString"
when
Item presenceString received update
then
// MAC address in JSON-string from ESP32, turn presence ON
if(presenceString.state.toString.contains("ff:ff:af:50:14:8f")){
if(Person1PresenceProxy.state == OFF){
Person1PresenceProxy.sendCommand(ON)
}
presenceTimerPerson1?.cancel
}
// MAC address not in JSON-string from ESP32, wait for 2min, check again, switch OFF
else{
presenceTimerPerson1 = createTimer(now.plusMinutes(2), [ |
if(!presenceString.state.toString.contains("ff:ff:af:50:14:8f")){
if(Person1PresenceProxy.state==ON){
Person1PresenceProxy.sendCommand(OFF)
}
}
presenceTimerPerson1 = null
])
}
end
rule "Person2PresenceProxy from presenceString"
when
//Time cron "0 0/1 * ? * * *" or
Item presenceString received update
then
// MAC address in JSON-string from ESP32, turn presence ON
if(presenceString.state.toString.contains("ff:ff:18:00:91:ff")){
if(Person2PresenceProxy.state == OFF){
Person2PresenceProxy.sendCommand(ON)
}
presenceTimerPerson2?.cancel
}
// MAC address not in JSON-string from ESP32, wait for 2min, check again, switch OFF
else{
presenceTimerPerson2 = createTimer(now.plusMinutes(2), [ |
if(!presenceString.state.toString.contains("ff:ff:18:00:91:ff")){
if(Person2PresenceProxy.state==ON){
Person2PresenceProxy.sendCommand(OFF)
}
}
presenceTimerPerson2 = null
])
}
end
The problem I have is, that it works as expected for Person2PresenceProxy but not for Person1PresenceProxy. It happens time to time, that it switches OFF an ON within less than 10 seconds. No matter if I let the timer wait for 2min or 5min.
It seems to me that the presenceTimerPerson1 does not cancel properly. But I’ve no idea why. And why not the other has the same problem, too.
There is no hint in the logs like warnings or errors as I can see. Can anyone help me out?