Not sure if this is any help, but here’s my rule for low batteries. Although I’ve not done a great deal of testing, but it certainly works as expect and I get notifications.
rule "Low battery alert"
when
Member of Batteries changed
then
if(Batteries.state > 35) return;
var String msg = ""
val lowBatteryList = Batteries.members.filter[ b | b.state < 36 ]
lowBatteryList.forEach [ i |
msg = msg + i.name + ': ' + i.state.toString + '%\n'
logInfo("org.openhab","Battery Status: Low battery at " + i.name + ": " + i.state.toString + "%")
sendPushoverMessage(pushoverBuilder("Battery Status: Low battery at " + i.name + ": " + i.state.toString + "%"))
]
end
thanks for your sharing but whi in the rules using "val Number lowBatteryThreshold = 20 " ? there is no lowBatteryThreshold used in the rule ? or i don’t understand ? thanks for your help
@Boogieman thanks for this kind of script. Tried to use this for my Homematic devices today.
As “then” parameter I write into the logfile and sendnotification. But I’m getting only “%s muss erneuert werden”. I believe %s should be the device, which is in the group (gBatterie). Do you have an idea how I can get the necessary information, which device has the low battery warning? gBatterie show me the members of this group.
In OH3 sendPushoverMessage does not work anymore as I see. After proper configurating Pushover binding here is my solution if somebody has the same issue:
// Rule: Publish Battery Status
//
// Process all items of group GRP_BatteryStatus. Check if battery is empty and create message that is published once a day.
rule "Publish Battery Status"
when
// Time cron "0 00 6 ? * * *"
Channel 'mihome:sensor_cube:286C0785BE33:158d00027de327:action' triggered FLIP90
then
val actions = getActions("pushover", "pushover:pushover-account:0fe2ba8e66")
val String ruleIdentifier = "Publish Battery Status "
val Integer batteryThreshold = 35 // %. This should be enough to change the battery within a few days
val StringBuilder aMessage = new StringBuilder
var Integer emptyBatteries = 0
emptyBatteries = gBatteries.members.filter[ i | ((i.state instanceof DecimalType) && (i.state < batteryThreshold)) || ((i.state instanceof OnOffType) && (i.state == ON)) ].size
logInfo(ruleIdentifier, "Daily battery check found {} empty batteries to report!", emptyBatteries)
if (emptyBatteries != 0) {
aMessage.append("-> Battery report <-\n")
gBatteries.members.filter[ i | ((i.state instanceof DecimalType) && (i.state < batteryThreshold)) || ((i.state instanceof OnOffType) && (i.state == ON)) ].forEach[ aBattery |
aMessage.append(aBattery.label+"\n")
]
aMessage.append("-> End <-")
logInfo("loggerName", aMessage.toString)
actions.sendHtmlMessage(ruleIdentifier + "Daily battery check found " + emptyBatteries + " empty batteries to report!" + aMessage.toString , "openHAB")
logInfo(ruleIdentifier, "Information about {} empty batteries has been published!", emptyBatteries)
}
end
I do a battery check with JavaScript: It is nothing fancy. I also set the percentage to check in the rule. I add the batteries I want to check into the gBatterycheck group.
triggers:
- id: "1"
configuration:
time: 06:30
type: timer.TimeOfDayTrigger
conditions: []
actions:
- inputs: {}
id: "2"
configuration:
type: application/javascript
script: >
var logger =
Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' +
ctx.ruleUID);
var ArrayList = Java.type('java.util.ArrayList');
battcheckList = new ArrayList();
/*
This will check the percentage of battery left and if it is low then email
Add the items to check into gBatterycheck group
*/
var whatitis = "";
var percenttocheck = 15;
ir.getItem("gBatterycheck").members
.stream()
.filter(function(batt) {return batt.state.intValue() <= percenttocheck; } )
.forEach(function(batt) { whatitis = whatitis + batt.label +": "+ batt.state +"%" + "\r\n"; } );
if(whatitis != ""){
battcheckList.add(whatitis);
logger.info('\r\nBattery low: \r\n' + whatitis );
}
//If something in the array then send an email
if(battcheckList.length !== 0){
message = "<H4>Sensor battery is low " +battcheckList +"</H4>" + "Percentage check was set at " +percenttocheck;
actions.get("mail", "mail:smtp:1032da1f").sendHtmlMail("user@gmail.com", "Openhab sensor battery low", message );
logger.info('\r\nArray list: \r\n' + battcheckList );
}
type: script.ScriptAction
That’s OK I only wrote it today. I am not a JavaScript expert and there is probably a better way to do it but it works.
By the way I am on OH3 in case that matters.
I have converted all my scripts from DSL to JavaScript just to see if I can run without DSL. So far so good.
I use Jyhton rule engine and DSL rules mixed. No problem with any of them. Installing jython is not that easy. There are some things what you need to watch for.