OpenHAB 4 "error while saving rule: bad request " while saving rule with script

HiHi

I have updated OpenHAB 3 to OpenHAB 4.
I can edit rules. But when I enter this rule, I cannot save it

OpenHAB 4 "error while saving rule: bad request "

Only when I remove id: “5” and id: “6” can I save the rule in the editor.

Any idea?

configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: Entrancesensor1_Bewegung
      previousState: ""
    type: core.ItemStateChangeTrigger
  - id: "3"
    configuration:
      itemName: Hallwaysensor1_Bewegung
    type: core.ItemStateChangeTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      command: ON
      itemName: Entrance
    type: core.ItemCommandAction
  - id: "4"
    configuration:
      itemName: Hallway
      command: ON
    type: core.ItemCommandAction
  - inputs: {}
    id: "5"
    configuration:
      script: |
        var ScriptExecution = Java.type('org.openhab.core.model.script.actions.ScriptExecution');
        var ZonedDateTime = Java.type('java.time.ZonedDateTime');
        if(entranceTimer !== null && entranceTimer.hasTerminated()) {
            entranceTimer = null;
        }
        if(entranceTimer === null) {
            entranceTimer = ScriptExecution.createTimer(ZonedDateTime.now().plusMinutes(2), function() {
                events.sendCommand("Entrance", OFF);
                entranceTimer = null;
            });
        } else {
            entranceTimer.reschedule(ZonedDateTime.now().plusMinutes(2));
        }
      type: script.ScriptAction
  - id: "6"
    configuration:
      script: |
        var ScriptExecution = Java.type('org.openhab.core.model.script.actions.ScriptExecution');
        var ZonedDateTime = Java.type('java.time.ZonedDateTime');
        if(hallwayTimer !== null && hallwayTimer.hasTerminated()) {
            hallwayTimer = null;
        }
        if(hallwayTimer === null) {
            hallwayTimer = ScriptExecution.createTimer(ZonedDateTime.now().plusMinutes(2), function() {
                events.sendCommand("Hallway", OFF);
                hallwayTimer = null;
            });
        } else {
            hallwayTimer.reschedule(ZonedDateTime.now().plusMinutes(2));
        }
      type: script.ScriptAction

Your syntax is wrong.
If you create the rules using the design mode, this error would not have happened.
You need to add

- inputs: {}

for each single action

1 Like

Also, the type: script.ScriptAction lines for both of the scripts are incorrectly indented. In yaml the level of the indentation matters and there are two possible type properties that can be filled in here. The first is a required type of the actual action that is a property of the action its self. That’s the line you have and because it is a property of the action itself, it needs to be at the same indentation level as the configuration property (similar to the trigger elements that are properly indented above it). You have it indented one extra level so now it is a sub-property of the configuration property.

The other type is what tells the rules engine which script language you have used (e.g., application/javascript) and that should be a sub-property of configuration.

So right now your script actions are not identified to the UI rule parser as script actions, and the text of the script itself is improperly identified as being in the “script.ScriptAction” scripting language instead of whatever language is actually is.

1 Like

Like this?

configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: Entrancesensor1_Bewegung
      previousState: ""
    type: core.ItemStateChangeTrigger
  - id: "3"
    configuration:
      itemName: Hallwaysensor1_Bewegung
    type: core.ItemStateChangeTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      command: ON
      itemName: Entrance
    type: core.ItemCommandAction
  - inputs: {}
    id: "4"
    configuration:
      itemName: Hallway
      command: ON
    type: core.ItemCommandAction
  - inputs: {}
    id: "5"
    configuration:
      type: script.ScriptAction
      scriptType: application/javascript
      script: |
        var ScriptExecution = Java.type('org.openhab.core.model.script.actions.ScriptExecution');
        var ZonedDateTime = Java.type('java.time.ZonedDateTime');
        if(entranceTimer !== null && entranceTimer.hasTerminated()) {
            entranceTimer = null;
        }
        if(entranceTimer === null) {
            entranceTimer = ScriptExecution.createTimer(ZonedDateTime.now().plusMinutes(2), function() {
                events.sendCommand("Entrance", OFF);
                entranceTimer = null;
            });
        } else {
            entranceTimer.reschedule(ZonedDateTime.now().plusMinutes(2));
        }
  - inputs: {}
    id: "6"
    configuration:
      type: script.ScriptAction
      scriptType: application/javascript
      script: |
        var ScriptExecution = Java.type('org.openhab.core.model.script.actions.ScriptExecution');
        var ZonedDateTime = Java.type('java.time.ZonedDateTime');
        if(hallwayTimer !== null && hallwayTimer.hasTerminated()) {
            hallwayTimer = null;
        }
        if(hallwayTimer === null) {
            hallwayTimer = ScriptExecution.createTimer(ZonedDateTime.now().plusMinutes(2), function() {
                events.sendCommand("Hallway", OFF);
                hallwayTimer = null;
            });
        } else {
            hallwayTimer.reschedule(ZonedDateTime.now().plusMinutes(2));
        }

No. You have not changed the indentation at all (order is irrelevant within the same indentation level) and there’s no such thing as scriptType. There are two different properties that are both named type, but because they are at different indentation levels in the yaml they are different properties.

Oliver is quite correct:

Even if you insist on typing these rules in to the code box (which is, for most users not a good idea), at least please create a dummy rule using the wizard and look at the structure that rule has. Guessing is going to take you much longer and just frustrate you.

@DiViNe , this is what I did to help you. I would never know this out of the top of my head.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.