It’s the “other parts” that converts the list to a String, namely the reduce
. If you are going to copy code it’s important to understand what it does before you change it. You can find more information on map/reduce at Design Pattern: Working with Groups in Rules
The map
operation takes each element of the list, does something to it, and returns a new list with the results. The reduce takes each element of the list and combines them into a single result.
val lowBatteryList = gBattery.members.filter[b|b.state <= 15]
.map[ i | i.name + ' ' + i.state ]
.reduce[ msg, b | list = b + '\n' ]
mailActions.sendMail("vanja@something.net", "openHAB Alert - Battery Low", "Low battery at:\n" + lowBatteryList)
In JS this is simpler since we have the join
operator. Note, you should always get a Thing action inside the rule that uses it. When you get the Action as a global, if the Thing isn’t online when the .rules file is read or if the thing goes offline then back online the action will fail until you reload the .rules file.
rules.when().timeOfDay("00:00")
.if(() => actions.Ephemeris.isWeekend())
.then( () =>
const bats = items.gBatteries.filter( i => i.numericState < 15)
.map(i => i.name + ' ' + i.state)
.join('\n');
actions.get("mail", "uid:of:mail:thing")
.sendMail("vanja@something.net",
"openHAB Alert - Battery Low",
"Low battery at:\n" + bats)
)
.build('Low battery alert', 'Sends an email with the list of all the low batteries.', [], 'lowBatteries');
Note the above is using rule builder.