Skip to content

Commit

Permalink
Merge pull request #94 from rkoshak/ruleDelay
Browse files Browse the repository at this point in the history
Added rule start delay template
  • Loading branch information
rkoshak authored Mar 6, 2023
2 parents e23c7cf + a39b0ac commit d98be9f
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
4 changes: 4 additions & 0 deletions rule-templates/ruleStartDelay/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Delay Start
Initially triggers at system runlevel 40 and disables a user selected list of rules.
Triggers again at system runlevel 100 plus a user configurable delay to reenable the rules.
See the [Marketplace posting](https://community.openhab.org/t/delay-start-4-0-0-0-4-1-0-0/144884) for full details.
73 changes: 73 additions & 0 deletions rule-templates/ruleStartDelay/ruleStartDelay.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
uid: rules_tools:delay_start
label: Delay Start
description: Triggers at system runlevel 40 and disables a set of rules. Then triggers at a user selected system runlevel and reenables those rules.
configDescriptions:
- name: rules
label: Rules
description: Rules to disable during startup.
type: TEXT
context: rule
required: true
multiple: true
- name: pause
label: Reenable Delay
description: How long after the second trigger to wait before reenabling the rules, ISO8601 duration format. Defaults to reenable them immediately.
type: TEXT
defaultValue: ''
required: false
triggers:
- id: "1"
configuration:
startlevel: 40
type: core.SystemStartlevelTrigger
- id: "2"
configuration:
startlevel: 100
type: core.SystemStartlevelTrigger
conditions: []
actions:
- inputs: {}
id: "3"
configuration:
type: application/javascript
script: >-
var {helpers} = require('openhab_rules_tools');
console.loggerName = 'org.openhab.automation.rules_tools.Delayed Start';
helpers.validateLibraries('4.1.0', '2.0.1');
// Properties
var disabledRules = '{{rules}}'.replace('[','').replace(']', '').split(', ');
var delay = '{{pause}}';
// First time the rule is called at runlevel 40 the flag will be initialized to false
var alreadyCalled = cache.private.get('flag', () => false);
var timerTime = (alreadyCalled) ? delay : 0; // run immediately the first run of the rule
var activity = (alreadyCalled) ? 'enabling' : 'disabling';
var when = (alreadyCalled) ? 'after ' + delay : 'immediately';
console.info('Delayed Start triggered, ' + activity + ' rules ' + when);
helpers.createTimer(timerTime, () => disabledRules.forEach(rule => {
console.info(activity, rule);
try {
rules.setEnabled(rule, alreadyCalled);
}
catch(e) {
console.error('Failed to disable rule ' + rule + ': ' + e);
}
}), 'delayedStart');
cache.private.put('flag', true);
type: script.ScriptAction

0 comments on commit d98be9f

Please sign in to comment.