# \[SOLVED\] Is there a better way to code this?

**URL:** <https://community.openhab.org/t/solved-is-there-a-better-way-to-code-this/45431>\
**Category:** Scripts & Rules\
**Created:** [May 25, 2018, 10:10am UTC](https://community.openhab.org/t/solved-is-there-a-better-way-to-code-this/45431 "2018-05-25T10:10:39Z")\
**Posts on this page:** 1\
**Showing post:** 5

<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:** [May 25, 2018, 2:38pm UTC](https://community.openhab.org/t/solved-is-there-a-better-way-to-code-this/45431/5 "2018-05-25T14:38:22Z")

</div>

I would recommend structuring your Rule according to [Design Pattern: How to Structure a Rule](https://community.openhab.org/t/design-pattern-how-to-structure-a-rule/45360) and some String manipulation which will result in something like:

```php
rule "Water Alarms"
when
    Item Kitchen_ST812_Flood_Alarm changed or
    Item Study_ST812_Flood_Alarm changed
then
    // 1. Decide if the Rule needs to run
    if(previousState == NULL) return; // we don't run on changes from the NULL state

    // 2. Calculate what needs to be done
    val room = triggeringItem.name.split("_").get(0)
    val sayMsg = if(triggeringItem.state == ON) "Water has been detected in the " + room else room + " water alarm! has now cleared."
    val nma = if(triggeringItem.state == ON) "Attention please! Water has been detected in the " + room else ""

    // 3. Do it
    if(nma != "") notifyMyAndroid("Water Alarm", nma)
    setMasterVolume(new PercentType(50)
    say(sayMsg)
end

```

It isn’t really fewer lines of code, but the code is more generic so if you needed to add more alarms later all you have to do is add them as triggers to the Rule. Or even better, create a Group and change the Rule trigger to `Member of WaterAlarms changed` (assuming you name the Group “WaterAlarms”).

It is also more maintainable in the future because if you ever want to change from using NMA to something else, or you have other things you want to do when the alarm is on, you only have to do it on ine place rather than the 2-4 places you would have to make changes in the current Rule.

I’ll also point you to [Design Pattern: Separation of Behaviors](https://community.openhab.org/t/design-pattern-separation-of-behaviors/15886) which you can use to centralize your NMA and alerting because I’m going to guess this isn’t the only Rule you have where you want say and NMA alerts.

Then your Rules become something like the following:

```csv
String InfoAlert
String AlarmAlert

Group:Switch WaterAlarms
Switch Kitchen_ST812_Flood_Alarm (WaterAlarms)
Switch Study_ST812_Flood_Alarm (WaterAlarms)

```

```php
rule "Alert!"
when
    Item InfoAlert received command or
    Item AlarmAlert received command
then
    setMasterVolume(new PercentType(50))
    say("Attention please! " + receivedCommand)

    if(triggeringItem.name == "AlarmAlert") notifyMyAndroid(receivedCommand)
end

rule "Water Alarms"
when
    Member of WaterAlarms changed
then
    // 1. Decide if the Rule needs to run
    if(previousState == NULL) return; // we don't run on changes from the NULL state

    // 2. Calculate what needs to be done
    val room = triggeringItem.name.split("_").get(0)
    val alertMsg = if(triggeringItem.state == ON) "Water has been detected in the " + room else room + " water alarm! has now cleared."

    // 3. Do it
    if(triggeringItem.state == ON) aAlert.sendCommand(alertMsg)
    else aInfo.sendCommand(alertMsg)
end

```

Not a significant savings in code if you are only alerting in this one Rule. But if more Rules you have that alert the more savings you get. Also, if you decide to use some other service instead of NMA or want to have more complex rules (e.g. I use a one alerting service during the day and differnt one at night) you only have to code that in one place.

---

_[View the full topic](https://community.openhab.org/t/solved-is-there-a-better-way-to-code-this/45431)._
