Hey Folks,
I have no pride of authorship and open to your solutions and improvements to these rules. This first post will have a door notification set of rules. I’ll post different iterations of these rules as I finish them on my OH3 installation.
Notes are in the code.
Rule 1:
Template Door Trigger V2
triggers:
- id: "1"
configuration:
itemName: ChangeMeDoor
type: core.ItemStateChangeTrigger
conditions: []
actions:
- inputs: {}
id: "2"
configuration:
type: application/vnd.openhab.dsl.rule
script: >-
/*---------------
The intent for these rules is to alert the administrator if a door is left open for a period of time. i.e. Your kids don’t close the garage freezer door all the way when getting a popsicle. I use Notepad ++ as an editor and find and replace to set the rule to any particular item.
ChangeMeTimer - Serves as what was a global timer variable in on OH2. Set the value of the timer to whatever suites your situation. I use 5 minutes for the refrigerator / freezer doors. Maybe you want a different period of time for another door. Like a notification if the garage is open for more than X period of time.
ChangeMeCounter – One limiting issue with the timer is that it could loop continuously unless the trigger is resolved. While it is useful to know that there is an issue with the door open, it may not be feasible to act on it immediately and you don’t want a repeating notification every 5 minutes. The timer allows the administrator to limit the number of times an alert is issued.
ChangeMeSwitch – Serves as the trigger for the counter and cancel rules.
------------Find and Replace the following-----------
--Description—
Template (provide a text description for logging) i.e. Freezer Door
--Existing Item--
ChangeMeDoor
--New Items--
Switch ChangeMeSwitch "Template switch V2" [AlertTemplate]
Switch ChangeMeCounter "Template counter V2" [AlertTemplate]
Number ChangeMeTimer "Template timer V2" [AlertTemplate]
--Rules--
Template Door Trigger V2 – Required to set the monitored item in the state of alert or normal
Template Door Timer w/Counter V2 – Required notify the administrator of the item state
Template Door Cancel V2 – Optional if the administrator wants notification of the item state as resolved
----------------*/
val door = ChangeMeDoor.state
logInfo("Template Trigger V2 Rule","Started. Door is " + door)
if ((door == "OPEN") && (ChangeMeSwitch.state == OFF) || (ChangeMeSwitch.state == NULL)
{
logInfo("Template Trigger V2 Rule","Initialized. Door is " + door)
sendCommand(ChangeMeSwitch, ON)
ChangeMeCounter.postUpdate(3) //This value needs to correlate with the ChangeMeCounter trigger item in the ruel Template Door Timer w/Counter V2
//Do something here if you want to be notified immediately. Warning.. You may not want to be notified every time the door is open.
}
if ((door == "CLOSED") && (ChangeMeSwitch.state == ON) || (ChangeMeSwitch.state == NULL)) //!= in case switch is null
{
logInfo("Template Trigger V2 Rule","No trigger initialized. All is ok. Door is " + door)
sendCommand(ChangeMeSwitch, OFF)
createTimer(now.plusSeconds(5),
[|
ChangeMeCounter.postUpdate(0) //delayed so that the rule Template Door Cancel V2 is not triggered if you don't want an immediate notifiation.
])
}
type: script.ScriptAction
Rule 2:
Template Door Timer w/Counter V2
triggers:
- id: "2"
configuration:
itemName: ChangeMeSwitch
command: ON
type: core.ItemCommandTrigger
- id: "3"
configuration:
itemName: ChangeMeTimer
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("Template Timer V2 Rule", "Initialized.")
createTimer(now.plusMinutes(5),
//createTimer(now.plusSeconds(15), //For testing
[|
var Number varCount = ChangeMeCounter.state as DecimalType
val door = ChangeMeDoor.state
if (varCount != 0)
{
if (varCount == 3)
{
varCount = varCount - 1
logInfo("Template Timer V2 Rule","Counter initialized. Current varCount is at " + varCount)
ChangeMeCounter.postUpdate(varCount)
sendCommand(ChangeMeTimer, ON)
//Do something after the first timer has cycled. i.e. the door has now been open for 5 minutes.
}
else
{
if (varCount == 1)
{
varCount = varCount - 1
logInfo("Template Timer V2 Rule","Counter This is the last notification. Current varCount is at " + varCount)
ChangeMeCounter.postUpdate(varCount)
sendCommand(ChangeMeTimer, ON)
//Do if you want to be notified the trigger items is not resolved and this is the last notification.
}
else
{
varCount = varCount - 1
logInfo("Template Timer V2 Rule","Counter repeating. Current varCount is at " + varCount)
ChangeMeCounter.postUpdate(varCount)
sendCommand(ChangeMeTimer, ON)
//Do if you want to be reminded each time the timer cycles
}
}
}
])
}
else
{
ChangeMeCounter.postUpdate(0)
logInfo("Template Timer V2 Rule","Counter complete.")
}
type: script.ScriptAction
Rule 3: Optional
Template Door Cancel V2
triggers:
- id: "1"
configuration:
itemName: ChangeMeSwitch
state: OFF
previousState: ON
type: core.ItemStateChangeTrigger
conditions:
- inputs: {}
id: "3"
configuration:
itemName: ChangeMeCounter
state: "3"
operator: "!="
type: core.ItemStateCondition
actions:
- inputs: {}
id: "2"
configuration:
type: application/vnd.openhab.dsl.rule
script: >-
val door = ChangeMeDoor.state
logInfo("Template Cancel V2 Rule","Resolved. Door is " + door)
//Do something here if you want confirmation that the trigger item is resolved.
type: script.ScriptAction