Generic notification rule

In my nice little flat I do have 20+ sensor to measure weather, temperature and plant health. Almost all of these runs of battery which unfortunately runs out once in a while.

So my idea is to add all these sensor to the group Group_Battery_Level. So by using tips from @rlkoshak my idea is to have a rule that get triggered everytime the group receives an update, then if the battery level is below 20% then send out a warning, if below 10% starts to manipulate lights to draw attention.

  • So the first problem is that some sensor uses 0-100 others 0-5, others 0-10 as battery level, can I map number item somehow to 0-100 range?

  • Then I only want notifications once a day when I am home and time of day is after afternoon and before night. If I get an email and I am not home, I most likely will forget it…

  • When the notification is sent out I would like to also know which(might be several) sensors it was.

  • Also I would like to get notification if the state of the sensor has not been updated in the last 24hours, since this then indicates dead battery, IP adress changed,Coverage changed, API key expired etc…

Is such a generic notification scheme possible to make?

My not so well functional rule (for moisture…)

val Boolean emailSent = true


rule "Water plants"
when
	Item Group_Moisture_LivingRoom changed
then
	if (Group_Moisture_LivingRoom.state <10 && emailSent){
	    sendMail("xxxxx@gmail.com","Plant","Water plants in living room")
		emailSent=false
    }
	else {
		emailSent=true
	}

end

and then the group item:

// Moisture
Group:Number:MIN Group_Moisture_LivingRoom	"Minimum Moisture [%.1f ]" 	<temperature>

Then I simply keep adding my devices to that group…

Number Miflora_Sensor1_Moisture "Sensor1 Soil Moisture [%d %%]" <text> (gMiflora, Group_Moisture,Group_Moisture_LivingRoom) {mqtt="<[mosquitto:miflora/sensor1:state:JSONPATH($.moisture)]"}

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