Skip to content

Rule Builder .for() invalid instantiation of TimingItemStateOperation #447

Description

@hauerm

Hi @florian-h05,

I cannot get a time based item state change rule to work using rule builder. Please refer to the error message from the logs in my description. Maybe the constructor of the TimingItemStateOperation is not used correctly by the for(timespan) method. Thanks for having a quick look.

https://github.com/openhab/openhab-js/blame/v5.11.1/src/rules/trigger-builder.js#L368

Snippet

  /**
   * For timespan
   * @param {*} timespan
   * @returns {ItemTriggerConfig}
   */
  for (timespan) {
    return new operations.TimingItemStateOperation(this, timespan);
  }

https://github.com/openhab/openhab-js/blame/v5.11.1/src/rules/operation-builder.js#L437

Snippet

  constructor (operationBuilder, itemChangedTriggerConfig, duration) {
    super(operationBuilder);
    if (typeof itemChangedTriggerConfig.to_value === 'undefined') {
      throw Error('Must specify item state value to wait for!');
    }

My call:

rules
  .when()
  .item("SomeItem")
  .changed()
  .toOn() // I also tried fromOff()
  .for("3m")
  .then((event) => {
...
}

Expected behaviour

The call from above should work considering the README.MD.

Current Behaviour

The following error is logged when the script is loaded.

Failed to execute script: Error: Must specify item state value to wait for! at <js>.TimingItemStateOperation(operation-builder.js:440)

Possible solution:

As far as I see, the for-method does not use the TimingItemStateOperation correctly as it expects an OperationBuilder as first parameter, RuleBuilder as second parameter and duration as third paramenter.
The instantiation in the for method described above just passes the RuleBuilder instance (this) and the duration. The following check in the constructur regarding itemChangedTriggerConfig.to_value fails, as it is executed upon the duration parameter I think.

Reproduction

Write a rule using a for(duration) construct.

Environment

OH5M4, openhabjs 5.11.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions