I thought it important to post this working example for the noobs (like me). I have seen many queries on this topic with scattered info, and only few confirmed working examples. I can confirm this works on OH2.0.0b3.
Basically, this test rule switches a Switch item OFF just seconds later (according to the Setpoint value).
// Items:
Number testSetpoint "Test Duration [%d s]"
Switch testSwitch "Test Switch" {mqtt=">[mosquitto:/ESP12e3/gpio/12:command:ON:1],>[mosquitto:/ESP12e3/gpio/12:command:OFF:0]"}
// Sitemap:
sitemap mySite1 label="Test Switch"
{
Setpoint item=testSetpoint minValue=1 maxValue=20 step=1
Switch item=testSwitch
}
// Rules:
rule "Test Timer"
when
Item testSwitch received update
then
if (testSwitch.state == ON)
{
var int testDuration = (testSetpoint.state as DecimalType).intValue
var DateTime endTest = now.plusSeconds(testDuration)
createTimer(endTest) [|
sendCommand(testSwitch, OFF)
postUpdate(testSwitch, OFF)
]
}
end
Similar example for a pool pump auto timer. The Pump Auto Timer Activation switch (poolPumpTimer) is the master switch for the timer. Switch poolPumpSwitch is used to manually switch the pool pump on/off, example; when cleaning the filters - especially nice when the pool or filters are a distance from the circuit breakers, making it easy to control from your mobile…
// Items:
Switch poolPumpTimer "Pump Auto Timer Activation"
Switch poolPumpSwitch "Pool Pump ON/OFF Switch" {mqtt=">[mosquitto:/ESP12e2/gpio/12:command:ON:1],>[mosquitto:/ESP12e2/gpio/12:command:OFF:0]"}
Number poolStartHour "Pool Pump Start Time [%d:00]"
Number poolPumpDuration "Pool Pump On Duration [%d Hr]"
// Sitemap:
sitemap home1 label="Home"
{
Frame label="Swimming Pool Pump"
{
Switch item=poolPumpTimer
Setpoint item=poolStartHour minValue=1 maxValue=23 step=1
Setpoint item=poolPumpDuration minValue=1 maxValue=12 step=1
Switch item=poolPumpSwitch
}
// Rules:
rule "Swimming Pool Pump Timer"
when
Item poolPumpTimer received update or
Time cron "0 5 0 * * ?"
then
var int startHrInt = (poolStartHour.state as DecimalType).intValue
var int durationHrInt = (poolPumpDuration.state as DecimalType).intValue
var DateTime startTmr = parse(now.getYear() + "-" + now.getMonthOfYear() + "-" + now.getDayOfMonth() + "T" + startHrInt + ":00")
var DateTime stopTmr = parse(now.getYear() + "-" + now.getMonthOfYear() + "-" + now.getDayOfMonth() + "T" + startHrInt + ":00").plusHours(durationHrInt)
var Timer poolOnTimer
var Timer poolOffTimer
if(poolPumpTimer.state == ON)
{
poolOnTimer = createTimer(startTmr) [|
sendCommand(poolPumpSwitch, ON)
postUpdate(poolPumpSwitch, ON)
Thread::sleep(10000) //wait 10 seconds, then resend for MQTT over WiFi
sendCommand(poolPumpSwitch, ON)
]
poolOffTimer = createTimer(stopTmr) [|
sendCommand(poolPumpSwitch, OFF)
postUpdate(poolPumpSwitch, OFF)
Thread::sleep(10000) //wait 10 seconds, then resend for MQTT over WiFi
sendCommand(poolPumpSwitch, OFF)
]
}
if(poolPumpTimer.state == OFF)
{
sendCommand(poolPumpSwitch, OFF)
postUpdate(poolPumpSwitch, OFF)
Thread::sleep(10000) //wait 10 seconds, then resend for MQTT over WiFi
sendCommand(poolPumpSwitch, OFF)
if(poolOnTimer != null)
{
poolOnTimer.cancel
poolOnTimer = null
}
if(poolOffTimer != null)
{
poolOffTimer.cancel
poolOffTimer = null
}
}
end