Hi there,
i´m currently starting to migrate and optimize a rule that i used in oH 1.8 to send me an reminder when one of the batterys in my smarthome devices is empty or almost empty.
This rule is 96 lines long and every single battery item is hardcoded…
I want to make this rule simplier/smarter then before and most of all scalable
What do i have?
5 devices with State (OPEN = full/CLOSED = empty)
3 devices with Voltage (under 2.20V is low)
2 devices with Percent (under 20% is low)
So currently i need 10 hardcoded if
s to check one by one.
Two things will stay in my rule:
var BatterieText = "Die folgenden Batterien sind leer: "
BatterieText = BatterieText.removeEnd(", ")
I already made groups for each type:
Group gBatteryState
Group gBatteryLevel
Group gBatteryPercent
Then i thought how to trigger this?
A cron jon is nice but would cause that i´m not able to use triggeringItem
and make it easier.
So i´m just checking if one of the items changed
Member of gBatteryLevel changed
or
Member of gBatteryState changed
or
Member of gBatteryPercent changed
What brings me to the next check, what type of item triggered the rule?
var TriggerLevel = gBatteryLevel.members.filter[ i|i.state <= 2.20 ].map[ name ].reduce[ result, name | result = name + ", " ]
var TriggerState = gBatteryState.members.filter[ i|i.state == CLOSED ].map[ name ].reduce[ result, name | result = name + ", " ]
var TriggerPercent = gBatteryPercent.members.filter[ i|i.state <= 20 ].map[ name ].reduce[ result, name | result = name + ", " ]
Now i´m down to three checks instead of ten (or more with more devices)
Let´s move on and make a check if the triggering item is low or in other words, passed the three checks before.
val EmptyBattery = (TriggerLevel !== null || TriggerState !== null || TriggerPercent !== null)
Add the triggering item to the BatterieText
and send a message if the triggering item is low.
if(TriggerLevel !== null)
{
BatterieText = BatterieText + triggeringItem.label
logInfo("BatteryCheck", TriggerLevel)
}
if(TriggerState !== null)
{
BatterieText = BatterieText + triggeringItem.label
logInfo("BatteryCheck", TriggerState)
}
if(TriggerPercent !== null)
{
BatterieText = BatterieText + triggeringItem.label
logInfo("BatteryCheck", TriggerPercent)
}
if(EmptyBattery)
{
sendTelegram("bot1", BatterieText)
}
else
{
logInfo("BatteryCheck", "Alle Batterien sind voll.")
}
And i think here´s my problem.
This rule would only work with one device having a low battery and run through this rule.
But what about the unlikely event of two batterys being low and triggering this rule one after another?
I was thinking about an persisted dummy item that will be filled with all triggering items and then being used for a time based reminder.
Any ideas how to make this battery check able to work with multiple triggering items?
kind regards
Michael