Hello everyone,
i have a rule, which notifys me, when a things goes offline. Its a simple, but heavily filled rule.
It looks like this:
rule "Thing: Offlinegeräte erkannt"
when
Thing "Device1" changed or
Thing "Device2" changed or
[...]
Thing "Device150" changed or
Thing "Device151" changed
then
if (newThingStatus == "OFFLINE") {
//notify me that the device is offline
}
else if (newThingStatus == "ONLINE") {
//notify me that the device is back online
}
end
As you may see, everytime a thing goes offline i get a notification. Everytime it goes online, i get a second notification. But the problem is, that some devices turn offline for a minute and then turn online again. With this i get 2 notifications in a short amount of time. This rule is meant to notify me, when a device REALLY wents offline (e.g. no power). So i would like to use a timer, that notifys me, when a device stays offline for at least 20 Minutes.
If i add a timer like this:
var Timer timerOfflineDevice
rule
when
// Device 1 turns offline
then
if (newThingStatus == "OFFLINE") {
timerOfflineDevice = createtimer(now.plusMinutes(20), [|
//notify me, that Device 1 turned offline
timerOfflineDevice = null
])
else if (newThingStatus == "ONLINE" && timerOfflineDevice !== null) {
timerOfflineDevice.cancel()
}
else if (timerOfflineDevice === null) {
// notify me that the thing went online
}
end
you can see i’ll only be notified, when a device turns offline for at least 20 min.
But what to do if multiple devices go offline in a short timespan? The rule triggers everytime and only notifies me for the LAST device that turns offline and online again. So i would like to have “dynamic timer names”
like this:
import org.openhab.core.model.script.ScriptServiceUtil
import org.openhab.core.thing.ThingUID
rule
when
// Device X turns offline
then
var device = ScriptServiceUtil.getInstance.thingRegistry.get(new ThingUID(triggeringThing)).getLabel
var Timer timerOffline+device = null
if (newThingStatus == "OFFLINE") {
timerOffline+device = createtimer(now.plusMinutes(20), [|
//notify me, that Device X turned offline
timerOffline+device = null
])
else if (newThingStatus == "ONLINE" && timerOffline+device !== null) {
timerOffline+device.cancel()
}
else if (timerOffline+device === null) {
// notify me that Device X went back online
}
end
ScriptServiceUtil.getInstance.thingRegistry.get(new ThingUID(triggeringThing)).getLabel
gets the Things name. (e.g. “Device_1”)
This rule is meant to create a different timer, everytime a device changes its status to Offline. The timers name should look like this: timerOfflineDeviceDevice_1 or timerOfflineDeviceDevice_154.
But this does not work, since you cannot dynamically name a timer variable (Or am i wrong?)
As you can see in THIS POST Generating a dynamic variable name in a rule - #3 by Flole someone stated, that this is possible with the cache system. But i have absolutely no clue how it works and dont really understand the documentation of privateCache.
Maybe some intelligent pro can help me with this? The only solution i see at the moment is, that i create a different timer for every single Device. Since i have more than 100 devices, this is everthing else than effective.
Regards
Jimmi