Battery Level Warning Rule

I have a number of battery powered window and door sensors that report their battery level as a percentage (Number) and that channel belongs to a group called ‘Battery’.

I’m not great at writing rules in openhab, I find the language very confusing, and so would like a little help in writing a rule that iterates through all the sensors battery level percentage channels (Battery group) and fires a warning that a particular sensor has a low battery and needs changing.

Many thanks in advance :slight_smile:

A simple rule could be:

rule "Battery rule"
when
    Item (your_battery_group_item) changed 
then
	if(battery_group_item.state < 10 ) {
	sendPushoverMessage(pushoverBuilder("Some device is below 10%").withSound("none"))
    }
end

This will send a pushover notification to your mobilphone, if any device in your group item goes below 10%.

This is indeed very simple, but this wil only get the group state and you have to set your group to MIN otherwise you won’t get an minimum value.
In my opinion you’re better off with this simple rule:

rule "Battery rule"
when
    Member of (your_battery_group_item) changed 
then
	if(triggeringItem.state < 10 ) {
        var message="Device "+triggeringItem.name.toString+ " is below 10%"
    	sendPushoverMessage(pushoverBuilder(message).withSound("none"))
    }
end
1 Like

Thanks so much for your replies, you’ve given me something to go on :slightly_smiling_face:

Fortunately, we are not forced to use the rules DSL! The new rule engine, which will be the only rule engine in OH 3.0, includes scripted automation, which allows you to use real scripting languages. I suggest Jython (which is Python for Java) and the helper libraries. You can do a manual install…

https://openhab-scripters.github.io/openhab-helper-libraries/index.html

… or just install this addon, which once reviewed and approved, will be available to install through the UI…

As for battery monitoring rules, this is what I use…

from personal.utils import notification

from core.rules import rule
from core.triggers import when

phone_battery_alerts = {}


@rule("Power: Device battery monitor")
@when("Member of gBattery changed")
@when("System started")
def device_battery_monitor(event):
    #device_battery_monitor.log.debug("Battery monitor: {}: start".format(event.itemState))
    LOW_BATTERY_THRESHOLD = 20
    if event is None or event.itemState <= DecimalType(LOW_BATTERY_THRESHOLD):
        message = "Warning! Low battery alert:\n{}".format(
            ",\n".join(
                "{}: {}%".format(low_battery.label, low_battery.state) for low_battery in sorted(
                    (battery for battery in itemRegistry.getItem("gBattery").members if battery.state < DecimalType(33)),
                    key = lambda battery: battery.state
                )
            )
        )
        notification("Battery monitor", message)


@rule("Power: Phone battery monitor")
@when("Item Phone_Scott_Battery changed")
@when("Item Phone_Lisa_Battery changed")
def phone_battery_monitor(event):
    #phone_battery_monitor.log.debug("Phone battery monitor: {}: {}: start".format(event.itemName,event.itemState))
    if not phone_battery_alerts.get(event.itemName) and (event.itemState < DecimalType(10) or event.itemState == 100):
        person = event.itemName.split("_")[1]
        phone_battery_alerts[event.itemName] = True
        notification("Phone battery monitor", "The battery in {}'s phone is {}".format(person, "very low" if event.itemState < DecimalType(10) else "charged"))
    else:
        phone_battery_alerts[event.itemName] = False
1 Like

Something to keep in mind: you might want different thresholds for different devices. My locks are in trouble if they report a battery level below about 50% (and a battery failure here really matters to me) whereas the Aqara temperature sensors appear to work fine well below 20% and I don’t care nearly so much if they run out of power. For these reasons I made multiple battery groups with different threshold settings.

2 Likes

Thanks @jswim788,

I have yet to know what level these sensors stop working at, they are all the same type, but I can imagine having different sensors in the future, so that’s a great idea :+1:

Thanks @5iver, I’ll look into it when I get the chance (house move next week) :+1:

@ljsquare, Im trying out your rule, though this is the first time I have tried pushover. The docs don’t make it clear how to configure pushover.
For example, where do I get my API key, how do I write the .cfg file etc
Any help with that would be great :wink:

The OH documentation for pushover is here.

In my pushover.cfg file in my services directory I have the API key and the user key populated. Everything else I’ve left as the default and commented out.

If you create an account at pushover.net you’ll see an option to create an application at the bottom of your account details page. Once you’ve created an application (I’ve called mine openHAB Security) it will be assigned an API key.

On pushover website.

easy when you have the API key.

This is my pushover.cfg: (I just use it simple, token and default user only, rest i standard).

# The timeout for the communication with the Pushover service (optional, defaults
# to 10000 milliseconds)
#defaultTimeout=

# You need to provide a Pushover API token to send to devices. If not here, than during
# the action call itself.
defaultToken=(your token goes here)

# You need to provide a Pushover User Key to send to devices. If not here, than during
# the action call itself.
defaultUser=(your default user goes here)

# Name of the sending application (optional). Defaults to 'openHAB'.
#defaultTitle=openHAB

# The priority to use for messages if not specified otherwise. Can range from
# -2 (lowest) to 2 (highest)
#defaultPriority=

# Url to attach to the message if not specified in the command (optional). Can be left empty.
#defaultUrl=

# Url Title to attach to the message if not specified in the command (optional). Can be left empty.
#defaultUrlTitle=

# When priority is high priority (2), how often in seconds should messages be resent. Defaults to 300 seconds.
#defaultRetry=

# When priority is high priority (2), how long to continue resending messages until acknowledged. Defaults to 3600 seconds.
#defaultExpire=

The pushover.cfg should be placed in /services/ directory.

I dont know if you can configure pushover directly from PaperUI… You may want to do that if possible.

Thanks guys, I have pushover.cfg set up now, but when I try a pushover I get this error:

Can't parse response from Pushover: Not a JSON Array: "no active devices to send to"

Never mind, I see that I needed to download the app to my phone.

@CraigDubya, I don’t use pushover. I only took the first example and rewrite it.
I use the openHAB notification and/or telegram.

Could you share your rule with openhab notification and telegram? I would like a similar setup as well

Ofcourse no problem
rule

		val message = "TOD rule \n Nieuwe status "+Current_TOD.state.toString+"\n Huidige tijd "+now.toString("HH:mm:ss.SSS")+"\n Ochtend tijd "+Morning_Time.state.format("%1$tH:%1$tM").toString+"\n Dag tijd "+Day_Time.state.format("%1$tH:%1$tM").toString+"\n Avond tijd "+Evening_Time.state.format("%1$tH:%1$tM").toString+"\n Nacht tijd "+Night_Time.state.format("%1$tH:%1$tM").toString
		telegramAction.sendTelegram(message)
		sendNotification("XXXX@XXXXX.nl",message,"error","high")

But you’ll need some configuration.
For telegram you can find it here.
For the openHAB notification, you must use the openHAB cloud connector, then you can use the sendNotification action

1 Like