# Generic notification rule

**URL:** <https://community.openhab.org/t/generic-notification-rule/37011>\
**Category:** Scripts & Rules\
**Tags:** rules, openhab2\
**Created:** [December 5, 2017, 10:41pm UTC](https://community.openhab.org/t/generic-notification-rule/37011 "2017-12-05T22:41:06Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [December 5, 2017, 11:09pm UTC](https://community.openhab.org/t/generic-notification-rule/37011/2 "2017-12-05T23:09:49Z")

</div>

> [@skatun](#):
>
> can I map number item somehow to 0-100 range?

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.

> [@skatun](#):
>
> 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 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.

```php
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](https://community.openhab.org/t/design-pattern-human-readable-names-in-messages/34117) to convert the Item names of your Batteries to something more human friendly.

> [@skatun](#):
>
> 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…

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:

```php
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

```

---

_[View the full topic](https://community.openhab.org/t/generic-notification-rule/37011)._
