Multiple battery sensor alert email - is there a better way?

Hi all, I have a rule that is working just as I want. However I don’t think it’s written in the best way and am wondering if anyone can suggest a cleaner solution please.

I have read the doc’s and other similar topic’s on here, but cant find anything that matches what I’m trying to do. I was looking at using a variable, not sure if that’s the right thing to do.

What I want is to receive an email when any of my sensor batteries report a low battery. I’m assuming there is a way of doing this other than the very longwinded was I’ve implemented the rule.

//test rule to send a mail

rule "Sensor battery email alert"
Item item1 changed or
Item item2 changed or
Item item3 changed or
Item item4 changed or
Item item5 changed or
Item item6 changed or
Item item7 changed or
Item item8 changed or
Item item9 changed or
Item item10 changed
	sendMail("; , "SENSOR BATTERY ALERT", "A sensor in the house is reporting a low battery. 

    Here's the status of the house sensor batteries, any value of zero or ON is a problem
    + " PIR  =   " + item1.state
    + "
     contact    =   " + item2.state
    + "
     contact   =   " + item3.state
    + "
     contact =   " + item4.state
    + "
     contact  =   " + item5.state
    + "
     PIR =   " + item6.state
    + "
     PIR =   " + item7.state
    + "
     PIR  =   " + item8.state
    + "
     detector  =   " + item9.state
    + "
     sensor =   " + item10.state
    + "
     stat   =   " + item11.state


Create a group of all your battery items that triggers your rule.
Inside the rule, sweep through the items to see which one caused the trigger

You should make use of groups, and triggeringItem.state (the item that changed within that group). See here as well for the groups tutorial.

To reduce the # of emails, you can have an internal timestamp to only send every so often. You can refer to an example implementation in this code.

1 Like

I will prepare a small tutorial for that, but this will take some days

see this link.
I will write it up better next week…now with alexa text to speech support aswell

Just to give you a short impression what i have designed. Every battery drive devide is in a goup of battery devices and in addition the battery channel is in a group as well. Every day a small rule checks the battrey channel group and generates a message of “empty” batteries that ist send out to me.

In my enviroment a battery channel is sometimes a number an somtimes a switch, so the rule differentiates that.

Over the last years i found out that a message once a day is enough, because often there are no batteries at home and typically if we get the message we have 3 to 5 day left to react.

All my Battery items are in a group Batteries
And I use this rule:
It runs at 10:55am daily because that’s convinient for me but you can change that of course!

rule "Battery Monitor"
    Time cron "0 55 10 * * ?"
    if (!Batteries.allMembers.filter( [ battery | (battery.state as Number) <= (lowBatteryThreshold) ] ).empty) {
        val String report = Batteries.allMembers.filter( [ b | (b.state as Number) <= (lowBatteryThreshold) ] ).sortBy( [ bat | bat.state as Number ] ).map[ name + ": " + state.toString ].join("\n")
        val message = "Battery levels:\n\n" + report + "\n\nRegards,\n\nOur House"
        sendMail("", "Low battery alert !", message)

@Dibbler42 wrote a nice HowTo here

There a two more methods you should look at in his thread.

1 Like