Hi,
I am running a weekly battery percentage check and the script runs through a battery check group and gets the name and the battery level. I get a print out but when I try to sort it by item label it fails.
var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);
var ArrayList = Java.type('java.util.ArrayList');
battcheckList = new ArrayList();
var datetoday = new Date();
var numberofweek = datetoday.getDay();
//WEEKLY REPORT
//Only do on Sundays (numberofweek is 0)
if (numberofweek == 0){
var whatitis = "";
var percenttocheck = 100;
ir.getItem("gBatterycheck").members
.stream()
.filter(function(batt) {return batt.state != NULL && batt.state.intValue() <= percenttocheck; } )
.forEach(function(batt) { whatitis = whatitis + batt.label +": "+ batt.state +"%" + "\r\n"; } );
if(whatitis != ""){
battcheckList.add(whatitis);
logger.info('\r\nBattery report: \r\n' + whatitis );
// logger.info('\r\nBattery report: \r\n' + whatitis.sort() );
}
//If something in the array then send an email
if(battcheckList.length !== 0){
message = "<H4>Battery weekly report <br> " +battcheckList +"</H4>" + "Percentage check was set at " +percenttocheck;
// actions.get("mail", "mail:smtp:a514b96247").sendHtmlMail("somebody@gmail.com", "Battery check weekly report", message );
}
logger.info('\r\nDay of week: \r\n' + numberofweek );
}
If I use the above code I get this output:
Battery report:
Window Bedroom Greg Battery: 100%
Window Bathroom Greg Battery: 100%
Temperature kitchen battery: 100%
Door front middle battery: 100%
Door front LH battery: 100%
Door back battery: 100%
Temperature Alison room battery: 75%
Door Alison battery: 100%
Hue Greg battery: 100%
Door front RH battery: 100%
Hue lounge battery: 95.5%
Door Greg battery: 100%
Window Bedroom Alison Battery: 100%
Window Bathroom Alison Battery: 100%
Temperature bedroom Greg battery: 83%
If I try to use logger.info(‘\r\nBattery report: \r\n’ + whatitis.sort() );
I get this.
Script execution of rule with UID '170a2157a6' failed: TypeError: whatitis.sort is not a function in <eval> at line number 23
I would like to sort By Door, Window etc.
I am missing something in the sorting.
Any help would be appreciated.
Your whatitis variable is just a string. Strings don’t have a sort() property. You need to get your items sorted before you run the forEach method to build the whatitis string.
sort() still won’t help you here though because you’ve run into one of the cases with nashorn where java and javascript objects are getting mixed together. Once you have done this:
ir.getItem("gBatterycheck").members
.stream()
you’re working with a java stream which does have a sorted() method, but you have still have your work cut out for you there. Rich (of course) has a better explanation:
Yes. I have something that sorts the label name now.
Thanks. I searched for sort,sorting. etc but that article didn’t come up.
Anyway this is what I have and it works in case someone else needs to sort the group by label.
var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);
var ArrayList = Java.type('java.util.ArrayList');
var Collectors = Java.type("java.util.stream.Collectors");
var gBatterycheck = ir.getItem("gBatterycheck");
gBatterycheck
.getAllMembers()
.stream()
.map(function(i){ return i.label; })
.sorted()
.forEach(function(label){
logger.info(label);
});
I have modified the code so now I can get the label name AND the state which in this case is the battery percentage. Putting it here for anyone else who may be looking to do the same.