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"
    Item (your_battery_group_item) changed 
	if(battery_group_item.state < 10 ) {
	sendPushoverMessage(pushoverBuilder("Some device is below 10%").withSound("none"))

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"
    Member of (your_battery_group_item) changed 
	if(triggeringItem.state < 10 ) {
        var message="Device " " is below 10%"

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…

… 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))
    if event is None or event.itemState <= DecimalType(LOW_BATTERY_THRESHOLD):
        message = "Warning! Low battery alert:\n{}".format(
                "{}: {}%".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"))
        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.


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 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)

# 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'.

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

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

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

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

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

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

		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

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


I have many sensors connecting to Mosquitto via wifi and wanted to start setting up some rules for sensors battery status. I ended up at this thread.

My problem is simple and similar to that of the original author: How to get notified with sensor battery falls below XX%. It seems to me that in order to have notifications pushed to email or your phone, you either need an account with the Cloud or with

I don’t want to even consider opening my OpenHab install to the internet so the cloud is not an option. For, people may think that its just a system to acts as a bridge between your install and your phone via an API. One thing I just don’t want is to push my system status info out of my local network for security/privacy concerns.

So my question is whether it is possible to achieve my goal using something that doesn’t have to rely on the Internet. Is there a solution, maybe with an Android app that that connects to my local OpenHab server?

If not, has anyone assessed the security of

I couldn’t find anything other than a self hosted email server, and the Mail Binding. But that was too much faff for me, so I settled on Telegram - with not much evidence, I perceive this to be more secure than Pushover:

Yes, Pushover has not convinced me. I found this on their FAQ:

From here: Messages on our servers are stored in plain-text but are only stored long enough to send them out to your devices, which then check-in with our servers and trigger those messages to be deleted from our database

At least they are being transparent but that does not solve the problem. Security and privacy should be top priority for an ecosystem like OpenHab. Any notification leaving my local instance should be encrypted on my instance and only be decrypted when it reaches my phone. The fact that it stays, even for only a short time, in clear text on their servers is not acceptable.

I thought the local email server would be the only option I had to choose from. I’ll see how easy it is to install a local Postfix instance and may use that.

I’m not a programmer but why would it not be possible to have something like the OpenHab android app running a service in the background that listens for notifications coming in on the same local network?

I understand why some/many people may want a Pushover type solution or may want to connect to the cloud but for those like me that don’t want/need to expose my domotics on the Internet due to privacy/security concerns should also have an option to chose from? I guess the mail binding is one such option. Are there others in the pipeline that people are aware of?

I’ve set up a local email server to which OpenHab can send emails in my LAN. Now the real challenge … getting the rule to work :slight_smile: and befor than, actually understanding how to build the rule. Having read this thread, I think I’m going to have nightmares.

Given that I’ve never built a rule before, where do you recommend I start? I was going to start here but seems to be incomplete? From the above post, it seems like there is a way of building rules and getting syntax check. People also check logs to see if the action is working properly using the “Broadcast” function?