Low Battery Alert (Example Rule Not Working)

Here is a solution in my usecase that works fine. Only the the notification in sitemap is lack. :slight_smile:
Instead of telegram you can simple adapt a other service.

import java.util.HashMap
import java.util.ArrayList
import java.util.concurrent.locks.Lock
import java.util.concurrent.locks.ReentrantLock

var Lock lock = new ReentrantLock()

//
// init values
// -----------------------------------------------------------------------------------------------------------
// customize for you own enviroment

var HashMap<String, Object> batteryNotificationInits = newLinkedHashMap(
	"debounceDelay" -> 300,		 						// debounce time state updates in group [ms]
	"message" -> "Batterietausch\n",                    // message subject then batterychange notice
	"telegramBot" -> "TelegramBotName",						// if the telegram bot available then bot name else ""
    "triggerValues" -> newArrayList(7,10,15),           // trigger values
	"debounceGroupTimer" -> null as Timer				// debounce timer						- no config
)

//
// data structure
// -----------------------------------------------------------------------------------------------------------
// you will get a telegram message then the batterylevel reached a specified level 
// triggerValues specified the level
// the first value (i.e. 7) means, you will get a message for 1%,2%,3%,4%,5%,6%,7%
// all other value means, you will get a message at this point 
//
// the battery battery values should be from 1% to 100%
//
// in item file:
// all battery obtained devices must be assigned to the group gBatteryStatus


rule "battery status and send notifications"
when
    Item gBatteryStatus received update
then
	try {
		lock.lock()
		var Timer debTimer = batteryNotificationInits.get("debounceGroupTimer")

        if (debTimer === null) {
            var expTime = now.plusMillis(batteryNotificationInits.get("debounceDelay"))
            debTimer = createTimerWithArgument(expTime, "debounce", [ p |
                try {
                    lock.lock()
                    val String bot = batteryNotificationInits.get("telegramBot")
                    var boolean telegramBotExist = false
                    var String notificationMessage = batteryNotificationInits.get("message")
                    var String notificationMessageItems = ""
                    var ArrayList<Integer> triggerValues = batteryNotificationInits.get("triggerValues") as ArrayList<Integer>
                    val Integer minToTrigger = triggerValues.get(triggerValues.size()-1)
                    if (batteryNotificationInits.get("telegramBot") != "") {
                        telegramBotExist = true
                    }

                    var triggertDevices = gBatteryStatus.members.filter[s|s.state <= minToTrigger]

                    triggertDevices.forEach [ i |
                        var Boolean addItem = false
                        if (i.state <= triggerValues.get(0)) {
                            addItem = true
                        }
                        if (triggerValues.contains((i.state as DecimalType).intValue)) {
                            addItem = true
                        }

                        if (addItem) {
                            notificationMessageItems = notificationMessageItems + i.name + ': ' + i.state.toString + '%\n'
                        }
                    ]

                    if (telegramBotExist && (notificationMessageItems != "")) {
                        sendTelegram(bot,notificationMessage + notificationMessageItems)
                    }

                    // insert message in Sitemap

					batteryNotificationInits.put("debounceGroupTimer",null)
					lock.unlock()
				}
				catch(Throwable t) {
			        logError("RuleBattery","E0201 something is wrong: " + t.toString)
        			lock.unlock()
    			}
			])
			batteryNotificationInits.put("debounceGroupTimer",debTimer)
        } else {
            debTimer.reschedule(now.plusMillis(batteryNotificationInits.get("debounceDelay")))
        }
        lock.unlock()
	}
    catch(Throwable t) {
        logError("RuleBattery","E0202 something is wrong: " + t.toString)
		smokeAlarmInits.put("debounceGroupTimer",null)
        lock.unlock()
    }
end
1 Like