HowTo: Get notified on empty batteries

@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

hi,

"aBattery" 

is coming from ?
i try your rule and i have a

 "sendHtmlMessage" : "The method sendHtmlMessage(String, String) is undefined for the type "

thanks for your sharing

Hello, No it’s not coming from that.
Pls check this line and your pushover configuartion:

val actions = getActions("pushover", "pushover:pushover-account:0fe2ba8e66") // "pushover:pushover-account:YourPushoverID"

This is needs to be replaced by your pushover ID: 0fe2ba8e66
Then it will work.

i do , but i’m on “2.5” i try to setup to my test “3.0” thanks for that !:wink:

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

1 Like

thanks a lot for this, i never use javascript on Openhab, so is time to try ! thanks !:wink:

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. :slightly_smiling_face:

You use files ? or on “UI” directly ?

setup jython javascript fail every time i try…

I only use the UI. That was another thing I wanted to see if I could do it all from UI. Also so far so good.

If you copy and paste the code into your rule it should just work. Let me know.

I tell you sure :slight_smile:

To test just change the percenttocheck to greater than 100 and you should see all the batteries in the group. Good luck.

:+1:

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.

On 3.0 ? what post you use for this ?
I tried several but each time I have errors, javascript even bug for the “Hello World”.

thanks in advance

What do you mean by what post?

To install jython I used this procedure, python script works, on the other hand javascript, bug all the time, I tried several times the procedure but without success, if you succeeded maybe you have a link describing it, I must obviously missing something :slight_smile:
I would like to order the javascript files on my test server 3.0.

thanks in advance

I didn’t install anything else. Mine is just a plain install of OH3.

I have these bindings: MQTT,mail,Astro.
I have no DSL rules and only ECMAScript (JavaScript).
My set up is very simple.

Start a new post for this, instead of adding on to a tutorial about something else,it’ll work out better.

Thank you rossko57, this is what will do it will indeed be clearer :+1: