There is a flaw in the Cron approach when trying to emulate a timer in that the first cycle could be any amount of time from the start of the count to the end. Therefore, I’ve created this scheme to resolve the timer issue.
I am trying to solve for the lack of timer functionality in OH3. The 2 rules below, Template Timer w/Counter V2 and Template Timer wo/Counter V2, are their own individual rules with timer functions that fullfill my requirement. The Counter will allow one to limit the number of notifications or actions when a trigger event is initiated. i.e. limit the number of SMS messages.
The item ChangeMeTemp could also be a door ChangeMeDoor. Just make sure the the .state is updated in the rules. Template Timer wo/Counter V2 at the bottom is for a nightlight function.
Notes
- Each Alert and corresponding item will require it’s own set of rules
- ChangeMe is a quick way to find text you need to change
- Change the timer count to suit your needs.
–Existing Item–
ChangeMeTemp = Temperature you are monitoring
–New Items–
Switch ChangeMeSwitch "Template switch V2" [AlertTemplate]
Switch ChangeMeCounter "Template counter V2" [AlertTemplate]
Number ChangeMeRestartTimer"Template timer V2" [AlertTemplate]
There are 3 Rules and more detail is below. Here’s a synopsis of the Actions (commands)
Rule 1 Template Timer w/Counter V2
triggers:
itemName: ChangeMeSwitch
command: ON
itemName: ChangeMeRestartTimer
command: ON
Do something after the first timer has cycled
Maybe you want to allow the trigger event to cycle once for an amount of time based on the cron. i.e. the temperture is over the threshold because the door is open and you don’t want an alert unless the temperature remains to be over the threshold for a period of time.
Do if you want to be notified the trigger items is not resolved and this is the last notification
Just in case you want to be notified that you are not going to recieve any more notifications because the counter is exhausted.
Do if you want to be reminded each time the timer cycles
More frequent reminders based on your counter value
—CODE—
triggers:
- id: "2"
configuration:
itemName: ChangeMeSwitch
command: ON
type: core.ItemCommandTrigger
- id: "3"
configuration:
itemName: ChangeMeRestartTimer
command: ON
type: core.ItemCommandTrigger
conditions: []
actions:
- inputs: {}
id: "1"
configuration:
type: application/vnd.openhab.dsl.rule
script: >-
if ((ChangeMeCounter.state > 0) && (ChangeMeSwitch.state == ON))
{
logInfo("Test Timer", "timer Created waiting 10 Seconds")
createTimer(now.plusSeconds(10),
[|
var Number varCount = ChangeMeCounter.state as DecimalType
if (varCount != 0)
{
if (varCount == 5)
{
varCount = varCount - 1
logInfo("Test Timer Rule","Counter initialized. Current varCount is at " + varCount)
ChangeMeCounter.postUpdate(varCount)
sendCommand(ChangeMeRestartTimer, ON)
//Do something after the first timer has cycled
}
else
{
if (varCount == 1)
{
varCount = varCount - 1
logInfo("Test Timer Rule","Counter This is the last notification. Current varCount is at " + varCount)
ChangeMeCounter.postUpdate(varCount)
sendCommand(ChangeMeRestartTimer, ON)
//Do if you want to be notifed the trigger items is not resolved and this is the last notification
}
else
{
varCount = varCount - 1
logInfo("Test Timer Rule","Counter repeating. Current varCount is at " + varCount)
ChangeMeCounter.postUpdate(varCount)
sendCommand(ChangeMeRestartTimer, ON)
//Do if you want to be reminded each time the timer cycles
}
}
}
])
}
else
{
ChangeMeCounter.postUpdate(0)
logInfo("Test Timer Rule","Counter complete.")
}
type: script.ScriptAction
Rule 2 Template Timer Temperature Trigger V2
trigger:
itemName: ChangeMeTemp
Changed
Do something here if you want to be notified immediately.
Maybe you want to be alerted if there is motion in the house or have a buzzer go off.
—CODE—
triggers:
- id: "1"
configuration:
itemName: ChangeMeTemp
type: core.ItemStateChangeTrigger
conditions: []
actions:
- inputs: {}
id: "2"
configuration:
type: application/vnd.openhab.dsl.rule
script: >
var Number temp = ChangeMeTemp.state as DecimalType
if ((temp > 28) && (ChangeMeSwitch.state == OFF) || (ChangeMeSwitch.state == NULL)) //!= in case switch is null
{
logInfo("Trigger Rule","ChangeMeTemp initialized. Current temp is at " + temp)
sendCommand(ChangeMeSwitch, ON)
ChangeMeCounter.postUpdate(5)
//Do something here if you want to be notified immediately.
}
if ((temp < 28) && (ChangeMeSwitch.state == ON) || (ChangeMeSwitch.state == NULL)) //!= in case switch is null
{
logInfo("Trigger Rule","ChangeMeTemp is ok. No trigger initialized. Current temp is at " + temp)
sendCommand(ChangeMeSwitch, OFF)
ChangeMeCounter.postUpdate(0)
}
type: script.ScriptAction
Rule 3 Template Timer Temperature Cancel V2 (optional)
Do something here if you want confirmation that the trigger item is resolved.
If you want confirmation that the temperature is OK now or the door is closed.
triggers:
itemName: ChangeMeSwitch
state: OFF
previousState: ON
—CODE—
triggers:
- id: "1"
configuration:
itemName: ChangeMeSwitch
state: OFF
previousState: ON
type: core.ItemStateChangeTrigger
conditions: []
actions:
- inputs: {}
id: "2"
configuration:
type: application/vnd.openhab.dsl.rule
script: >
logInfo("ChangeMeTemp Rule","ChangeMeTemp cancellation. Current temp is
at " + ChangeMeTemp.state)
//Do something here if you want confirmation that the trigger item is resolved.
type: script.ScriptAction
Rule 4 Template Template Timer wo/Counter V2 (optional)
Do something will continue to repeat until resolved.
Recheck for motion to turn nightlights off.
triggers:
itemName: ChangeMeSwitch
command: ON
itemName: ChangeMeRestartTimer
command: ON
—CODE—
triggers:
- id: "2"
configuration:
itemName: ChangeMeSwitch
command: ON
type: core.ItemCommandTrigger
- id: "3"
configuration:
itemName: ChangeMeRestartTimer
command: ON
type: core.ItemCommandTrigger
conditions: []
actions:
- inputs: {}
id: "1"
configuration:
type: application/vnd.openhab.dsl.rule
script: >
if (ChangeMeSwitch.state == ON)
{
sendCommand(GroupNightLights, ON)
logInfo("Test Timer","timer Created waiting 10 Seconds")
createTimer(now.plusSeconds(10),
[|
if (ChangeMeSwitch.state == ON)
{
sendCommand(ChangeMeRestartTimer, ON)
}
else
{
logInfo("Test Timer","Nightlight timer complete. Turning off lights.")
sendCommand(GroupNightLights, OFF)
}
])
}
else
{
}
type: script.ScriptAction