I have an irrigation setup with 4 zones
Each zone is a switch item called SprinklerZone1 to SprinklerZone4
My rules file is long, as i have each seperate rule, written seperately for each zone.
For instance, i have a rule that runs when the zone is turned on, One that runs when its turned off, and one that runs when the zone starts and shows a countdown timer.
But I then have each of those 4 times, for each zone.
Is there a way to get the number from the switch or number item, and use that in a variable. So i can condense my rules file smaller.
For instance
when SprinklerSystem1 changed to ON
then
var Number Zone = item.Switch.numbercode (i know this isn’t correct, but it shows what im trying to do.
Here is my rules and items file for reference.
String Irrigation_Zone1 "Zone 1 is [%s]"
String Irrigation_Zone2 "Zone 2 is [%s]"
String Irrigation_Zone3 "Zone 3 is [%s]"
String Irrigation_Zone4 "Zone 4 is [%s]"
Switch FrontDownlights "Front Downlights" [ "Switchable" ]{mqtt=">[broker:SmartHouse/Utilities/SprinklerSystem:command:ON:TESTON],>[broker:SmartHouse/Utilities/SprinklerSystem:command:OFF:TESTOFF]"}
Number Countdown_Zone1 "Minutes Remaining [%d]"
Number Countdown_Zone2 "Minutes Remaining [%d]"
Number Countdown_Zone3 "Minutes Remaining [%d]"
Number Countdown_Zone4 "Minutes Remaining [%d]"
Switch SprinklerCycle "30 Minute Cycle"
Switch SprinklerSystemSchedule1 "Front Grass Schedule"
Switch SprinklerSystemSchedule2 "Front Garden Schedule"
Switch SprinklerSystemSchedule3 "Back Grass Schedule"
Switch SprinklerSystemSchedule4 "Back Garden Schedule"
Switch SprinklerSystemZone1 "Front Grass Sprinklers" [ "Switchable" ] {mqtt=">[broker:SmartHouse/Utilities/SprinklerSystem:command:ON:Z1ON],>[broker:SmartHouse/Utilities/SprinklerSystem:command:OFF:Z1OFF]"}
Switch SprinklerSystemZone2 "Front Garden Sprinklers"[ "Switchable" ] {mqtt=">[broker:SmartHouse/Utilities/SprinklerSystem:command:ON:Z2ON],>[broker:SmartHouse/Utilities/SprinklerSystem:command:OFF:Z2OFF]"}
Switch SprinklerSystemZone3 "Back Grass Sprinklers" [ "Switchable" ] {mqtt=">[broker:SmartHouse/Utilities/SprinklerSystem:command:ON:Z3ON],>[broker:SmartHouse/Utilities/SprinklerSystem:command:OFF:Z3OFF]"}
Switch SprinklerSystemZone4 "Back Garden Sprinklers" [ "Switchable" ] {mqtt=">[broker:SmartHouse/Utilities/SprinklerSystem:command:ON:Z4ON],>[broker:SmartHouse/Utilities/SprinklerSystem:command:OFF:Z4OFF]"}
DateTime Irrigation_StartTime1 "Zone 1 Last Watered [%1$ta %1$tR]"
DateTime Irrigation_StartTime2 "Zone 2 Last Watered [%1$ta %1$tR]"
DateTime Irrigation_StartTime3 "Zone 3 Last Watered [%1$ta %1$tR]"
DateTime Irrigation_StartTime4 "Zone 4 Last Watered [%1$ta %1$tR]"
and my Rules File
//This is the rules page
import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.joda.time.*
rule "Reset Irrigation at OH Start"
when
System started
then
SprinklerySystemZone1.sendCommand(OFF)
SprinklerySystemZone2.sendCommand(OFF)
SprinklerySystemZone3.sendCommand(OFF)
SprinklerySystemZone4.sendCommand(OFF)
Irrigation_Zone1.postUpdate("Inactive")
Irrigation_Zone2.postUpdate("Inactive")
Irrigation_Zone3.postUpdate("Inactive")
Irrigation_Zone4.postUpdate("Inactive")
Countdown_Zone1.postUpdate(0)
Countdown_Zone2.postUpdate(0)
Countdown_Zone3.postUpdate(0)
Countdown_Zone4.postUpdate(0)
end
//**************************************************************************************
rule "Zone 1 Inactive"
when
Item SprinklerSystemZone1 received command OFF
then
Countdown_Zone1.postUpdate(0)
Irrigation_Zone1.postUpdate("Inactive")
end
//**************************************************************************************
rule "Zone 2 Inactive"
when
Item SprinklerSystemZone2 received command OFF
then
Countdown_Zone2.postUpdate(0)
Irrigation_Zone2.postUpdate("Inactive")
end
//**************************************************************************************
rule "Zone 3 Inactive"
when
Item SprinklerSystemZone3 received command OFF
then
Countdown_Zone3.postUpdate(0)
Irrigation_Zone3.postUpdate("Inactive")
end
//**************************************************************************************
rule "Zone 4 Inactive"
when
Item SprinklerSystemZone4 received command OFF
then
Countdown_Zone4.postUpdate(0)
Irrigation_Zone4.postUpdate("Inactive")
end
//**************************************************************************************
//Zone 1 Watering
rule "Zone 1 Watering"
when
Item SprinklerSystemZone1 changed to ON
then
var Number timeout = onHours1.state as DecimalType
var int lawnTime = (timeout).intValue
postUpdate(Irrigation_StartTime1, new DateTimeType())
Irrigation_Zone1.postUpdate("Active")
if (lawnTime > 0)
{
sendBroadcastNotification("Watering Zone 1 for " + timeout.toString + " minutes")
Irrigation_Zone1.postUpdate("Active " + timeout.toString + " minutes remaining")
Countdown_Zone1.postUpdate(lawnTime)
while (SprinklerSystemZone1.state == ON)
{
if (now.minusMinutes(lawnTime).isAfter((Irrigation_StartTime1.state as DateTimeType).calendar.timeInMillis))
{
sendCommand(SprinklerSystemZone1, OFF)
sendBroadcastNotification("Zone 1 has finished watering")
lawnTime = 0
}
if ((onHours1.state as DecimalType).intValue != lawnTime)
{
lawnTime = (onHours1.state as DecimalType).intValue
}
}
}
else {
sendBroadcastNotification("Watering Zone 1")
}
end
//**************************************************************************************
//Zone 2 Watering
rule "Zone 2 Watering"
when
Item SprinklerSystemZone2 changed to ON
then
var Number timeout = onHours2.state as DecimalType
var int lawnTime = (timeout).intValue
postUpdate(Irrigation_StartTime2, new DateTimeType())
Irrigation_Zone2.postUpdate("Active")
if (lawnTime > 0)
{
sendBroadcastNotification("Watering Zone 2 for " + timeout.toString + " minutes")
Irrigation_Zone2.postUpdate("Active " + timeout.toString + " minutes remaining")
Countdown_Zone2.postUpdate(lawnTime)
while (SprinklerSystemZone2.state == ON)
{
if (now.minusMinutes(lawnTime).isAfter((Irrigation_StartTime2.state as DateTimeType).calendar.timeInMillis))
{
sendCommand(SprinklerSystemZone2, OFF)
sendBroadcastNotification("Zone 2 has finished watering")
lawnTime = 0
}
if ((onHours2.state as DecimalType).intValue != lawnTime)
{
lawnTime = (onHours2.state as DecimalType).intValue
}
}
}
else {
sendBroadcastNotification("Watering Zone 2")
}
end
//**************************************************************************************
//Zone 3 Watering
rule "Zone 3 Watering"
when
Item SprinklerSystemZone3 changed to ON
then
var Number timeout = onHours3.state as DecimalType
var int lawnTime = (timeout).intValue
postUpdate(Irrigation_StartTime3, new DateTimeType())
Irrigation_Zone3.postUpdate("Active")
if (lawnTime > 0)
{
sendBroadcastNotification("Watering Zone 3 for " + timeout.toString + " minutes")
Irrigation_Zone3.postUpdate("Active " + timeout.toString + " minutes remaining")
Countdown_Zone3.postUpdate(lawnTime)
while (SprinklerSystemZone3.state == ON)
{
if (now.minusMinutes(lawnTime).isAfter((Irrigation_StartTime3.state as DateTimeType).calendar.timeInMillis))
{
sendCommand(SprinklerSystemZone3, OFF)
sendBroadcastNotification("Zone 3 has finished watering")
lawnTime = 0
}
if ((onHours3.state as DecimalType).intValue != lawnTime)
{
lawnTime = (onHours3.state as DecimalType).intValue
}
}
}
else {
sendBroadcastNotification("Watering Zone 3")
}
end
//**************************************************************************************
//Zone 4 Watering
rule "Zone 4 Watering"
when
Item SprinklerSystemZone4 changed to ON
then
var Number timeout = onHours4.state as DecimalType
var int lawnTime = (timeout).intValue
postUpdate(Irrigation_StartTime4, new DateTimeType())
Irrigation_Zone4.postUpdate("Active")
if (lawnTime > 0)
{
sendBroadcastNotification("Watering Zone 4 for " + timeout.toString + " minutes")
Irrigation_Zone4.postUpdate("Active " + timeout.toString + " minutes remaining")
Countdown_Zone4.postUpdate(lawnTime)
while (SprinklerSystemZone4.state == ON)
{
if (now.minusMinutes(lawnTime).isAfter((Irrigation_StartTime4.state as DateTimeType).calendar.timeInMillis))
{
sendCommand(SprinklerSystemZone4, OFF)
sendBroadcastNotification("Zone 4 has finished watering")
lawnTime = 0
}
if ((onHours4.state as DecimalType).intValue != lawnTime)
{
lawnTime = (onHours4.state as DecimalType).intValue
}
}
}
else {
sendBroadcastNotification("Watering Zone 4")
}
end
//***************************************************************************
rule "Countdown Zone 1 Timer"
when
Item Countdown_Zone1 changed
then
createTimer(now.plusMinutes(1)) [|
if (Countdown_Zone1.state as DecimalType != 0)
{
Irrigation_Zone1.postUpdate("Active " + (Countdown_Zone1.state as DecimalType - 1) + " minutes remaining")
Countdown_Zone1.postUpdate(Countdown_Zone1.state as DecimalType - 1)
}
]
end
//***************************************************************************
rule "Countdown Zone 2 Timer"
when
Item Countdown_Zone2 changed
then
createTimer(now.plusMinutes(1)) [|
if (Countdown_Zone2.state as DecimalType != 0)
{
Irrigation_Zone2.postUpdate("Active " + (Countdown_Zone2.state as DecimalType - 1) + " minutes remaining")
Countdown_Zone2.postUpdate(Countdown_Zone2.state as DecimalType - 1)
}
]
end
//***************************************************************************
rule "Countdown Zone 3 Timer"
when
Item Countdown_Zone3 changed
then
createTimer(now.plusMinutes(1)) [|
if (Countdown_Zone3.state as DecimalType != 0)
{
Irrigation_Zone3.postUpdate("Active " + (Countdown_Zone3.state as DecimalType - 1) + " minutes remaining")
Countdown_Zone3.postUpdate(Countdown_Zone3.state as DecimalType - 1)
}
]
end
//***************************************************************************
rule "Countdown Zone 4 Timer"
when
Item Countdown_Zone4 changed
then
createTimer(now.plusMinutes(1)) [|
if (Countdown_Zone4.state as DecimalType != 0)
{
Irrigation_Zone4.postUpdate("Active " + (Countdown_Zone4.state as DecimalType - 1) + " minutes remaining")
Countdown_Zone4.postUpdate(Countdown_Zone4.state as DecimalType - 1)
}
]
end