I am trying to make a rule to turn off teh HVAC if teh door is left openfor more than x amount of time. That works OK but if teh door is opened and then closed while the timer is running the timer is not canceled. I’ve tried some different variations with no luck. Here is how I have the rule set up:
When - The door sensor changes from any to any
Then - Blockly script (check for door sensor = OPEN, start timer, turn off HVAC when timed out, cancel if retriggered)
Blockly script:
Hmm, won’t let me upload image…
Rule code:
configuration: {}
triggers:
- id: "2"
configuration:
itemName: Door_Sensor_Front_Door
type: core.ItemStateChangeTrigger
conditions: []
actions:
- inputs: {}
id: "1"
configuration:
blockSource: <xml xmlns="https://developers.google.com/blockly/xml"><block
type="controls_if" id="-d?#@^Qt}psU~AK[!|`Z" x="38" y="12"><value
name="IF0"><block type="logic_compare" id="u2Y,p-Bk~xFrajRua(?l"><field
name="OP">EQ</field><value name="A"><block type="oh_getitem_state"
id="]uR.x^^8g{r|aoBj(U*4"><value name="itemName"><shadow type="oh_item"
id=",G(wRIfF!)0mRgMUoPTW"><field
name="itemName">Door_Sensor_Front_Door</field></shadow></value></block></value><value
name="B"><block type="text" id="L-)awhPsS3tdLCa3RnpQ"><field
name="TEXT">OPEN</field></block></value></block></value><statement
name="DO0"><block type="oh_log" id="EvS:Dw2V9-V5d*@8)Evq"><field
name="severity">info</field><value name="message"><shadow type="text"
id="V,A;6--|N.+5P.ZeWh0;"><field name="TEXT">Door Open HVAC Off Rule
Started</field></shadow></value><next><block type="oh_log"
id="%$YI!NUC49z3ke)w0k1,"><field name="severity">info</field><value
name="message"><shadow type="text" id="G^3AUxe9wc$hOyunX}/%"><field
name="TEXT">Door Open Timer Started</field></shadow></value><next><block
type="oh_timer_ext" id="s.lI~FEDhX|*^T*?vmkq"><field
name="delayUnits">plusMinutes</field><field
name="retrigger">cancel</field><value name="delay"><shadow
type="math_number" id="?23;@*F}5G#8*s0E.eGK"><field
name="NUM">1</field></shadow></value><value name="timerName"><shadow
type="text" id=",uW.!XZI]EfKs[zq(QQp"><field name="TEXT">Door Open
Timer</field></shadow></value><statement name="timerCode"><block
type="oh_event" id="HA_,?eO9BM(7=r=CH-q."><field
name="eventType">sendCommand</field><value name="value"><shadow
type="text" id="JTAj2L3s.aro.6%E{pi5"><field
name="TEXT">off</field></shadow></value><value name="itemName"><shadow
type="oh_item" id="f5A~-tiWVpR8p!golz[X"><field
name="itemName">Ecobee_Mode</field></shadow></value><next><block
type="oh_event" id="qY3!p|2H_|7_GqJCai:G"><field
name="eventType">sendCommand</field><value name="value"><shadow
type="text" id="BoFh0~Au$JwDQj}u3Eld"><field
name="TEXT">OFF</field></shadow></value><value name="itemName"><shadow
type="oh_item" id="_3cnMR%nx4*{[t[B,d!E"><field
name="itemName">Dehu_Wall_Switch</field></shadow></value><next><block
type="oh_log" id="S$L71uS0Y^vJ+*,Y^8_{"><field
name="severity">info</field><value name="message"><shadow type="text"
id="(l-pDZNVlF,lu1/F/yzr"><field name="TEXT">Ecobee & Dehu Wall
Switch Turned
Off</field></shadow></value></block></next></block></next></block></statement></block></next></block></next></block></statement></block></xml>
type: application/javascript
script: >
var logger =
Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' +
ctx.ruleUID);
var scriptExecution = Java.type('org.openhab.core.model.script.actions.ScriptExecution');
var zdt = Java.type('java.time.ZonedDateTime');
if (typeof this.timers === 'undefined') {
this.timers = [];
}
if (itemRegistry.getItem('Door_Sensor_Front_Door').getState() == 'OPEN') {
logger.info('Door Open HVAC Off Rule Started');
logger.info('Door Open Timer Started');
if (typeof this.timers['Door Open Timer'] === 'undefined' || this.timers['Door Open Timer'].hasTerminated()) {
this.timers['Door Open Timer'] = scriptExecution.createTimer(zdt.now().plusMinutes(1), function () {
events.sendCommand('Ecobee_Mode', 'off');
events.sendCommand('Dehu_Wall_Switch', 'OFF');
logger.info('Ecobee & Dehu Wall Switch Turned Off');
})
} else {
this.timers['Door Open Timer'].cancel();
this.timers['Door Open Timer'] = undefined;
}
}
type: script.ScriptAction