rules

rules

Source:

Rules namespace. This namespace allows creation of openHAB rules.

Methods

(static) when(withToggleopt) → {triggers.TriggerBuilder}

Source:

Create a new RuleBuilder chain for easily creating rules.

Examples

Basic rule

rules.when().item("F1_Light").changed().then().send("changed").toItem("F2_Light").build("My Rule", "My First Rule");

Rule with function

rules.when().item("F1_light").changed().to("100").then(event => {
  console.log(event)
 }).build("Test Rule", "My Test Rule");
Parameters:
Name Type Attributes Default Description
withToggle boolean <optional>
false

rule can be toggled on or off (optional)

Returns:

rule builder

Type
triggers.TriggerBuilder

(static) removeRule(uid) → {boolean}

Source:

Remove a rule when it exists. The rule will be immediately removed. Only works for rules created in the same file.

Parameters:
Name Type Description
uid string

the UID of the rule

Returns:

whether the rule was actually removed

Type
boolean

(static) runRule(uid, argsopt, condopt)

Source:

Runs the rule with the given UID. Throws errors when the rule doesn't exist or is unable to run (e.g. it's disabled).

Parameters:
Name Type Attributes Default Description
uid string

the UID of the rule to run

args object <optional>
{}

args optional dict of data to pass to the called rule

cond boolean <optional>
true

when true, the called rule will only run if it's conditions are met

Throws:

Will throw an error if the rule does not exist or is not initialized.

(static) isEnabled(uid) → {boolean}

Source:

Tests to see if the rule with the given UID is enabled or disabled. Throws and error if the rule doesn't exist.

Parameters:
Name Type Description
uid string
Throws:

an error when the rule is not found.

Type
Error
Returns:

whether or not the rule is enabled

Type
boolean

(static) setEnabled(uid, isEnabled)

Source:

Enables or disables the rule with the given UID. Throws an error if the rule doesn't exist.

Parameters:
Name Type Description
uid string

UID of the rule

isEnabled boolean

when true, the rule is enabled, otherwise the rule is disabled

Throws:

an error when the rule is not found.

Type
Error

(static) JSRule(ruleConfig) → {HostRule}

Source:

Creates a rule. The rule will be created and immediately available.

Example
import { rules, triggers } = require('openhab');

rules.JSRule({
 name: "my_new_rule",
 description: "this rule swizzles the swallows",
 triggers: triggers.GenericCronTrigger("0 30 16 * * ? *"),
 execute: (event) => { // do stuff }
});
Parameters:
Name Type Description
ruleConfig RuleConfig

The rule config describing the rule

Throws:

an error if the rule with the passed in uid already exists and RuleConfig.overwrite is not true

Type
Error
Returns:

the created rule

Type
HostRule

(static) SwitchableJSRule(ruleConfig) → {HostRule}

Source:

Creates a rule, with an associated SwitchItem that can be used to toggle the rule's enabled state. The rule will be created and immediately available.

Parameters:
Name Type Description
ruleConfig RuleConfig

The rule config describing the rule

Throws:

an error is a rule with the given UID already exists.

Type
Error
Returns:

the created rule

Type
HostRule