rules

Namespace

rules

Description:
  • Rules namespace. This namespace allows creation of openHAB rules.

Source:

Methods

(static) JSRule(ruleConfig) → {HostRule}

Description:
  • Creates a rule. The rule will be created and immediately available.

Source:
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}

Description:
  • Creates a rule, with an associated Switch Item that can be used to toggle the rule's enabled state. The rule will be created and immediately available. The Switch Item will be created automatically unless you pass a RuleConfigswitchItemName and an Item with that name already exists.

Source:
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

(static) isEnabled(uid) → {boolean}

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

Source:
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) removeRule(uid) → {boolean}

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

Source:
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)

Description:
  • 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).

Source:
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:

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

Type
Error

(static) setEnabled(uid, isEnabled)

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

Source:
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) when(withToggleopt) → {triggers.TriggerBuilder}

Description:
  • Create a new RuleBuilder chain for easily creating rules.

Source:
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