Generic notification rule

Look at the SCALE transform. Your best bet with that will be to map everything to 0-5. If that isn’t workable, you will have to use a Rule to normalize the values.

When a battery goes below the threshold, set a flag to send an alert.

Have a rule that triggers when it becomes Afternoon or when that flag changes to ON and creates the message.

Have a Battery alert Item that gets set to the alert message.

Have a rule that triggers when Presence changes to ON and send the message if necessary.

rule "Low battery alert"
when
    Item ToD changed or
    Item BatteryAlert received command ON
then
    // Only consider sending the alert in the AFTERNOON
    if(ToD.state.toString == "AFTERNOON") {
        // Only consider sending the alert if BatteryAlert is ON
        if(BatteryAlert.state == ON) {
            val StringBuilder message = new StringBuilder
            message.append("The following batteries are low: ")
            Batteries.members.forEach[battery | message.append(transform("batteries.map", battery.name) + " is at " + battery.state.toString) ]
            if(Present.state == ON) // send alert using message.toString
            else BatteryMessage.postUpdate(message.toString) // save the message for when you get home
        }
    }
    else {
        BatteryMessage.postUpdate(NULL)
    }
end

rule "Presence changed to ON"
when
    Item Presence changed to ON
then
    if(BatteryMessage.state != NULL && ){
        // send alert message
        BatteryMessage.postUpdate(NULL)
    }
end

rule "System started"
when
    Item ToD changed
then
    // Reset BatteryAlert
    if(ToD.state.toString == "NIGHT") {
        BatteryAlert.postUpdate(OFF)
        BatteryMessage.postUpdate(NULL) // guess we will skip this alert
    }
end

In the above code, if the Battery alert occurs before AFTERNOON, nothing happens until AFTERNOON comes around. At that time the alert message is prepared. If you are home it is immediately sent out. If not we send it to an Item to be held until you do come home.

When you come home, no matter the time of day, if there is a BatteryMessage waiting it gets sent when you get home.

We only update the BatteryAlert Item and BatteryMessage Items at the start of NIGHT so you will only ever get one alert a day and no message at all if you happen to not be home between AFTERNOON and NIGHT.

Note that I’m using Design Pattern: Human Readable Names in Messages to convert the Item names of your Batteries to something more human friendly.

This would be handled separately. I would use Expire to set the Items to NULL after 24 hours of non-activity. Assuming we are talking about your same Battery Items get refreshed every 24 hours then:

rule "Low battery alert"
when
    Item ToD changed or
    Item BatteryAlert received command ON
then
    // Only consider sending the alert in the AFTERNOON
    if(ToD.state.toString == "AFTERNOON") {
        // Only consider sending the alert if BatteryAlert is ON
        if(BatteryAlert.state == ON) {
            val StringBuilder message = new StringBuilder
            message.append("The following batteries are low: ")
            Batteries.members.filter[battery | battery.state != NULL].forEach[battery | message.append(transform("MAP", "batteries.map", battery.name) + " is at " + battery.state.toString) + " "]
            message.append("\nThe following devices are offline: ")
            Batteries.members.filter[battery | battery.state == NULL].forEach[battery | message append(transform("MAP", "batteries.map", battery.name) + " ")]
            if(Present.state == ON) // send alert using message.toString
            else BatteryMessage.postUpdate(message.toString) // save the message for when you get home
        }
    }
    else {
        BatteryMessage.postUpdate(NULL)
    }
end
3 Likes