How to run a rule from within a ECMA 2011 rule

I have a rule that opens the blinds on the ground floor. The individual blinds are not opened all at once, but in series, just as a person would go from switch to switch. This works nicely so far.
Now I want to run this rule from a ECMA 2011 javascript rule. I narrowed it down to just the exec part to see how it works.

Here is the trigger:

configuration: {}
triggers: []
conditions: []
actions:
  - inputs: {}
    id: "1"
    configuration:
      type: application/javascript;version=ECMAScript-2021
      script: exec.executeCommandLine("curl -X POST
        http://127.0.0.1:8080/rest/rules/AAA_Test/runnow");
    type: script.ScriptAction

I’ve got this error:

org.graalvm.polyglot.PolyglotException: ReferenceError: “exec” is not defined

Unfortunately the documentation for java script in this area is not available.
Can someone show me how to solve this please or show me where I can find the documentation.

Thanks a lot.

The helper library docs for the JSScripting rules can be found here and the full reference here.

But right now, you’re trying to reinvent the wheel (several times over). You do not need to call the rest API just to run another rule. You can do it from the helper library using runRule command. But, even better, you don’t need any scripting at all to run another rule. When you are building your rule just select the “Other rules” action in the action wizard:


and then find the rule you want to run.

1 Like

Thanks Justin for your help but actually I want to run the “other rule” from a more complex script. This one above was just to check how to do it generally. So here is the script I want to call the other from:

/*
    Dieses Script oeffnet die Rolllaeden am Morgen. Ist es zu den 
    Oeffnungszeiten noch dunkel bleiben sie zu bis die Uhrzeit
    erreicht ist.
    
    Es gelten unterschiedliche oeffnungszeiten fuer 
    - Wochentags  ( 7 Uhr ) und 
    - Wochenenden ( 9 Uhr ).
    
    Die Ausloeser dieses Scripts sind:
    - Sonnenaufgang
    - 7 Uhr
    - 9 Uhr
    
    Ist also der Sonnenaufgang nach 7 bzw. 9 Uhr geht der Rollladen 
    zum Sonnenaufgang auf und nicht zu den Uhrzeiten (7-, 9-Uhr).
    
*/
daysOfWeek    = ["SO", "MO", "DI", "MI", "DO", "FR", "SA"];
daysOfWeekend = ["SA", "SO"];
sunPhases     = ["DAYLIGHT", "SUN_RISE", "CIVIL_DAWN"];

currentTime = new Date();
currentDayOfWeek = currentTime.getDay();
currentWeekdayName = daysOfWeek[currentDayOfWeek];
currentSunPhase = items.getItem("Lokale_Sonnendaten_Sonnenphase").state;

time9AM = new Date();
time9AM.setHours(9, 0, 0, 0);
time7AM = new Date();
time7AM.setHours(7, 0, 0, 0);

console.log("Script rollos_morgens_rauf gestartet");
console.log("Sonnenphase ist " + currentSunPhase);

if ( sunPhases.includes(currentSunPhase) ) {
  if ( daysOfWeekend.includes(currentWeekdayName) ) {
    // Hier sind wir am Wochenende
    console.log("Wochenende " + currentWeekdayName);
    if ( currentTime < time9AM ) {
      console.log("aktuelle Zeit ist vor 9 Uhr");
      console.log("keine Aktion!");
    } else {
      console.log("aktuelle Zeit ist nach 9 Uhr");
      console.log("Starte ROLLO hochfahren Wochenende!");
      /*
        at this point I want to trigger the other rule
      */
    } 
  } else {
    // Hier ist Werktag
    console.log("Wochentag " + currentWeekdayName);
    if ( currentTime < time7AM ) {
      console.log("aktuelle Zeit ist vor 7 Uhr");
      console.log("keine Aktion!");
    } else {
      console.log("aktuelle Zeit ist nach 7 Uhr");
      console.log("Starte ROLLO hochfahren unter der Woche!");
      /*
        at this point I want to trigger the other rule
      */
    }
  }
}

You see that this is harder to achieve with what is offered by the UI.

Any other suggestions?

Though, as it’s written this isn’t so tough for a UI rule. The only complicated part are the sun phases. The rest could be all UI.

Also, you are doing the daysOfWeek part the hard way. Use Ephemeris.

configuration: {}
triggers:
  - id: "1"
    configuration:
      time: 09:00
    type: timer.TimeOfDayTrigger
conditions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/javascript
      script: >-
        sunPhases = ["DAYLIGHT", "SUN_RISE", "CIVIL_DAWN"];

        daysOfWeekend.includes(items.getItem("Lokale_Sonnendaten_Sonnenphase").state);
    type: script.ScriptCondition
  - inputs: {}
    id: "3"
    configuration:
      offset: 0
    type: ephemeris.WeekdayCondition
actions:
  - id: "4"
    configuration:
      ruleUIDs:
        - scratchpad
    type: core.RunRuleAction
configuration: {}
triggers:
  - id: "1"
    configuration:
      time: 07:00
    type: timer.TimeOfDayTrigger
conditions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/javascript
      script: >-
        sunPhases = ["DAYLIGHT", "SUN_RISE", "CIVIL_DAWN"];

        daysOfWeekend.includes(items.getItem("Lokale_Sonnendaten_Sonnenphase").state);
    type: script.ScriptCondition
  - inputs: {}
    id: "3"
    configuration: {}
    type: ephemeris.WeekendCondition
actions:
  - id: "4"
    configuration:
      ruleUIDs:
        - scratchpad
    type: core.RunRuleAction

It can be so easy :relieved:

Instead of having one for all occasions, I can of course have 3 for each with conditions.

Thanks guys for your help.

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